A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [RESOLVED] Loop brain fart

  1. #1
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124

    resolved [RESOLVED] Loop brain fart

    this is my loop. Basically I want to loop through every instance on the stage with the prefix of hotspot. I.E. - hotspot1, hotspot2, hotspot3, and so on.

    Anyway, I do this, and get a reference error when i is greater than the amount of hotspot movieclips.

    Code:
    for (var i:uint = 1; this["hotspot"+i]; i++) {
    	trace(i);
    	this["hotspot"+i].alpha = 0;
    	trace(this["hotspot"+i].alpha);
    }
    then I try this:

    Code:
    for (var i:uint = 1; i<this["hotspot"+i]; i++) {
    	trace(i);
    	this["hotspot"+i].alpha = 0;
    	trace(this["hotspot"+i].alpha);
    }
    and nothing happens.

    Then if I enter the total number of hotspot movieclips on stage it works, but I want it to count them, instead of me doing it. I.E. - this works:

    Code:
    for (var i:uint = 1; i<16; i++) {
    	trace(i);
    	this["hotspot"+i].alpha = 0;
    	trace(this["hotspot"+i].alpha);
    }
    Any ideas? Thanks

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You could do this:
    Code:
    for (var i:uint = 1; typeof(this["hotspot"+i]) != "undefined"; i++) {
    	trace(i);
    	this["hotspot"+i].alpha = 0;
    	trace(this["hotspot"+i].alpha);
    }
    By the way, you are not getting things named hotspotnumber on the stage with this code. You are accessing properties in the object. This is different because 1. The property won't necessarily be on the displaylist. 2. The property's value won't necessarily be any particular type. 3. There may be instances on the stage with an instancename of hotspotsomenumber that do not correspond to properties in the object.

    Since you don't know the number of hotspots at compile time, I'm assuming you put them up dynamically. If that's the case, why not store them in an array? Or are you trying to write code that will apply to a multitude of document classes that someone else may be creating?

  3. #3
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Thanks.

    I'm writing this so that anyone else can add as many or as few hotspots as they need. Normally this would be part of a class, but due to time constraints, I've had to do it in this manner.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I'd still suggest using getChildByName rather than this['hotspot'+i]. You're guaranteed that the thing you get back is a DisplayObject (and therefor has an alpha property), and also that it's actually on the stage. I'd further suggest that if this will be called repeatedly that you collect the hotspots in a proper array so that you don't have to go through the whole displayList over and over.

    Are the hotspots guaranteed to be direct children of the object calling this code? If not, you'll have to write a recursive function to collect them all.

  5. #5
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Could you show an example? Thanks.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    //setup
    var hotspots:Array = new Array();
    for (var i:uint = 0; i < numChildren; i++){
      if (getChildAt(i).name.indexOf("hotspot") == 0){
        hotspots.push(getChildAt(i));
      }
    }
    
    //alpha call.
    for (var i:uint = 0; hotspots.length; i++) {
    	hotspots[i].alpha = 0;
    	trace(hotspots[i].alpha);
    }
    Or did you mean example of the recursive function?

  7. #7
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    recursive function as well please, thanks.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    //setup
    var hotspots:Array = new Array();
    getAllHotspots(this, hotspots);
    
    function getAllHotspots(container:DisplayObjectContainer, hs:Array):void{
      var dobj:DisplayObject;
      for (var i:uint = 0; i < container.numChildren; i++){
        dobj = container.getChildAt(i);
        if (dobj.name.indexOf("hotspot") == 0){
          hs.push(dobj);
        }
        if (dobj is DisplayObjectContainer){
           getAllHotspots(dobj, hs);
        }
      }
    }
    
    //alpha call.
    for (var i:uint = 0; hotspots.length; i++) {
    	hotspots[i].alpha = 0;
    	trace(hotspots[i].alpha);
    }
    Edit: Missed a "container." in front of getChildAt.
    Last edited by 5TonsOfFlax; 07-24-2008 at 04:55 PM.

  9. #9
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Thanks. I really appreciate it.

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I just noticed I missed a small but important part. Look at the edited code above.

  11. #11
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    Cool, thanks.

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