Archive

Archive for the ‘ActionScript 3’ Category

Writing simple code in AS3

I have been learning AS3 at my work and it is starting to grow on me. I started to enjoy it once I learned OOP (Object Oriented Processing). The simple idea to give each object its own mind and it can interact with any other object is fantastic.

We use Eclipse FDT for all of our class writing. It is a useful and powerful actionscript class-writing software. You can download a free version here (there are limitations with the free version, but its not so bad). http://fdt.powerflasher.com/

So in this post I will be showing simple as3 codes that I have been learning.  A extremely good idea for anyone is to have a digital notebook. It helps the learning process go so much faster and you also have many more options at your disposal.

Here are a few most important things in my notebook:

//Date and Time in AS3,

//You can make a expiration date or change the style of a site with the date of the week. It is always up to you.


addEventListener(Event.ENTER_FRAME, eFrame);

//We add these arrays because the online clock only gives us the number of the day or month.   //Example: 0 = Sunday. The code would spit out 0 and we replace it with the array string.

var days:Array = [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”];

var months:Array = [“January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December”];

function eFrame(evt:Event):void{
var current:Date = new Date();

var sec:int = current.getSeconds();
var min:int = current.getMinutes();
var hours:int = current.getHours();

//This code makes sure the numbers always look normal. Example: 12:20 not 12:2

if(sec < 10){
var s:String = “0” + sec;
}else{
s = “” + sec;
}

if(min < 10){
var m:String = “0” + min;
}else{
m = “” + min;
}

if(hours < 10){
var h:String = “0” + hours;
}else{
h = “” + hours;
}

txt2.text = “The current time is ” + h + “:” + m + “:” + s;

txt.text = “Today is ” + days[current.getUTCDay()] + ” the ” + current.getDate() + “th of ” +
months[current.month];

}

//Creating a Timer in AS3

var timer:Timer;

//1000 is the number count in milliseconds
timer = new Timer(1000, 0);
timer.addEventListener(TimerEvent.TIMER, onTime);
timer.start();

private function onTime(e:TimerEvent) : void {
trace(“Timer is at ” + timer.currentCount);
}

//Dispatching Events in AS3

//These are very important when working with multiple classes. It is this simple.

//send directly to the MovieClip that you want to catch the event. I used (parent as MovieClip).
(parent as MovieClip).dispatchEvent(new Event("fadeOthers"));

//the catcher is a simple one
addEventListener(“fadeOthers”, newFunction);

private var newFunction(e:Event):void{
trace(“caught”);
}

That is all for today. I will be continuing on to part 2 tomorrow. Thanks for reading. 

Scott.