Friday, September 10th, 2010
Actionscript Basics

Removing event listeners in Actionscript

var timer:Timer = new Timer(1000); timer.addEventListener(TimerEvent.TIMER, onTimer); timer.start(); function onTimer(evt:TimerEvent):void { watch.hand.rotation +=5; if (watch.hand.rotation >= 25) { timer.removeEventListener(TimerEvent. TIMER, onTimer); } }

More Actionscript Basics

Actionscript timer event

var timer:Timer = new Timer(1000); timer.addEventListener(TimerEvent.TIMER, onTimer); timer.start(); function onTimer(evt:TimerEvent):void { watch.hand.rotation +=5; }

Using Keyboard Events to Call Methods in Actionscript

function onKeyPressed(evt:KeyboardEvent):void { switch (evt.keyCode) { case Keyboard.ENTER: box.play(); break; case Keyboard.BACKSPACE: box.stop(); break; case Keyboard.LEFT: box.prevFrame(); break; case Keyboard.RIGHT: box.nextFrame(); break; case Keyboard.SPACE: box.gotoAndStop(3); break; default: trace(“keyCode:”, evt.keyCode); } };

Actionscript multiple events

myMovieClip.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag); myMovieClip.addEventListener(MouseEvent.MOUSE_UP, onStopDrag); function onStartDrag(evt:MouseEvent):void { evt.target.startDrag(); } function onStopDrag(evt:MouseEvent):void { evt.target.stopDrag(); }

Actionscript addEventListener() function

The addEventListener() method is used to identify the event, and assign the function to be executed to the object that is ...

Actionscript movie clip properties

Description: Location Property: x, y Syntax for Setting Value:box.x = 100; box.y = 100;Units and/or Range: pixelsDescription: Scale (1) Property: scaleX, scaleY Syntax for Setting Value:box.scaleX ...

Actionscript passing objects to function as parameter

function showPlaneStatus(obj:Object):void { trace(obj.pitch); trace(obj.roll); trace(obj.yaw); }; showPlaneStatus(plane); //0 //5 //5

Actionscript simple objects

var plane:Object = new Object(); plane.pitch = 0; plane.roll = 5; plane.yaw = 5;trace(plane.pitch); //0

Actionscript functions with parameters

function celToFar(cel:Number):Number { return (9/5)*cel + 32; } trace(celToFar(20)); //68 function farToCel(far:Number):Number { return (5/9)*(far - 32); } var celDeg:Number = farToCel(68)); trace(celDeg); //20

Actionscript functions example

function showMsg(){ trace("hello"); } showMsg(); //hello

Actionscript arrays

var myArray:Array = new Array(); myArray.push(1); trace(myArray) // 1 appears in the Output panel myArray.push(2); // the array now has two items: 1, 2 trace(myArray.pop()); // the pop() ...

Actionscript while loop

var num:Number = 0; while (num < .5) { num = Math.random(); }

Actionscript for loop

for (var i:Number = 0; i < 3; i++) { trace("hello"); }

Actionscript switch statement

switch (a) { case 1 : trace("one"); break; case 2 : trace("two"); break; case 3 : trace("three"); break; default : trace("other"); break; }

Actionscript if else example

if (a == 1) { trace("option a"); } if (b == "hello") { trace("option b"); } else { trace("option other"); }

Actionscript if condition example

var a:Number = 1; var b:String = "hello"; var c:Boolean = false; if (a == 1) { trace("option a"); }

eXTReMe Tracker