Flash actionscript notes

h1. http://tv.adobe.com/show/actionscript-11-with-doug-winnie/

Creating Instances

Turn object into a movie clip. (Convert to symbol)
Make sure to check, "export to actionscript" and to give it a name
If you have an instance of the object on the stage you have to give that a name to access it from actionscript

trace("Hello, World");  // prints to output, similar to debug console in JS

var myCircle = new Circle(); // makes a new instance, but it won't appear on the stage yet
addChild(myCircle); // this adds it to the display stack

____

Functions

function runMe():void{
 trace("yo");
}

function reposition(newX:Number,myName:String):String{
 myObject.x = newX;
 return("Hello " + myName);
}

________

Events

myObject.addEventListener(MouseEvent.MOUSE_DOWN,callbackFunction);
 function callbackFunction(e:MouseEvent):void {
 trace("my object was clicked");
}

________

Timers

var myTimer = new Timer(1000,3);
myTimer.addEventListener(TimerEvent.TIMER, tickTock);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerFinished);
myTimer.start();
function tickTock(e:TimerEvent):void{
 trace("this gets outputted once a second for three times")
}
function timerFinished(e:TimerEvent):void {
 trace("Timer is Finished");
}

________

The Timeline (frame scripts)

stop();
// below, myObject has its own timeline
myObject.gotoAndStop(10);

----------

The Event Object

// attach a listener to everthing on the stage
addEventListener(MouseEvent.MOUSE_OVER, hover);
// in the function below, e.target is how you access the object that the event was called on
function hover(e.MouseEvent){
 e.target.alpha = .5;
 trace("Now over " + e.target.name");
}

_________

Drag and Drop

myObject.addEventListener(MouseEvent.MOUSE_DOWN, drag);
myObject.addEventListener(MouseEvent.MOUSE_up, drop);

function drag(e.MouseEvent):void{
myObject.startDrag();
}

function drop(e.MouseEvent):void{
myObject.stopDrag();
}

______

Math Functions

var num:Number;
num = Math.floor(5.8);
// num == 5
num = Math.ceil(5.2);
// num == 6
num = Math.round(5.5);
// num == 6
num = Math.ceil(Math.random() * 6);
// num is now equal to a number 1 through 6

______

Animation Basics

/*
 * move an object in a straight line, once every second for ten seconds
 */
// create a timer that broadcasts every second for 10 seconds
var animateTimer:Timer = new Timer(1000,10);

var slopeX = 10;
var slopeY = 15;

animateTimer.addEventListener(TimerEvent.TIMER, moveDot);
function moveDot(e.TimerEvent):void {
 myObject.x += slopeX;
 myObject.y += slopeY;
}

animateTimer.start();

-------

Loops

for(var i:Number = 0; 1 < 10; i++){
 trace(i);
}

-------

Arrays

var myArray:Array = ["Sarajune","Jeff"];
trace(myArray[0]);
// outputs Sarajune
myArray.push("Tim");
trace(myArray[2]);
// outputs Tim
myArray.splice(2,1);
// removes one item starting at position 2
for (var i:Number = 0; i < myArray.length; i++){
 trace(i + ": " + myArray[i]);
}

____

Create external .as file

* In the properties panel, give the fla a Class and click the edit button (button with pencil icon)
* After clicking the edit button, it will show you an editor that automatically puts in code that makes the package, imports the MovieClip object, and creates the constructor function. Save as myProject.as

____

Getters

public class Die extends MovieClip {

 // private variables often start with an underscore
 // any variable used in a class must be declared before the constructor
 private var _dieValue:uint;

 public function Die() {
  stop();
 }

 public function rollDie():void {
  _dieValue = Math.ceil(Math.random()*6);
  this.gotoAndStop(_dieValue);
 }

 public function get dieValue():uint{
  return _dieValue;
 }

}

public class DiceOut extends MovieClip {

 // declare all class variable before constructor
 var rollButton:GameButton;
 var die1:Die;

 public function DiceOut() {
  rollButton = new gameButton();
  die1 = new Die();
  
  addChild(rollButton);
  addChild(die1);
  
  rollButton.x = 25;
  rollButton.y = 25;
  
  die1.x = 250;
  die1.y = 25;
 
  createListeners();
 }
 
 private function createListeners():void {
  rollButton.addEventListener(MouseEvent.CLICK, buttonClick);  
 }
 
 public function buttonClick(e.MouseEvent):void {
  die1.rollDie();
  // die1 is an instance of Die
  // we access the private variable by using the dot syntax without parenthesis
  //  that runs the get function dieValue
  trace(die1.dieValue);
 }

}

Article Type

General