A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] Hi, newbie question about concat

Hybrid View

  1. #1
    MindGem Graphics Inc. JediMind's Avatar
    Join Date
    Nov 2001
    Location
    Stockholm/Sweden
    Posts
    407

    resolved [RESOLVED] Hi, newbie question about concat

    Hello.

    I was a great flash programmer in Flash 4.0 But things have changed and I'm not back to the newbie category.


    So I'm wondering if I can concatinate variable names so I can make a loop creating several variables for example.

    So here's how I make a new instance of a movieclip by linkage.
    var ball1:MovieClip = new Ball();
    addChild(ball1);
    ball1.x = 300;
    ball1.y = 250;


    Can I somehow do like this:

    var i:int=1;
    var ["ball"i]:MovieClip = new Ball();
    addChild(["ball"+i]);
    ["ball"+i].x = 300;
    ["ball"+i].y = 250;


    Where I can put that in a loop and make 10 clips at once, same code just increase the variable i.
    Obiously the syntax is wrong but is there a way?

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    Yes, but don't.
    What you'd be doing is dynamically adding properties to an object or a dynamic subclass of object. Not all classes are dynamic so this is not a reliable habit. For instance, MovieClip is dynamic, Sprite is not.

    You'd have to evaluate the names to loop over them later which is expensive.

    Instead, push them to an array and loop over that.

    Actionscript Code:
    var myArray:Array = new Array();
    for(var i:int = 0; i < 10; i++){
      var ball:Ball = new Ball();
      ball.x = 10 * i;
      ball.y = 5 * i;
      myArray.push(ball);
    }

    then later you can loop over the balls like this

    Actionscript Code:
    for(var i:int = 0; i < myArray.length; i++){
      var ball:Ball = myArray[i];
      // do something with ball
    }

  3. #3
    MindGem Graphics Inc. JediMind's Avatar
    Join Date
    Nov 2001
    Location
    Stockholm/Sweden
    Posts
    407
    thx. Understand completely

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