I have a simple countdown timer, after which I would like to automatically go to a specific scene after the timer times out.
I am pretty new to AS3.
I have tried the gotoANDPlay ("Scene 1", 1); after the code, but it doesn't seem to work.
Any suggestions would be appreciated.
Here is my timer code:
var timer:Timer = new Timer(1000, 21);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.start();
function countdown(event:TimerEvent) {
cooling1.text = String(21 - timer.currentCount);
cooling2.text = String(21 - timer.currentCount);
cooling3.text = String(21 - timer.currentCount);
}
What do I put here to automatically send me another scene?
Thanks,
Bob
Thanks Ned, but I tried that syntax also, but nothing happens. The countdown timer times out and stays in its scene (3).
var timer:Timer = new Timer(1000, 21);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.start();
function countdown(event:TimerEvent) {
cooling1.text = String(21 - timer.currentCount);
cooling2.text = String(21 - timer.currentCount);
cooling3.text = String(21 - timer.currentCount);
}
gotoAndPlay(1, "Scene 1");
That gotoAndPlay command will execute almost immediately, having no relationship to the countdown. So if that line is in frame 1 of the main timeline, it executes immediately when you start running the file and simply puts you where you already are. It needs to be conained within some function in order to have it happen at some later point in time when the function is called.
If you want that code to execute after the timer has timed out its 21 counts, then you will need to have an event listener for the TIMER_COMPLETE event, and inside that listeners event handler function is where you want to put that gotoAndPlay command.
But the function only needs to contain the one line: gotoAndPlay(1, "Scene 1");
I think you need to learn something more basic than something specific to Timer events. There are event listeners and event handler functions for those listeners. You have already used one set of them in the code you showed...
// this is the listener
timer.addEventListener(TimerEvent.TIMER, countdown);
// this is the event handler function for that listener
function countdown(event:TimerEvent):void {
// actions to take when event occurs
}
You should be able to use the above as the basis for creating another listener/handler set. What will the TIMER_COMPLETE listener look like? What will the handler function look like for the gotoAndPlay command that you need to trigger when the TIMER_COMPLETE event occurs?
You are correct. I need a LOT of learning on actionscript. I just started. That's why I am hunting the internet for examples, to help me understand the syntax.
I never meant for you to put yourself out on my behalf, but I appreciate your help. I have been getting by on code snippets up til now, and the Adobe Flash CS5 books I have are not specific enough.
You aren't putting me out. Moreso I am putting you out. Others that help around here would have handed you the code a few postings back, but I prefer to try to help people solve things rather than hand them solutions. I wouldn't be surprised if one of them jumpps in
If you can gain the simple concept of an event listener and its handler, you will have gained something that applies throughtout Actionscript and is used everywhere. The way they are implemented is the same across the board. You create a listener and you create its handler function... the event when it occurs causes the handler function to execute whatever code it contains.
In the code you showed, a handler function was created named countdown. THe event listener for it is listeneing for a TIMER event. The function could have been named anything you like. The name of the function has to be unique though, so any new one you create for another purpose needs a different name.
Now you need to create another listener for the TIMER_COMPLETE event for that Timer object that you have. So create the new event listener by modeling it after the one you have, just specify the new event type and a new function for it to call... then create the new event handler function for it too, again modeling it after the one you have already, but put the new code inside it that you want it to execute.
Well, being this my first ever attempt at actionscript 3, my problem was that I didn't know exactly "where" to place the event listener for Timer_Complete in the sequence. I was putting it after the timer code initially.
After some trial and error and playing with some relevant examples I found on Google, this is what I lucked into that made the code work:
stop ()
var timer:Timer = new Timer(1000, 21);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
timer.start();
function countdown(event:TimerEvent) {
cooling1.text = String(21 - timer.currentCount);
cooling2.text = String(21 - timer.currentCount);
cooling3.text = String(21 - timer.currentCount);
}
function onTimerComplete(e:TimerEvent):void
{
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
gotoAndPlay(1,"Scene 1");
}
So my first mystery is solved, and I am sure I will have many more. ![]()
Thank you all for assistance.
Bob
North America
Europe, Middle East and Africa
Asia Pacific