ActionScript 3.0 – Using the Date Object

Basically the Date object hasn’t changed in AS 3.0 but it’s still the most effective way to get and time items related to the date. In this example the skull moves once every second and when it reaches the end of the stage then a minute has passed.  the red overlay moves every minute, covering the screen in an hour.

In general if you want to move movie clips based on the current time like this then this is the formula you’d use:

var myTimer:Timer=new Timer(1000);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, moveFlyingSkull);
function moveFlyingSkull(myevent:TimerEvent):void {
var dt:Date = new Date();
var mySeconds = dt.getSeconds();
trace(”Current second: ” + mySeconds);
//divide by 60 to get a percentage
mySeconds = mySeconds /60;
//Multiply by the width of the stage (770)
mySeconds = mySeconds*770;
//Move the skull that many pixels
wingedSkull_mc.x = mySeconds;
//Red color
var myMinutes = dt.getMinutes();
//divide by 60 to get a percentage
myMinutes = myMinutes /60;
//Scale it up that percentage (100% covers the stage)
red_mc.scaleX = myMinutes;
}

Download Source Files

error

Enjoy this blog? Please spread the word :)