FINAL
Properties Methods Events
Orielly
--------------------------
Next, we start off the ActionScript 3.0-specific material with a look at the three essential building blocks of most ActionScript objects: properties, methods, and events—the latter being one of the most significantly changed elements of ActionScript, with the introduction of version 3.0.
---------------------------
Orielly
--------------------------
Properties…describe the object being modified or queried.
For example, you can check or set the width of a button
Methods are a bit like verbs. They are used to tell objects to do something, such as play and stop.
navigateToURL()
Events are the catalysts that trigger the actions you write.
For instance, a user might click the mouse button, which would then result in a mouse event.
----------------------------------------
Properties
Orielly said that properties describe objects, and this is a good way to look at properties. If we look at it in the movie script sense, properties are just characteristics of the actors(variables or functions). Lets make an actor:
var fatMan:Object= new Object();
(For those of you who have played Metal Gear Solid 2)
Our actor is a newObject object (I know it sounds weird). Now, we want to make our variable fat. The scale of an object is one property that works automatically (it is built in,). You can also make your own properties, but not now:
fatMan.scaleX=2;
I am going to trace (fatMan.scaleX) to see if my movie works.
trace(fatMan.scaleX);
//This traces 2
So, the movie works. But fatMan is only stretched in the x direction. I will stretch him in the y direction too.
var fatMan:Object= new Object();
fatMan.scaleX=2;
fatMan.scaleY=2;
The scale property is written as a decimal percentage, so instead of going from 0% to 100% it goes from 0 to 1.0. So, fatMan is now twice his size.
Other properties are:
alpha--changes transparency (0-1)
rotation--rotates object (degrees)
visible--makes object visible or invisible (boolean)
x--changes objects location along x direction (pixels)
y--changes objects location along x direction (pixels)
width--changes the width of the object (in pixels)
height--changes height of the object (in pixels)
Here are how they are applied to a variable:
fatMan.alpha= 1;
fatMan.rotation= 45;
fatMan.visible= true;
fatMan.x=200;
fatMan.y=300;
fatMan.width= 50;
fatMan.height=50;
Again, these are built in properties, but you can create your own, not now.
Methods
Orielly
------------------------------
Like properties, methods appear consistently in the dot syntax that is the foundation of ActionScript, following the object calling the method.
------------------------------
Orielly said that Methods are like verbs. Using the scenario of a movie script, Methods are like stage directions:
stop();
play();
getURL();
gotoAndStop();
And there are more, many more!
Here is how they are applied:
fatMan.stop();
fatMan.play();
fatMan.gotoAndPlay();
fatMan.getURL();
It is like saying, “Hey, Joe, play the tape!” They tell the variables or functions to do something.
Don’t be afraid of this next bit of script, just SPOT THE METHODS.
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);
}
};
The methods are:
play();
stop();
prevFrame();
nextFrame();
gotoAndStop();
You may be thinking that “ENTER,BACKSPACE,LEFT,RIGHT,SPACE,keyCode” are methods also, but they are events. Rember that a verb is telling the variable to do something, even though ENTER sounds like a stage direction. But, you can use context clues (Keyboard.ENTER) to see that it is the keyboard key “ENTER.”
Any function can be used as a method.
Events and their Listeners and Handlers
Events trigger Methods to set Properties, just as A button triggers a switch to change a game character.
CONCEPT:
In the case of a movie and a director. When the director says, “Action,” The car starts the ignition and pushes the gas and turns the wheel which makes the car move along the preset path.
The major Event is the director’s words
The Method is to start the car push gas and turn wheel
The Properties of the car that change are its position, direction, acceleration, speed etc.
Another way to look at it is the event of the car driver pushing the gas, which starts the motor and engines to run, changing the position, acceleration, and speed of the vehicle.
However, in order for these events to be recognized, someone has to listen for them. There has to be a cue-man. The cue-man can be the director himself or some other person hired to cue the actors.
Even more than this someone is needed to handle the events. This means that someone must make sure all events and cues work the way the director wants them to work.
So, a variable (actor) may or may not have a cue-man(event listener)
A variable is given an event listener like this:
var joe: Object= new Object();
joe.addEventListener(MouseEvent.CLICK, onClick);
You can see that it seems the event listener acts like a property or a method, but if you think about it, it is like giving joe ears as well as telling him to do something (listen for…).
The things in the parenthesis are the things joe listens for and the thing he has to do.
Now, remember that play(); is a function, as well as stop();. And remember that these functions can be used as methods too. Well, the above event listener listens to the mouse which has a “CLICK” property (a feature of the mouse). Then what joe does is the function “onClick” which we have yet to create (remember that stop(); and play(); are pre-created functions).
function rotMove(event:MouseEvent):void
{
joe.y-=30;
joe.rotation+=25;
}
Joe performs his duty only as an event listener cues him to(like a method tells something what to do). The event listener listens for a mouse event. The mouse event it listens for is “CLICK”. Then the event listener cues a function named “onClick.” The rotMove function changes joe’s “y position” and “rotation.”
Remember, this function only happens when joe is clicked. The mouse triggers the “y position” and “rotation” of joe to change. Since joe has the event listener, he must be clicked in order to trigger the function, “rotMove.”
Built-in Events
Note: Many classes have methods and properties that can be assigned to them, and some have events that can be assigned to them.
Note: check the “class” tutorial for an explanation of what a class is.
The button class can have the following events assigned:
Button.onDragOut Invoked when the mouse button is pressed over the button and the pointer then rolls outside the button.
Button.onDragOver Invoked when the user presses and drags the mouse button outside and then over the button.
Button.onKeyUp Invoked when a key is released.
Button.onKillFocus Invoked when focus is removed from a button.
Button.onPress Invoked when the mouse button is pressed while the pointer is over a button.
Button.onRelease Invoked when the mouse button is released while the pointer is over a button.
Button.onReleaseOutside Invoked when the mouse button is released while the pointer is outside the button after the button is pressed while the pointer is inside the button.
Button.onRollOut Invoked when the mouse pointer rolls outside of a button area.
Button.onRollOver Invoked when the mouse pointer rolls over a button.
Button.onSetFocus Invoked when a button has input focus and a key is released.
Resources for some information listed can be found in the Resource.pdf file.