Recycling identifiers?
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Recycling identifiers?

  1. #1
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    335

    Recycling identifiers?

    I am being a bit puzzled on figure out how to release temporary identifiers to recycle them.

    For instances, suppose that I have 2 loops to instantiate dynamic mcs from library like this:

    Code:
    var zz:Number;
    
    for (zz=0;zz<=4;zz++)
    {
    	var _mc:MovieClip = new clsSquare();
    	_mc.name = "mcSquare"+zz;
    	addChild(_mc);
    }
    
    for (zz=0;zz<=4;zz++)
    {
    	var _mc:MovieClip = new clsBall();
    	_mc.name = "mcBall"+zz;
    	addChild(_mc);
    }
    If I run this snippet of code, the compiler will complain and tell that I have a "Duplicate variable definition".

    I cannot understand why it doesnt complain when it is redefined five times within the first loop, and just complain because the second lop.

    Anyway, there is a way to release the _mc identifier then I can reuse it? I am an addicted to standartize names and recycle code...


    Thanks!
    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    	_mc.name = "mcSquare"+zz;
    No! Bad! Use an array for a collection of objects. Sequential names are an extremely poor substitute, which will cause you all kinds of problems.

    To answer your question, the compiler is complaining because you are redeclaring the variable, not because you're re-using it. You don't have to do anything special to re-use it, just assign a new value like you did in the first loop. To validly declare two variables with the same name, they have to be in different scopes. But that isn't what you want anyway.

    Here's the solution you think you want:
    Code:
    var zz:int;
    var _mc:MovieClip;
    
    for (zz=0;zz<=4;zz++){
    	_mc = new clsSquare();
    	_mc.name = "mcSquare"+zz;
    	addChild(_mc);
    }
    
    for (zz=0;zz<=4;zz++){
    	_mc = new clsBall();
    	_mc.name = "mcBall"+zz;
    	addChild(_mc);
    }
    And here's the solution you really want.
    Code:
    var squares:Array = [];
    var balls:Array = [];
    
    for (var zz:int=0;zz<=4;zz++){
            squares.push(addChild(new clsSquare()));
            balls.push(addChild(new clsBall()));
    }
    This will stack them in a different order, so if it's important that all balls appear on top of all squares, return to two separate loops.

  3. #3
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    335
    Hi, 5Tons.

    Thanks for clearification!

    Actually the solution I want would be a mix of the both ones:

    Code:
    var zz:int;
    var squares:Array = [];
    var balls:Array = [];
    
    
    for (zz=0;zz<=4;zz++)
            squares.push(addChild(new clsSquare()));
    
    for (zz=0;zz<=4;zz++)
            balls.push(addChild(new clsBall()));


    However, I am still puzzled why I need to have arrays to store the mc names... I have used sequential naming for years in AS2 and it have been proven to be an efficient solution. Since mostly of times the mcs have autonomy (all the necessary events are tied to them) so I just need to ensure that they will have different names.

    In all of this time I never had any single problem with sequential naming. What you would point as a potential issue for this technique?
    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You don't need to use arrays to store your collections, it's just a lot more efficient and convenient. If you use a naming scheme, every time you access an object, you have to build the string representing the name, and then look up that object by name in its parent. Both of these are relatively expensive operations compared to array access. It is also conceptually far less direct to go through names than arrays.

    You are not storing the mc names in your array. You are storing the actual MovieClips. This means that if you want the 3rd square, the computer just has to get the 3rd entry in the array (a constant time lookup) and dereference it.

    As an analogy, let's say you have a messy room full of your favorite books (I do). You want to let your friend borrow the third book in the Dark Tower series. You could just tell him that, and he'll go search through the room looking at every book until he finds it. Or you could organize that room and perhaps have a section just for Stephen King. It's a lot faster and easier for your friend to find the book he wants if you can tell him that it's the 10th book on the Stephen King shelf.

    The code you posted last is what I intended by
    if it's important that all balls appear on top of all squares, return to two separate loops.

  5. #5
    flashguy
    Join Date
    May 2005
    Location
    In the mountains
    Posts
    335
    I got the point now... It is a matter of CPU usage cost...

    Thanks!
    Visit my business at http://www.ballooncreator.com - Software Tool For Party Balloons Online Design!

  6. #6
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,131
    Also, suppose you wanted two of those MC's to change positions, or to remove one, or add one to the beginning or the end, without having to rename all of them...

    I have always been puzzled, 5Tons, at the fact that the compiler doesn't throw an error when you re-declare a local variable inside a loop... or in a statement like for(var i:int=0;i<10;i++), for that matter. It seems inconsistent, because it is being re-declared withing the same scope, isn't it? (Or do loops create a subroutine scope and pass their final variables out to the class when they complete?)

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

Poll by Flashkit.com