Sunday, August 19, 2007

Classes

Actionscript 3 is super class based, which is cool because classes are extremely powerful, and I've never really used them before. Now I have to learn how to use them. Classes are awesome cuz you can make one and use it over and over again. Here are some examples of simple simple classes:

You can download all the files here: [download]

HERE IS HOW THE CODE IS SET UP FOR A SIMPLE CLASS:
This code needs to be in a seperate .as file
whatever you save this file as is what your CLASS NAME will be

/// this class adds to the built in movie clip class ///
package{
import flash.display.MovieClip; //Imports movie clip class
import flash.events.MouseEvent; //Imports the mouse event

public class FunButton extends MovieClip{
private var _origXScale:Number; //create a variable to remember the original size of movie clip
private var _origYScale:Number; //ditto

public function FunButton(){
_origXScale = this.scaleX; //set x scale variable
_origYScale = this.scaleY; //set y scale variable
this.addEventListener(MouseEvent.ROLL_OVER, grow); //add listener. call function "grow"
this.addEventListener(MouseEvent.ROLL_OUT, shrink); //add listener. call function "shrink"
}

private function grow(event:MouseEvent):void{
this.scaleX *= 1.5; //this is make the mc grow to 150% its size (width)
this.scaleY *= 1.5; //this is make the mc grow to 150% its size (height)
}

private function shrink(event:MouseEvent):void{
this.scaleX = _origXScale; //go back to normal
this.scaleY = _origYScale; //go back to normal
}
}
}

now if you have a movieclip in an fla file, right click on it in the library and go to "linkage" and in the class field type in the CLASS NAME (whatever you named your .as file) and all instances of that movieclip will have those proporties. This is the very very very tip of the iceburg of what classes can do.

No comments: