|
-
timed addChild
I want to loop through an array and add each item to the stage in 3 second intervals
When I loop through the array I dump everything on the stage at once. This is not what I want to do - I want to stagger the adding to stage bit.
anyone???
-
Total Universe Mod
A for loop might imply that it happens over time but its actually just one statement like any other if statement, switch statement etc. It completes all at once before the next statement is even interpreted.
What you want is an interval. setInterval allows you to set a delay, a function to call after each delay and if you want, parameters to pass to that function. I rarely need to pass parameters.
setInterval(myFunction, 3000); // the delay is in milliseconds. 1000 milliseconds = 1 second
You should always record your setInterval to a variable so that you can stop it later.
var myInterval = setInterval();
Actionscript Code:
//So if you want to count while placing new children, you'll have to keep the count separate var myCount:Number = 0;
//and the function to call
function addInstance():void { if(myCount < 30){ var myChild:MovieClip = new MyMovieClip(); addChild(myChild); myCount++; }else{ clearInterval(myInterval); // this is important to stop your interval when done. } }
// now start the interval var myInterval = setInterval(addInstance, 3000);
-
Thank You!
This was a huge frustration... now alleviated 
Thank You!
-
Consider using a Timer rather than setInterval/clearInterval. With a Timer, you can set both the delay and the number of times to repeat, plus it's a little more in the Object/Class oriented style. Even the livedocs for setInterval say to consider Timer instead.
-
Total Universe Mod
I didn't want to confuse him with event handlers and their params but yes, Timer is better.
-
Her :)
and again thanks to you both. It's all great info.
-
Actionscript Code:
var myChild:MovieClip = new MyMovieClip(); var objectsArray:Array = new Array; objectsArray=this.objectsArray;
for (var i:int=0; i<myArray.length;i++){ for (var t:int=0; t<30;t++){ //Make sure the 30 is what ever your fla's fps is so if your at 60 change this to 60 trace("Timer:",t); } addChild(myChild); trace("OBJECT ADDED"); objectsArray.push=myChild;
}
off the top of my head i think would work...
-
No, that won't work. The inner loop will not take anywhere near 1 second to finish, and even if it did, that would be a blocking operation and the entire swf would be unresponsive while it did that. Including redrawing the display. Basically, you wouldn't see anything until the end even if the inner loop took the right amount of time to run.
Also, when creating a new instance of a class, I strongly suggest using the () to indicate that you are calling the constructor, even though the compiler lets you get away with not using that in the case of a no-argument constructor. But, when creating a new Array, use the empty array literal [] instead, as it's faster to execute, faster to type, and in my opinion easier to read.
The line where you set objectsArray back equal to itself does nothing.
You can only add a DisplayObject to the display in one place. Adding the same myChild instance several times is useless. Each subsequent add will remove it from where it was before. But in this case that's the same place, making that entirely useless. You'd need to add a different instance to get multiple things on the display.
push is a method of Array, and must be called as a method. You cannot assign an element to push.
-
whoops this was backwards
this.objectsArray=objectsArray;
and this needed to be...
objectsArray.push(myChild);
the line where you set objectsArray back equal to itself does nothing.
then why did you teach me to do that in another post...
You can only add a DisplayObject to the display in one place. Adding the same myChild instance several times is useless. Each subsequent add will remove it from where it was before. But in this case that's the same place, making that entirely useless. You'd need to add a different instance to get multiple things on the display.
really then how come I can add multiple ones and track independent hits to them? Im confused now...
Listen to flax though he knows way better then I do! Im just trying to understand myself...
-
Code:
this.something = something;
That can do something. Usually you would not need to do that. What it does is assign the value of the variable something in the local scope to a property of the current object. If that line is inside a function, the value of something within that function may differ from the property of the same name on the object. I probably told you to do that so that you could put something as a dynamic property of a MovieClip and access it across multiple frames rather than leave it as a variable local to one frame.
I don't know what you're actually doing when you think you're putting the same instance on the display more than once. You're probably getting different instances somehow.
-
ok cool, sorry if I make you explain stuff several times...
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
|