2 Replies Latest reply on Nov 19, 2012 5:10 PM by kglad

# How can I change actions parameters within a function using different function

Cant think of another way to ask it, I've looked EVERYWHERE and tried doing it to my best of my knowledge but no luck.
Suppose I got 10 squares respectfully named sq1, sq2... sq10 and I have a 10 button that, when pressed, calls a function that makes its respectful  square move and change color. This would be a problem if it were only 10 squares and just do 2 actions, just change the instance name of each function... something like:

button1.onPress = movesquare1 ();

button2.onPress = movesquare2();

....... and so on

function movesquare1 ();

sq1._x = sq._x + 1;

function movesquare2 ();

sq2._x = sq._x + 1;

....... and so on

is there a way to have a general movesquare function, designate an N variable so that when a button is pressed the N variable would be designated?

something like:

button1.onPress = movesquare(n=1)

function movesquare ();

sqn._x = sqn._x + 1;

this way I only have the change the number of n=1 so that it points to the right sq and this avoiding writing  A LOT of movesquare functions to accommodate for each sq...

Long post but tried to explain my dilemma it the best way possible.

Tried using var:String and var:number but no luck...

Many thanks

• ###### 1. Re: How can I change actions parameters within a function using different function

You can make use of bracket notation to use string values to target objects.  First think in terms of passing a number to the function such as shown below...

button1.onPress = function() { movesquare("1") };

button2.onPress = function() { movesquare("2") };

function movesquare(num) {

this["sq"+num]._x = sq._x + 1;

}

• ###### 2. Re: How can I change actions parameters within a function using different function

there are a number of ways to do this.  one would be to use your button's _name property:

var buttonNumber:Number = 10;

for(var i:Number=1;i<=buttonNumber;i++){

this["button"+i].onPress = movesquare;

}

function movesquare():Void{

this["sq"+this._name.substring(6)]._x++;

}