A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: dynamically generated movie clip names.

  1. #1
    Junior Member
    Join Date
    Dec 2002
    Posts
    29

    dynamically generated movie clip names.

    In AS2 I would do this:

    for(i=0;i<10;i++){
    this.attachMovie("clipLinkageName", "instanceName" + i ,this.getNextHighestDepth());
    }


    But now in AS3, I'm learning to use:

    var instanceName1:clipClassName = new clipClassName();
    addChild(instanceName1);


    But I don't know how I can put that in a for loop like I did above..

    this["instanceName" +i]:clipClassName = new ClipClassName();

    does not work.

    Can anyone help me out?

  2. #2
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    You won't be able to do that. You cannot instantiate the variable like that. (Not that I know of anyways).

    However, you can assign a name to your dynamically generated object and retrieve it with the following method:

    var myMC:MovieClip = new MovieClip();

    myMc.name = "instanceName" + i;

    addChild(myMc);

    and then to retrieve that MovieClip:

    getChildByName("instanceName + i);

  3. #3
    Junior Member
    Join Date
    Dec 2002
    Posts
    29
    Thanks for the reply. I understand what you mean but it doesn't quite cover what I want to do.

    I used to do things like this in AS2:

    var ID = 0;
    var unitType = "monster1";
    for(i=0;i<10;i++){
    this.attachMovie(unitType,"sprite" + ID,this.getNextHighestDepth());
    this["sprite" + ID]._x = 50 + (ID *30);
    this["sprite" + ID]._y = 50;
    ID++;
    }

    How can I do the same in AS3?
    Last edited by homeDrone; 09-04-2010 at 05:51 PM.

  4. #4
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    You can refer to variables that way, but you cannot instantiate them like that.

    So you could for instance create all your sprite variables:

    var sprite1:Sprite;
    var sprite2:Sprite;
    var sprite3:Sprite;

    And then create/refer to them like this:

    this["sprite" + ID] = new Sprite();
    this["sprite" + ID].x = 0;

    But you concatenate new variable names on the fly.

  5. #5
    Junior Member
    Join Date
    Dec 2002
    Posts
    29
    right, I get that. Maybe it was a cheat, but I was basically able to create new objects that way in AS2..

    I want a system of generating monsters for a game.. that way, I'd have to have set 100s of vars at the game start. Some might never get used and if the limit was reached, that would be it.

    var monster0
    var monster1
    var monster2
    ...
    var monster100

    AS2 did it so easy.. what am I missunderstanding about AS3?

    Thanks again for your help Beathoven.

  6. #6
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    The way AS3 works is completly different. Unfortunately some of the things that seemed so easy in AS2 are not possible anymore. However the power increase is worth the trade.

    The .name way is the best way. You can assign a name to a sprite.

  7. #7
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Try using an array. I find that they are very useful for this kind of thing. Do something like:
    sprite.push(new Sprite());
    then you can refer to it like this:
    sprite[0].x or something like that.
    .

  8. #8
    Member
    Join Date
    Dec 2009
    Posts
    50
    I use arrays for this kind of thing.
    I don't know if it's the most efficient method, but it works fine for me... until I get too complicated for myself to follow, but, you know... it works.

    What I just figured out yesterday is that if you only declare that that object is a new object at the beginning of your code, then add it to an array, then changing that object will change it in the array, but if you declare it as a new object every time you want to make a new one and add it to the array, it will be a different object stored in the array.

    So, for every iteration of making a new object, you just have to have;
    PHP Code:
    var instanceName1:clipClassName = new clipClassName() 
    before all of your making stuff.


    So, you COULD use:
    PHP Code:
    for(i=0;i<10;i++){
    var 
    instanceName1:clipClassName = new clipClassName();
    addChild(instanceName1)
    yourArray.push(instanceName1)

    EDIT: As you can see, you don't need to use variable i to name them, because they are saved in order in the array. You could use "yourArray.unshift()" to add it to the beginning if you need to.
    Last edited by Multihunter; 09-05-2010 at 06:10 AM.

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Exactly. Naming schemes were and are a terrible substitute for an actual collection. Use Arrays or Vectors.
    A general rule of thumb is that any time you want to name something "something"+i, you want an array of somethings instead.

  10. #10
    Junior Member
    Join Date
    Dec 2002
    Posts
    29
    Yes, I did eventally figure out how I can make an array and then use a function to create an object and retutn the object to a location in the array.

    Which in the end, as you are all saying, is better overall.

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