A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Dynamic Variables

  1. #1
    Junior Member
    Join Date
    Aug 2007
    Posts
    2

    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;
    }

  2. #2
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    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.200;
    tempContainer.100;
    addChild(tempContainer);
    tempContainer.addEventListener(Event.ENTER_FRAMEthemain);
    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.Math.random()*200;

    the advantage of using an array is now RedBalls has a bunch of characteristics with it.

    PHP Code:
    RedBalls[INTEGER].+= 10 // to call a specific one

    RedBalls.length // gives you how many objects are in it
    for(var i:Number 0i<RedBalls.lengthi++){
         
    RedBalls[i].+= 3;  // updates EVERY RED BALL
    }

    // many more useful functions can be had by using arrays. 
    Sorry if you understood arrays already.
    Last edited by deadlock32; 08-05-2007 at 12:35 AM.

  3. #3
    Junior Member
    Join Date
    Aug 2007
    Posts
    2

    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

  4. #4
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    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.

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