-
Dynamic Variables
The code below works fine to make a red ball from my library dance around screen. But I want to have a hundred balls doing the same thing. In the old days I'd use the eval command with duplicatemovieclip and now it's the square brackets but .... var container1 = new RedBall(); this line works,
var this["container"+n] = new RedBall(); - THIS DOESN'T!!!
But if you look at the code below. Once the variable has been created in line 3 I can refer to it dynamically. But I need to 'create' it dynamically too. Anyone help, going mental??? Big Thanks.
JAB
To use the code below simply put a symbol called RedBall into your library.
var n = 1;
var jjj = new YellowContainer();
var container1 = new RedBall();// THIS LINE WORKS FINE BUT IS NOT DYNAMIC, I WANT TO USE THE ONE BELOW.
// var this["container"+n] = new RedBall(); // THIS LINE SHOULD WORK BUT DOESN'T WHICH IS ANNOYING ME......
this["container"+n].x = 200;//BUT I CAN START USING THE DYNAMIC VARIABLE LIKE THIS ONCE IT'S FIRST CALLED IN THE 3RD LINE (var container1 = new RedBall();)
this["container"+n].y = 100;
this.addChild(this["container"+n]);
container1.addEventListener(Event.ENTER_FRAME, themain);
function themain(event:Event):void {
this["container"+n].x = Math.random()*200.0;
}
-
have you considered using arrays to reference the objects?
PHP Code:
/// init-vars///
var n:Number = 1;
//var jjj:YellowContainer = new YellowContainer; This wasn't used in this example
var RedBalls:Array = new Array();
///////////////
// the below can be in a function, or loop or what ever
var tempContainer:RedBall = new RedBall();
tempContainer.x = 200;
tempContainer.y = 100;
addChild(tempContainer);
tempContainer.addEventListener(Event.ENTER_FRAME, themain);
RedBalls.push(tempContainer); // this adds tempContainer to the end of the array, .unshift would add it to the beginning
///////////////
function themain(event:Event):Void
{
event.target.x = Math.random()*200;
}
the advantage of using an array is now RedBalls has a bunch of characteristics with it.
PHP Code:
RedBalls[INTEGER].x += 10 // to call a specific one
RedBalls.length // gives you how many objects are in it
for(var i:Number = 0; i<RedBalls.length; i++){
RedBalls[i].x += 3; // updates EVERY RED BALL
}
// many more useful functions can be had by using arrays.
Sorry if you understood arrays already.
-
You The Man
Thanks so much for that, I did get it working. I'm a director games programmer and can program in that as fast as I can think.
I got into a bad habit with flash of using duplicatemovieclip and the EVAL command. Which in the original actionscript was nearly the only way to get things done.
Now in actionscript 3 it's all gone (along with just about everything else) and I just wanted to figure out that one thing.
The speed of AS3 is great but the swapping of capital letters in commands (Void v void etc) is SO annoying.
At least they're releasing the new version of Director soon.
JAB
-
In general I agree with the array approach as well, but just note that you can dynamically define variables using this["something"+i] ...you just can't use the var declaration when you do.