Where Do Objects Come From?


When two objects have known each other for a long time, and are very much in love, they…. Wait a minute, that's something else. Objects come from classes.

A class acts as a blueprint from which object instances of that class can be created. It states what properties that object has and what things that object can do (methods).

There are two ways of building objects: One is the old way that can still be done in the Flash authoring environment. The other way must be done in an external ActionScript file written in ActionScript 2.0. It is beyond the scope of this chapter to go into great detail about building classes, but it is covered in Chapter 18, "External ActionScript." Our main examples will be in ActionScript 2.0, but there will also be the ActionScript 1.0 counterparts so you can see the difference.

To create an object class in Flash, you first have to create an external ActionScript file (*.as) with the same name as your class (in our examples, "Car"). Although it is not a necessity, it is good coding practice to capitalize object class names. To begin the object class, you use the keyword class then the class name followed by an open curly bracket. And you will need the constructor function, which is basically just a function inside the class file that has the same name as the class itself as you can see below. Then a closing curly bracket to close the class.

ActionScript 2.0

 //create the class class Car{    function Car(){       //stuff    } } 

ActionScript 1.0

 //create the class Car = function(){ //stuff } 

Now, this example doesn't have any properties, methods, or events. Nor are there any instantiated properties. The instantiated properties are passed to the object by means of parameters in the constructor function. So now we will add some properties to the class and some parameters to the function. Then we will instantiate the properties from within the constructor function.

ActionScript 2.0

 //create the class class Car{    public var speed:Number;    private var speedLimit:Number;    private var color:String;    function Car(theColor:String, theSpeed:Number, theSpeedLimit:Number){       this.color = theColor;       this.speed = theSpeed;       this.speedLimit = theSpeedLimit;    } } 

ActionScript 1.0

 //create the object class constructor Car = function(theColor:String, theSpeed:Number, theSpeedLimit:Number) {      //instantiate a few properties      this.color = theColor;      this.speedLimit = theSpeedLimit;      this.speed = theSpeed; } 

This block of code is a little more advanced than the previous. We pass three parameters into the constructor function and then instantiate them to the object within the function. Of course, there could be several more properties, but for now we are just sticking with three; the color of the car, its current speed, and its maximum allowable speed.

We now have an object class. From this we can create several Car objects, which are called instances, and set their individual properties.

This next block of code would be written in a frame action of a Flash movie that has access to our class file (i.e. in the same directory):

 //create a mustang var mustang:Car = new Car("red", 35, 170); //create a jeep var liberty:Car = new Car("silver", 45, 130); //create a 86 pinto var lemon:Car = new Car("yellow", 10, 45); //now get the current speed of the jeep trace(liberty.speed); //Output: 45 

As you can see, we created three individual instances of the Car object. We gave them each individual properties and even retrieved the current speed of the Jeep instance we made.

So far, our object just has properties. Of course, we want any car we make to be able to do things, so now let's add a method to the class:

ActionScript 2.0

 //create the class class Car{    public var speed:Number;    private var speedLimit:Number;    private var color:String;    function Car(theColor:String, theSpeed:Number, theSpeedLimit:Number){       this.color = theColor;       this.speed = theSpeed;       this.speedLimit = theSpeedLimit;    }    //create a method for the Car class    public function accelerate(amount:Number):Number{       //increase the speed       this.speed += amount;       //return the current speed       return this.speed;    } } 

ActionScript 1.0

 //create the object class constructor Car = function(theColor:String, theSpeed:Number, theSpeedLimit:Number) {      //instantiate a few properties      this.color = theColor;      this.speedLimit = theSpeedLimit;      this.speed = theSpeed;      //create a method for the Car class      this.accelerate = function(amount:Number):Number{           //increase the speed           this.speed += amount;           //return the current speed           return this.speed;      } } 

In this block of code, we take our object class a bit further by creating a method that has one parameter, the amount to increase the speed by. Notice that after we increase the speed, we also return the current speed of the object calling the method.

Now we can create a car, call the method, and see what happens. Again, the next block of code is after the Car constructor:

 //create a mustang var mustang:Car = new Car("red", 35, 170); //get the current speed trace(mustang.speed); //call the accelerate method mustang.accelerate(10); //now get the current speed trace(mustang.speed); //call the accelerate method, and get the new speed trace(mustang.accelerate(15)); //Output: 35 //       45 //       60 

This time, we create a single instance of the Car object. We then send the current speed to the Output panel. After that, we call the accelerate method, and then send the current speed again to the Output panel. Finally, we call the accelerate method and send the new speed to the Output panel at the same time; this is where the return action comes in handy.

Now we can create cars and increase their speed, but what if they go over their designated speed limit? This is where the events come in.

This time, we create a blank method that will act as a callback (more on callbacks in Chapter 13). Then in the accelerate method, we will put a conditional to check if the car has gone over its designated speed limit. If it has, the blank method will be called.

ActionScript 2.0

 //create the class class Car{    public var speed:Number;    private var speedLimit:Number;    private var color:String;    function Car(theColor:String, theSpeed:Number, theSpeedLimit:Number){       this.color = theColor;       this.speed = theSpeed;       this.speedLimit = theSpeedLimit;    }    //event callback    public function onSpeedLimitBreak():Void{};    public function accelerate(amount:Number):Number{       //increase the speed       this.speed += amount;       //if the car is going to fast, call the event       if(this.speed > this.speedLimit){          this.onSpeedLimitBreak();       }       //return the current speed       return this.speed;    } } 

ActionScript 1.0

 //create the object class constructor Car = function(theColor:String, theSpeed:Number, theSpeedLimit:Number) {      //instantiate a few properties      this.color = theColor;      this.speedLimit = theSpeedLimit;      this.speed = theSpeed;      //here is the event      this.onSpeedLimitBreak = function(){};      //create a method for the Car class      this.accelerate = function(amount){           //increase the speed           this.speed += amount;           //check to see if the car is going to fast            if(this.speed > this.speedLimit){                //trigger the event                this.onSpeedLimitBreak();           }           //return the current speed           return this.speed;      } } 

This time, we create what looks like a normal method, but it is in fact our event callback for when the cars speed is beyond its speedLimit. You can tell it is meant to be an event because it starts with "on". Then in the accelerate method, we still increase the car's speed, but then we check to see if the speed is above the speedLimit. If so, we call the empty event. Then the current speed is returned.

So now let's create an instance of the Car object. Create a callback event method (again, events are covered in greater detail in Chapter 14). Then use the accelerate method to push the car's speed past its speed limit.

 //create a mustang var mustang:Car = new Car("red", 55, 170); //create the event callback for this Car mustang.onSpeedLimitBreak=function(){      //send a message to the output panel      trace("slow down lead foot"); } //call the accelerate method trace(mustang.accelerate(120)); //Output: slow down lead foot //        175 

As just mentioned, we create the instance of the Car object. Then we create a callback method that will send a message to the Output panel in the event that the car goes beyond its speedLimit property. Finally, we call the accelerate method to increase the car's speed.

Now you have created your own object class with properties, methods, and events. You have created an instance of that object and used its methods and events. There is one more way to add information to an object class without having to place it in the class file.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

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