Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

 
« Return

ActionScript 3

Timer Event

A timer event calls the function of your choice every time the specified amount of time has elapsed.

Download the FLA file

var horizontalSpeed:int = 10;
var verticalSpeed:int = 8;

var ballRadius:Number = ball_mc.width / 2;

var myTimer:Timer = new Timer(20);
myTimer.addEventListener(TimerEvent.TIMER, moveBall);
myTimer.start();

function moveBall(myEvent:Event):void
{
	if (ball_mc.x + ballRadius > stage.stageWidth)
	{
		horizontalSpeed *= -1;
	}
	else if (ball_mc.x - ballRadius < 0)
	{
		horizontalSpeed *= -1;
	}
	
	if (ball_mc.y + ballRadius > stage.stageHeight)
	{
		verticalSpeed *= -1;
	}
	else if (ball_mc.y - ballRadius < 0)
	{
		verticalSpeed *= -1;
	}	
	
	ball_mc.x += horizontalSpeed;
	ball_mc.y += verticalSpeed;	
}


 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM