Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

SPONSORED BY

The Very Best Algebra Notes


The Very Best Algebra Notes for the Very Best Flash Games

 

FLASH ACTIONSCRIPT TIMER CODE LESSON

In this lesson, students learn how to code a timer for a stopwatch application. The AS3 API object, methods and properties used in the lesson include the TextField, addChild, TextFormat, Timer, TimerEvent, addEventListener, and function..

In the program the timer starts after the green button is clicked. The textfield displays the time elapsed. When the red button is clicked the timer stops.

To schedule an online Flash lesson, contact mark.c.stansberry@gmail.com, Text: (707)235-4095

 

 

 

 

 

 

 

 

 

 

 

COPY AND PASTE TO DOWNLOAD CODE

var stopB:Sprite = new Sprite()
stopB.graphics.beginFill(0xFF0000,1);
stopB.graphics.drawRect(250, 200, 50, 50);
stopB.graphics.endFill();

addChild(stopB)
var stopwatch1: Sprite = new Sprite()
stopwatch1.graphics.beginFill(0x00FF00,1);
stopwatch1.graphics.drawRect(250, 250, 50, 50);
stopwatch1.graphics.endFill();
addChild(stopwatch1)

var adText:TextField = new TextField();
adText.x = 150
adText.y = 200
adText.width = 40;
//tipText.height = 20;
adText.multiline = true;
adText.wordWrap = true;
adText.autoSize = TextFieldAutoSize.LEFT

var adText1:TextField = new TextField();
adText1.text = "Click green to start timer, red to stop"
adText1.x = 300
adText1.y = 250
adText1.width = 100
adText1.multiline = true
adText1.wordWrap = true;
adText1.autoSize = TextFieldAutoSize.LEFT
//adText.selectable = true
//adText.background = true
addChild(adText1)

adText.border = false

var myFormat:TextFormat = new TextFormat();
myFormat.size = 30;
adText.defaultTextFormat = myFormat;

//TIMER1 CODE
var i: int = 0
var myTimer:Timer = new Timer(1000, 20);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void{
i = i +1
//trace(i, "seconds");
adText.text = i.toString();
/*var w:Number
w = i/10
adText1.text = w.toString();
*/}
myTimer.start();

 

var myFormat1:TextFormat = new TextFormat();
myFormat1.size = 30;
adText1.defaultTextFormat = myFormat1;

var w1: Number = 10
var w: Number = 0
var myTimer1:Timer = new Timer(100, 200);
myTimer1.addEventListener(TimerEvent.TIMER, timerListener1);
function timerListener1 (e:TimerEvent):void{
w = w +1
w1 = w/10
//trace(i, "seconds");
adText1.text = w1.toString();
/*var w:Number
w = i/10
adText1.text = w.toString();
*/}
//myTimer1.start();

//myTimer1.start()

stopB.addEventListener(MouseEvent.CLICK, timer_stop_fn)
function timer_stop_fn(e: MouseEvent): void
{

myTimer1.stop()
myTimer1.reset()

}

stopwatch1.addEventListener(MouseEvent.CLICK, timer_start_fn)
function timer_start_fn(e: MouseEvent): void
{


myTimer1.start()

}

You are at:
http://www.3d-perspective-software-development.com/lesson-flash-as3-timer-code-lesson.html