Classes and OOP

In this book, I concentrate on the various principles and formulas involved in creating ActionScripted animation, rather than on teaching object-oriented programming (OOP) or specific coding methodologies. Thus, I wont get too involved in the creation of large class libraries and building complex inheritance structures and frameworks. Rather, Im going to keep things as simple as possible.

The animation concepts youll learn here can definitely be incorporated into more advanced ActionScript classes. However, my goal is not to hand you a prebuilt set of animation classes to copy and paste, but to pass on some understanding of the principles.

All that being said, lets take a ten-minute, drive-by course on classes.

If you think you do not have any familiarity with classes, you may be wrong. If youve written any code in Flash, chances are you used several classes. A class simply refers to a type of object. MovieClip is a class referring to, you guessed it, movie clips. Text fields, movie clips, buttons , strings, and numbers all have classes.

A class basically has two things associated with it: properties (data or information) and behaviors (actions, or things it can do). Properties are essentially variables that hold information relating to the class, and behaviors are just functions, though when a function is part of a class, we usually refer to it as a method .

A basic class

Similar to the relationship between symbols and instances, classes are templates, and objects (also known as instances) are individual manifestations of a particular class. You can make a simple class like this:

 class MyClass {    public var myProperty:Number = 100;    public function myMethod() {       trace("I am here");    } } 

Youll see that within the class are only two things: a variable named myProperty and a function named myMethod . These will become properties and methods of any instance of this class you create. The word public means that any code outside the object will be able to access that property or call that method. If you create properties or methods that are meant only for use internal to the class, you can label them private , which prevents them from being messed with by outside code.

This class code must be in an external text file named the same as the class, with the suffix .as , as in MyClass.as . You can create the file by using the ActionScript editor in Flash (creating a new ActionScript file) or by using your favorite code editor or any other text-editing program. This file must be in the same directory as your FLA file or in your class path .

The class path is simply a list of directories. When you specify a class name in your code, Flash will search those directories for a class with that name . You can set an overall class path, which will apply to any and all FLA files, in the ActionScript 2.0 Settings panel, accessed by pressing the ActionScript 2.0 Settings button in the Preferences panel, as shown in Figure 2-12, and an additional class path for a specific FLA file in the Publish Settings dialog box. By default, the current directory of the FLA file, and the classes directory in the Flash configuration directory, are what compose your class path. If you wind up with a bunch of other classes that you want to use across several projects, you can put them in their own directory and add that directory to the class path.

image from book
Figure 2-12: Specifying your class path

Back in Flash, create a new FLA file and save it in the same directory where you just created the class file. On the timeline, you can make a new instance of the class like this:

 var myInstance:MyClass = new MyClass(); 

Flash will search the class path for the specified class. When it finds it in the current directory, it will use that code to create a new instance of the class. This instance will have all the properties and methods defined in the class. You can test it by running the following:

 trace(myInstance.myProperty);   // traces 100 myInstance.myMethod();   // traces "I am here" 

Constructors

You can set a constructor for the class, which is a method that has the same name as the class and is automatically called when a new instance is created. You can pass arguments to the constructor as follows .

First, create the class:

 class MyClass {    public function MyClass(arg) {       trace("constructed");       trace("you passed " + arg);    } } 

Then, back on the timeline in Flash, create the instance:

 var myInstance:MyClass = new MyClass("hello"); 

This should trace "constructed" and then "you passed hello" .

Inheritance

A class can inherit from, or extend, another class. This means that it gets all the same things that the other class has. The subclass (the one that is inheriting) can then add additional properties and behaviors, or change some of the ones from the superclass (the one that is being extended). This is done like so:

 class MyBaseClass {    public var myProperty1:String = "A";    public var myProperty2:String = "B"; } class MySubClass extends MyBaseClass {    public var myProperty2:String = "C";    public var myProperty3:String = "D"; } 

Remember that each class must be in its own file named after the class name, with the .as extension, so you will have a MyBaseClass.as file and a MySubClass.as file in the same directory as your FLA file. Now, you can make a couple of instances and see what happens:

 var myBaseInstance:MyBaseClass = new MyBaseClass(); trace(myBaseInstance.myProperty1); // traces "A" trace(myBaseInstance.myProperty2); // traces "B" var mySubInstance:MySubClass = new MySubClass(); trace(mySubInstance.myProperty1); // traces "A" trace(mySubInstance.myProperty2); // traces "C" trace(mySubInstance.myProperty3); // traces "D" 

The first instance has no surprises . But notice that the second one has a value of "A" for myProperty1 , even though MySubClass does not define myProperty1 . The class inherited it from MyBaseClass . Next, notice that myProperty2 traces "C" , not "B" . We say that the subclass has overridden the property. Finally, the subclass adds a new property, myProperty3 , which the base class does not have.

A MovieClip subclass

You may or may not write a class, and then write another class that extends that. But chances are that, if you do much with ActionScript 2, you will eventually wind up extending the MovieClip class. The class MovieClip is the template for all the ActionScript properties and methods that are part of a movie clip object. It contains properties such as _x , _y , _xscale , _alpha , _currentframe , and so on, and methods like gotoAndPlay , attachMovie , lineTo , and so on.

If you write a class that extends MovieClip , it will automatically inherit all the properties and methods inherent to a movie clip. Then you can add specific behaviors or properties that apply only to the type of object you are creating. For example, say you wanted to make a spaceship object for a game. You might want it to contain some graphics, have a position on the screen, move around, rotate, change its appearance over time, contain some sounds, listen for enterFrame events for animation, and listen for keyboard and mouse events for interaction. These are all things that movie clips can do, so it makes sense to extend MovieClip . You could then add custom properties such as speed , fuel , and damage and custom behaviors such as takeOff , crash , shoot , and selfDestruct . The class might start out something like the following:

 class SpaceShip extends MovieClip {    var speed = 0;    var damage = 0;    var fuel = 1000;     function takeOff() {       // . . .    }    function crash() {       // . . .    }    function shoot() {       // . . .    }    function selfDestruct() {       // . . .    } } 

Lets make an actual class that extends MovieClip and see it in action.

  1. Start again with chapter2base.fla . Right-click the ball symbol in the library and choose Linkage from the pop-up menu.

  2. Select Export for ActionScript and leave Export on first frame checked. In the AS 2.0 Class field, enter Ball . This links the ball symbol to the Ball class, which you are about to create.

  3. Create an ActionScript file named Ball.as in the same directory as the movie, and type the following in it:

     class Ball extends MovieClip {     function onEnterFrame():Void     {         this._x += 5;     } } 

This class adds only one thing to the base MovieClip class: defining the onEnterFrame function to handle that event.

You can see how powerful this could be for animation, especially when you are creating more than one of the same thing with some complex behavior. Rather than creating or assigning the code each time you create a new instance, it becomes a native part of the new object as soon as its born.



Foundation ActionScript. Animation. Making Things Move
Foundation Actionscript 3.0 Animation: Making Things Move!
ISBN: 1590597915
EAN: 2147483647
Year: 2005
Pages: 137
Authors: Keith Peters

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net