A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: 15 buttons script

  1. #1
    Junior Member
    Join Date
    Feb 2022
    Posts
    27

    15 buttons script

    Hi, I have 15 buttons on Stage.
    All of them are named similar except numbers (1-15)
    Action Scripts are also the same except numbers (1-15).
    Script is pretty long, can it be optimized using something like "for" cycle or arrays?

    Code:
    //path button 1
    this.path_button_1.enabled = true;
    this.path_button_1.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_1.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_1.onPress = function() {
    	character.goToNode("node_1", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_1);
    };
    
    //path button 2
    this.path_button_2.enabled = true;
    this.path_button_2.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_2.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_2.onPress = function() {
    	character.goToNode("node_2", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_2);
    };
    
    //path button 3
    this.path_button_3.enabled = true;
    this.path_button_3.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_3.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_3.onPress = function() {
    	character.goToNode("node_3", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_3);
    };
    
    //path button 4
    this.path_button_4.enabled = true;
    this.path_button_4.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_4.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_4.onPress = function() {
    	character.goToNode("node_4", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_4);
    };
    //path button 5
    this.path_button_5.enabled = true;
    this.path_button_5.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_5.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_5.onPress = function() {
    	character.goToNode("node_5", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_5);
    };
    //path button 6
    this.path_button_6.enabled = true;
    this.path_button_6.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_6.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_6.onPress = function() {
    	character.goToNode("node_6", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_6);
    };
    //path button 7
    this.path_button_7.enabled = true;
    this.path_button_7.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_7.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_7.onPress = function() {
    	character.goToNode("node_7", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_7);
    };
    //path button 8
    this.path_button_8.enabled = true;
    this.path_button_8.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_8.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_8.onPress = function() {
    	character.goToNode("node_8", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_8);
    };
    
    //path button 9
    this.path_button_9.enabled = true;
    this.path_button_9.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_9.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_9.onPress = function() {
    	character.goToNode("node_9", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_9);
    };
    
    //path button 10
    this.path_button_10.enabled = true;
    this.path_button_10.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_10.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_10.onPress = function() {
    	character.goToNode("node_10", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_10);
    };
    
    //path button 11
    this.path_button_11.enabled = true;
    this.path_button_11.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_11.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_11.onPress = function() {
    	character.goToNode("node_11", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_11);
    };
    
    //path button 12
    this.path_button_12.enabled = true;
    this.path_button_12.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_12.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_12.onPress = function() {
    	character.goToNode("node_12", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_12);
    };
    
    //path button 13
    this.path_button_13.enabled = true;
    this.path_button_13.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_13.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_13.onPress = function() {
    	character.goToNode("node_13", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_13);
    };
    
    //path button 14
    this.path_button_14.enabled = true;
    this.path_button_14.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_14.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_14.onPress = function() {
    	character.goToNode("node_14", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_14);
    };
    
    //path button 15
    this.path_button_15.enabled = true;
    this.path_button_15.onRollOver = function() {
    	_root.module.cursor.setState("walk");
    };
    this.path_button_15.onRollOut = function() {
    	_root.module.cursor.resetState();
    	_root.cursor.stats = "";
    };
    this.path_button_15.onPress = function() {
    	character.goToNode("node_15", characterMoveCallback, false);
    	showbtn();
    	hidebtn(path_button_15);
    };
    
    
    //button hiding script
    function hidebtn(btn:Button) {
    	btn.enabled = false;
    	btn._visible = false;
    	trace("button "+btn+" hidden");
    }
    //switch for enabling all buttons
    function showbtn() {
    	var button:String = "path_button_";
    	for (var i = 1; i<=15; i++) {
    		//trace(button+i);
    		i.toString();
    		this[button+i]._visible = true;
    		this[button+i].enabled = true;
    	}
    	trace("all buttons are shown");
    }
    15_buttons.fla

  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    PHP Code:
    for (var 1i<=15i++) {
    this["path_button_"+i].enabled true;
    this["path_button_"+i].onRollOver = function() {
    _root.module.cursor.setState("walk");
    };
    this["path_button_"+i].onRollOut = function() {
    _root.module.cursor.resetState();
    _root.cursor.stats "";
    };
    this["path_button_"+i].onPress = function() {
    character.goToNode("node_1"characterMoveCallbackfalse);
    showbtn();
    hidebtn(this);
    }
    }

    function 
    hidebtn(btn:Button) {
    btn.enabled false;
    btn._visible false;
    trace("button "+btn+" hidden");
    }

    function 
    showbtn() {
    var 
    button:String "path_button_";
    for (
    1i<=15i++) {
    i.toString();
    this[button+i]._visible true;
    this[button+i].enabled true;
    }
    trace("all buttons are shown");


  3. #3
    Junior Member
    Join Date
    Feb 2022
    Posts
    27
    thanks.
    It's almost perfect. = )
    Is there a correct syntax to character.goToNode part?
    it's also changing from 1 to 15 (not just "node1" every time).
    I've tried character.goToNode(["node_"+i], characterMoveCallback, false); but it's not working for some reason
    Last edited by Alex_Cherkasoff; 09-14-2022 at 01:41 PM.

  4. #4
    Junior Member
    Join Date
    Feb 2022
    Posts
    27
    PHP Code:
    for (var 1i<=15i++) {
    this["path_button_"+i].enabled true;
    this["path_button_"+i].onRollOver = function() {
    _root.module.cursor.setState("walk");
    };
    this["path_button_"+i].onRollOut = function() {
    _root.module.cursor.resetState();
    _root.cursor.stats "";
    };
    this["path_button_"+i].onPress = function() {
        
    newnode "node_" this._name.slice(12,14);
    character.goToNode(newnode,characterMoveCallback,false);
    trace(newnode);
    showbtn();
    hidebtn(this);

    }
    }

    function 
    hidebtn(btn:Button) {
    btn.enabled false;
    btn._visible false;
    trace("button "+btn+" hidden");
    }

    function 
    showbtn() {
    var 
    button:String "path_button_";
    for (
    1i<=15i++) {
    i.toString();
    this[button+i]._visible true;
    this[button+i].enabled true;
    }
    trace("all buttons are shown");

    I kinda figured it out by myself = )
    it's a some sort of "cheating"... but, oh well = )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center