<< Chapter < Page | Chapter >> Page > |
The TIMER event handler
The TIMER event handler is shown in Listing 15. Except for the method signature, this is essentially the same code that you saw in the ENTER_FRAME event handler in Listing 4.
public function onTimer(event:TimerEvent):void {
x += dx;y += dy;
}//end onTimer
That's a wrap
I'm going to let that be it for this lesson, which has concentrated on time bases and animation for ActionScript 3 projects.
I encourage you to run these projects from the web. Then copy the code from Listing 16 through Listing 21. Use that code tocreate your own projects. Compile and run the projects. Experiment with the code, making changes, and observing the results of your changes. Makecertain that you can explain why your changes behave as they do.
I will publish a list containing links to ActionScript resources as a separate document. Search for ActionScriptResources in the Connexions search box.
Complete listings of the projects discussed in this lesson are provided below.
/*Project TimeBase01
The purpose of this project is to experiment with the useof Event.ENTER_FRAME as a time base. The project computes
and displays the average frame rate over five consecutivesets of 200 frames. It also shows how to set the frame
rate to something other than the default value.Must be run in debug mode to display the text data.
*********************************************************/package {
import flash.display.Sprite;import flash.events.Event;public class Main extends Sprite{
private var sprite:Sprite;private var date:Date;
private var countA:uint = 0;private var baseTime:Number = 0;
private var currentTime:Number = 0;public function Main():void {sprite = new Sprite();
addEventListener(Event.ENTER_FRAME, onEnterFrame);// stage.frameRate = 10;
}//end constructor//--------------------------------------------------////Event handler.
public function onEnterFrame(event:Event):void {currentTime = new Date().time;
if (countA == 0) {baseTime = currentTime;
trace(baseTime);}//end ifif ((countA>0)&&(countA<1001)&&(countA % 200 == 0)){
trace(currentTime + " " + countA + " "+ 1000 / ((currentTime - baseTime) / 200));
baseTime = currentTime;}//end if
countA++;}//end onEnterFrame
}//end class}//end package
/*Project TimeBase02
The purpose of this program is to experiment with the useof a Timer object as a time base. The program computes
and displays the average tick rate of the Timer objectover five consecutive sets of 100 frames . It also shows
how to set the tick rate to a specific value and how tospecify the number of ticks.
*********************************************************/package {
import flash.display.Sprite;import flash.events.TimerEvent;
import flash.utils.Timer;public class Main extends Sprite{private var sprite:Sprite;
private var timer:Timer = new Timer(1000/10,1001);private var date:Date;
private var countB:uint = 0;private var baseTime:Number = 0;
private var currentTime:Number = 0;public function Main():void {sprite = new Sprite();
timer.addEventListener(TimerEvent.TIMER, onTimer);timer.start();// stage.frameRate = 10;
}//end constructor//--------------------------------------------------//
//Event handlerpublic function onTimer(event:TimerEvent):void {
currentTime = new Date().time;if (countB == 0) {
baseTime = currentTime;trace(baseTime);
}//end ifif((countB>0)&&(countB<1001)&&(countB % 200 == 0)){trace(currentTime + " " + countB + " "
+ 1000 / ((currentTime - baseTime) / 200));baseTime = currentTime;
}//end ifcountB++;
}//end onTimer//--------------------------------------------------//
}//end class}//end package
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?