Objects

**Objects, classes, and instances**
Everything in Flash is an object. Here are some objects you'll definitely be using: But the whole point of programming is that you can create your own objects.
 * Number -- a positive or negative floating point (lots of decimal places!) number
 * int -- an positive or negative whole number, a.k.a. integer
 * String -- a piece of text
 * Boolean -- i.e., //true// or //false//
 * Array -- a list of other objects

You write a //class// to define an object. Here is how you make a **//class//** in Actionscript 3 (the Flash programming language): code format="actionscript3" package {   public class Car {

} } code This is about the simplest class you can write. The phrase: //public class Car// is how you give the Car object its name. The phrase: //package// says where the Car.as (actionscript file) lives. In your Flex project, Car.as will sit in the //src// (a.k.a. source code) directory, or //src/Car.as// for shorthand.

An //instance// is a particular copy of the object. Here is how you make an object **//instance//**//:// code format="actionscript3" var myCar:Car; myCar = new Car; code In the first line, you declare a //variable// ̣called myCar, whose object //type// is Car. The first line declares //myCar// as just a nickname for an Car object instance, but it doesn't actually create the object instance. The phrase in the second line //new Car// actually creates the object instance. The "//myCar ="// assigns the value of //myCar// to the new object instance so you can refer to it later.

You can think of the class as a recipe (for making a cake) or a blueprint (for making a car). You can think of an object instance as the particular cake or car sitting in front of you. You can make many copies of Car: code format="actionscript3" var myFirstCar:Car = new Car; var mySecondCar:Car = new Car; code //myFirstCar// and //mySecondCar// are two different copies of Car, i.e., if you change the color of myFirstCar, it doesn't change the color of mySecondCar.

**Packages**
Let's make a quick change to Car: code format="actionscript3" package edu.nu.sesp {   public class Car {

} } code We changed the first line to //package edu.nu.sesp.// This means that the Car.as file now has to live in the directory: //src/edu/nu/sesp/Car.as//.

**Variables**
Let's make another change to Car: code format="actionscript3" package edu.nu.sesp {   public class Car {       public var _speed:Number; } } code We added a line: //public var _speed:Number;// which creates a variable (var) named _speed, whose type is Number. This allows us to put objects inside other objects (a number inside a Car).

Now we can do something more interesting with Car: code format="actionscript3" var myCar:Car;      // create a nickname for a car myCar = new Car;  // create the car myCar._speed = 55;  // set the speed of the car to 55; var speedCheck:Number = myCar._speed;  // sets the value of the number speedCheck to the car's speed which is 55

code Notice that you can use the "dot" (the period) to set the value of an object instance's variable.

**Functions**
Let's make another change to Car: code format="actionscript3" package edu.nu.sesp {   public class Car {       public var _speed:Number;

public function stop:void {           speed = 0; }   } } code We added a line: public function stop{ ... }. This is a function. A function can run a sequence of of code statements, like a macro. Now instead we can call the function to stop the car. code format="actionscript3" var myCar:Car;      // create a nickname for a car myCar = new Car;  // create the car myCar._speed = 55;  // set the speed of the car to 55; var speedCheck:Number = myCar._speed;  // sets the value of the number speedCheck to the car's speed which is 55 myCar.stop;       // set the speed of the car to 0;  speedCheck is still 55

code Notice that you use the "dot" (the period) to set the value of an object instance's variable and to call a function.

Let's make another change to Car: code format="actionscript3" package edu.nu.sesp {   public class Car {       private var _speed:Number;

public function setSpeed(value:Number):void{ _speed = value; }

public function getSpeed:Number{ return _speed; }

public function stop:void {           _speed = 0; }   } } code

This first thing we did is change //public var _speed:Number// to **//private//** //public var _speed:Number//. We changed _speed from a public, to a private variable. Now, if you try to do this: code format="actionscript3" var myCar:Car = new Car; myCar._speed = 55;             // ERROR! you can't do this anymore because _speed is private code you will get an error. But that's OK, because we added a setSpeed and getSpeed functions. You can use these functions like this: code format="actionscript3" var myCar:Car = new Car; myCar.setSpeed(55);                       // set the speed of the car to 55; var speedCheck:Number = myCar.getSpeed; // set the value of speedCheck to myCar's speed, which is 55 myCar.setSpeed(0);                        // myCar._speed is now 0, while speedCheck is still 55 code Notice that the functions can have both input and output. The setSpeed function takes 1 //parameter//, a Number, as input and uses it to set the value of _speed. The getSpeed function takes 0 parameters (no input) but returns a Number, the value of _speed, as it's output.

Why would you use the getSpeed and setSpeed functions instead of a public _speed variable? Doesn't this make things more complicated? Check this out:

code format="actionscript3" package edu.nu.sesp {   public class Car {       private var _speed:Number;

public function Car:void{ _speed = 0; }

public function setSpeed(value:Number):void{ if(value >= 0){ _speed = value; }       }

public function getSpeed:Number{ return _speed; }

public function stop:void{ _speed = 0; }   } } code

Now, when you use setSpeed, it will check that the speed is not negative. For example: code format="actionscript3" var myCar:Car = new Car; myCar.setSpeed(55);                       // sets _speed to 55 myCar.setSpeed(-40);                      // this doesn't do anything, speed stays at 55 myCar.setSpeed(0);                        // sets _speed to 0 code Cool huh?

**Getters and Setters**
If you are just using functions to get and set a value, and want it to look like you are getting and setting a variable, but still want to check the values before you set the variable, you can use a special kinds of functions called getters and setters. It looks like this:

code format="actionscript3" package edu.nu.sesp {   public class Car {       private var _speed:Number;

public function set speed(value:Number):void{ if(value >= 0){ _speed = value; }       }

public function get speed:Number{ return _speed; }

public function stop:void {           _speed = 0; }   } } code

Now you access the getter and setter functions as if you were accessing a variable of the object instance, but it the setter still checks the input value: code format="actionscript3" var myCar:Car = new Car; myCar.speed = 55;          // _speed is now 55 myCar.speed = -40;         // this doesn't do anything, _speed stays at 55 myCar.speed = 0;           // _speed is now 0 code

**Constructors**
Let's look at one final kind of special function called the //constructor//. You've actually already used the default constructor: code format="actionscript3" new Car; code The constructor function creates a new object. However, we can specify a custom instructor for a class that runs code whenever you create the object. The constructor looks like this: code format="actionscript3" package edu.nu.sesp {   public class Car {       private var _speed:Number;

public function Car:void{ _speed = 0; }

public function set speed(value:Number):void{ if(value >= 0){ _speed = value; }       }

public function get speed:Number{ return _speed; }

public function stop:void {           _speed = 0; }   } } code Now, when you create a new instance of car, the _speed will be set to 0 right away.