In this tutorial we'll see how to work with the Date class of Actionscript 3.0.
It 'a very large class with many methods and properties so I made two good examples that we draw near to
solutions / applications that need to retrieve the data from the user's machine.
The first example works with time, more precisely with hours.
Retrieving the current time on the user's machine, according to the hours I can handle the action to my SWF.
In this case, the actions will be based on nightly or daily hours.
So I created a MovieClip on the stage with a tween shape between two images (one at night and one at day).
We will move the timeline of this MovieClip based on the current time.
The second example instead works on four seasons of the year.
So we'll move the timeline of a MovieClip based on the current date that we get by the user's machine.
Example 1 ( try to change the time of your computer )
The code I used + explanation comments:
Code:// Package declaration package { // Import the necessary classes import flash.display.*; import flash.text.*; import flash.events.*; // Declare the class that extends the MovieClip and assign it // as Document Class of FLA file public class Hours extends MovieClip { // Constant type int with value of hours that start the day private const DAY_START:int=7; // Constant type int with value of hours that finish the day private const DAY_END:int=19; // Variable type Date that will get the date from user's machine private var my_date:Date; // Contructor declaration public function Hours() { // Adding the ADDED_TO_STAGE event that calls the init function // ( this is useful if you want load this SWF into another without lose the root ) addEventListener(Event.ADDED_TO_STAGE,init); } private function init(evt:Event):void { // Remove the ADDED_TO_STAGE event removeEventListener(Event.ADDED_TO_STAGE,init); // Instance a new Date. // At this moment, Flash creates a new date based on user's machine date. my_date=new Date(); checkDate(); } private function checkDate():void { // Get the value of the hours ( 0-24 ) var now:Number=my_date.getHours(); // Check the hours if(now > DAY_START && now < DAY_END) // If the hours are greater than DAY_START and less than DAY_END, // so if it is a number between 7 and 19 // move the city_mc timeline to frame 2 city_mc.gotoAndPlay(2); else // If not, move the city_mc timeline to frame 32 city_mc.gotoAndPlay(32); } } }Example 2 ( try to change date of your computer )
The code I used + explanation comments:
Code:// Package declaration package { // Import the necessary classes import flash.display.*; import flash.text.*; import flash.events.*; // Declare the class that extends the MovieClip and assign it // as Document Class of FLA file public class Seasons extends MovieClip { // Declare the constants that represent the // 4 months of the beginning of each season private const SPRING:int=3; private const SUMMER:int=6; private const AUTUMN:int=9; private const WINTER:int=12; // Variabile type Date that will get the date from user's machine private var my_date:Date; // Dichiaro la funzione costruttrice public function Seasons() { // Adding the ADDED_TO_STAGE event that calls the init function // ( this is useful if you want load this SWF into another without lose the root ) addEventListener(Event.ADDED_TO_STAGE,init); } private function init(evt:Event):void { // Remove the ADDED_TO_STAGE event removeEventListener(Event.ADDED_TO_STAGE,init); // Instance a new Date. // At this moment, Flash creates a new date based on user's machine date. my_date=new Date(); checkDate(); } private function checkDate():void { // I get the month from my_date and add 1 because // months in Actionscript have a range of 0-11 var month:Number=my_date.getMonth()+1; // I get the day of the month ( from 1 to 31 ) var day:Number=my_date.getDate(); // Check month and day // If month is less than SPRING if(monthSPRING) { seasons_mc.gotoAndStop(1); } if(month==SUMMER) { if(day<21) seasons_mc.gotoAndStop(1); else seasons_mc.gotoAndStop(2); } if(month SUMMER) { seasons_mc.gotoAndStop(2); } if(month==AUTUMN) { if(day<21) seasons_mc.gotoAndStop(2); else seasons_mc.gotoAndStop(3); } if(month AUTUMN) { seasons_mc.gotoAndStop(3); } if(month==WINTER) { if(day<21) seasons_mc.gotoAndStop(3); else seasons_mc.gotoAndStop(4); } } } }
Bookmarks