AS3 spawn problem

Started by: Steyene | Replies: 4 | Views: 837

Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
May 13, 2009 11:04 AM #417788
I want to make a time thing based off my sig game that I had a while back. But I also want to do it in AS3, this is where my problem has hit. AS3 isn't doing what AS2, was. It executes everything at once, and as a result instead of getting one object spawning then another, they all spawn at once.

Code

trace("frame two");

var num_objects:int = 0;
var total_num_objects:int = 50;
var timer:int = 300;
var new_object = 0;

trace("num_objects", num_objects, "total_num_objects", total_num_objects, "timer", timer, "new_object", new_object);

while (num_objects != total_num_objects) {
timer = timer-1;
trace("timer", timer);
if (timer == new_object) {
trace("NEW OBJECT SPAWNS NOW");
timer = 300;
trace("timer", timer);
num_objects = num_objects +1;
var Ball:object = new object;
Ball.x = Math.random()*550;
Ball.y = Math.random()*400;
addChild(Ball);
}
}


Here is the .swf, or not seeing as CS3 hates me. Any ideas?
Scorpioxxx
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
May 13, 2009 3:16 PM #417893
Does increasing the timer effect it at all? Could be that the time is simply running to 0 very very fast for some reason. Apart from that I can't see why it shouldn't work, but then I'm working from start to finish, not doing it all at once.
Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
May 13, 2009 9:18 PM #418055
Nope, I mean I even put a empty for loop inside the nested If statement. I put it up to 1000000000000, and it still did it instantaneously, granted it took a while to actually work.
Polnareff
2

Posts: 17
Joined: Nov 2005
Rep: 10

View Profile
May 14, 2009 3:23 AM #418221
It's because an actual loop like that is meant to run at an incredibly fast speed. Essentially, you're running a loop at some instant in your code, and the condition is that it will keep running until you have 300 objects. During that time, the "timer" variable will just decrease reeeaaally fast until you're loop is done.

What you need to do is decrease the timer variable in the enterFrame event. That way it's change is based on the actual frames which have passed.

Now what I'm about to say is based on my knowledge of AS2, so I don't know quite how this is done in AS3. The best thing to really do here is have an interval. Essentially you make some function called "countDown" with a body
{ timer --; }

And then (in AS2) you'd do:
timerInterval = setInterval(1000, countDown);
(something like this, the two parameters might be switched, I don't remember)

Now countDown will be called every 1000 ms (or 1 second...you can change this to whatever you want), and you change what value you initialized "timer" to a new one.

The body of the countDown() function can also have all the other code you wrote previously, that way it'll check every time the counter goes down.

Also, remember to call clearInterval(timerInterval) when you want the timer to finally stop.
Steyene

Posts: 2,060
Joined: Apr 2006
Rep: 10

View Profile
May 14, 2009 10:44 PM #418453
Right, so instead of using a decreasing variable, use an actual counter. I'll try that. Thanks