;

PDA

Click to See Complete Forum and Search --> : [RESOLVED] Loop brain fart


jweeks123
07-24-2008, 03:12 PM
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.


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:


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:


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


Any ideas? Thanks

5TonsOfFlax
07-24-2008, 03:57 PM
You could do this:

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?

jweeks123
07-24-2008, 04:23 PM
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.

5TonsOfFlax
07-24-2008, 04:27 PM
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.

jweeks123
07-24-2008, 04:38 PM
Could you show an example? Thanks.

5TonsOfFlax
07-24-2008, 04:42 PM
//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?

jweeks123
07-24-2008, 04:45 PM
recursive function as well please, thanks.

5TonsOfFlax
07-24-2008, 04:49 PM
//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.

jweeks123
07-24-2008, 04:53 PM
Thanks. I really appreciate it.

5TonsOfFlax
07-24-2008, 04:55 PM
I just noticed I missed a small but important part. Look at the edited code above.

jweeks123
07-24-2008, 04:57 PM
Cool, thanks.