|
-
Senior Member
[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
-
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?
-
Senior Member
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.
-
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.
-
Senior Member
Could you show an example? Thanks.
-
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?
-
Senior Member
recursive function as well please, thanks.
-
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.
-
Senior Member
Thanks. I really appreciate it.
-
I just noticed I missed a small but important part. Look at the edited code above.
-
Senior Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|