Flash AS3 timer event, start and stop timers. [EN]

This is a short little code to start a timer in flash and after a click (or after the time is up) the timer is stopped and you are taken to another frame in the document.

This is the code i used for this example:

stop();
// the number of seconds you have the time to answer
var count:Number = 10;
// the timer
var myTimer:Timer = new Timer(1000,count);
// start the listener for the timer
myTimer.addEventListener(TimerEvent.TIMER, countdown);
// and start counting down
myTimer.start();
 
function countdown(event:TimerEvent):void
{
	// fill the text block with the time left
	myText_txt.text = String((count)-myTimer.currentCount);
	// if the time is 0 go to frame nr 4 and stop the timer and remove the listener
	if (myText_txt.text == "0")
	{
		myTimer.removeEventListener(TimerEvent.TIMER, countdown);
		myTimer.stop();
		gotoAndStop(4);
	}
}
// the buttons, wich take you to their respective frames and stop the timer
// and remove the listener
Fout_btn.addEventListener(MouseEvent.CLICK, ToFrame3);
 
function ToFrame3(evt:MouseEvent)
{
	myTimer.removeEventListener(TimerEvent.TIMER, countdown);
	myTimer.stop();
	gotoAndStop(3);
}
 
Goed_btn.addEventListener(MouseEvent.CLICK, ToFrame2);
 
function ToFrame2(evt:MouseEvent)
{
	myTimer.removeEventListener(TimerEvent.TIMER, countdown);
	myTimer.stop();
	gotoAndStop(2);
}

Leave a Reply

Your email address will not be published. Required fields are marked *