/* ActionScript
to demo creating instances
*/
// create instances of BlueSquare
var myBlueSquare = new BlueSquare();
var myOtherBlueSquare = new BlueSquare();
trace(myBlueSquare);
addChild(myBlueSquare);
addChild(myOtherBlueSquare);
//reposition instance named myOtherBlueSquare
myOtherBlueSquare.x = 300;
myOtherBlueSquare.y = 150;
In the sample, above, "BlueSquare" is the name of the object
stop();
var blueButton = new pushbuttonblue();
addChild(blueButton);
blueButton.x = 200;
blueButton.y = 200;
blueButton.addEventListener(MouseEvent.MOUSE_DOWN,frFive);
function frFive(e:MouseEvent):void
{
gotoAndStop("five");
blueButton.removeEventListener(MouseEvent.MOUSE_DOWN,frFive);
removeChild(blueButton);
}
"five" = the frame label.
In this example, the button is removed from frame 5
/*first parameter is miliseconds the second is the amount of times the function will run*/
var myTimer = new Timer(1000,3);
myTimer.addEventListener(TimerEvent.TIMER, tickTock);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
myTimer.start();
function tickTock(e:TimerEvent):void {
trace("Tick");
}
function timerDone(e:TimerEvent):void {
trace("DONE");
}
Void mean undefined
Functions are called Methods
runMe();
function runMe():void {
trace("This is the runMe function");
}
runMe();
A simple sample
//calls function "createGreeting" with "You" as variable
trace( createGreeting("You") );
/*the :String defines what type of thing the return statement returns
the "return" exits out of the function*/
function createGreeting(firstName:String):String {
return ("Welcome " + firstName);
}
As stated in the notes (code above), return exits the function
//add instance of object
var square = new BrownSquare();
trace(square);
//position instance
square.x = 50;
square.y = 50;
addChild(square);
//reposition instance
function reposition(newX:Number,myName:String):void {
square.x = newX;
trace("Hello " + myName);
}
reposition(100,"Me");
+ Addition
- Subtraction
* Multiplication
/ Division
++ Increment
-- Decrement
trace (2+3);
trace (3-2);
trace (2*3);
trace (5/2);
//"%" shows the remainder of 5/2
trace (5%2);
// myValue is 2
var myValue:Number = 2;
//a new myValue = 2 + 2
myValue = myValue + 2;
trace(myValue);
//does the same thing as the above
var myOtherValue:Number = 2;
myOtherValue += 2;
trace(myOtherValue);
var newNumber:Number = 5;
trace(newNumber);
newNumber++;
trace(newNumber);
newNumber--;
trace(newNumber);
Events have 3 phases: Capture, Target, and Bubbbling.
// create instances of BlueSquare
var myBlueSquare = new BlueSquare();
trace(myBlueSquare);
addChild(myBlueSquare);
myBlueSquare.addEventListener(MouseEvent.MOUSE_DOWN,onClick);
function onClick(e:MouseEvent):void {
trace("The first blue square was clicked.");
}