Learning flash and actionscript 3(AS3)

Many flash developers that produce applications in as2 see only trouble when trying to convert to as3. The problem is the change from designer thinking to object oriented programming. The gap is huge. Lets look at some samples in creating flash in actionscript 3.

Car exampleIf we visualize a car, the car has many nuts and bolts creating the the whole car witch is the term we use. When looking at the content of the car we can just imagine how many items there are, but lets look at a few. Some of the items in a car can be an engine, number of wheels, steering wheel, doors and headlights. We can recreate this car in programming as well. We can create an object called “Car” that contains the items as variables. This object can be used one time or many times, depending what you want to create. Lets create a simple car object using the standard Object variable:


// first creating an object called car
var car:Object = new Object();
// giving the car properties to define the car
car.name = "My custom Porche";
car.engine = 6.3;
car.numberWheels = 4;
car.stearingWheel = "Recaro";
car.doors = "Elevating doors";

This was just an example coded car, but to use this efficiently we should create this object as an own class witch also means as a standalone file. A class is a construction containing attributes and properties representing the whole object, in our case the “car”. Lets first look at some other variables commonly used before we look further into our car example:


var object:Object = new Object();
var string:String = "Text based variable";
// integer represents a whole number
var integer:int = 422;
// the number variable is an integer but can contain decimals
var number:Number = 4.3;
// boolean variables can be true or false
var boolean:Boolean = true;
// arrays are lists of data
var array:Array = new Array();
// setting the first position in the array with a string
array[0] = "first position";

All these variable types has their own creator when they are made. On the right side of the equation you call something called a constructor. This creates the object you have defined and can include data as parameters between the “(” and “)”. Lets look at the array constructor, witch can contain parameters:


// creating an array containing two strings as parameters to the constructor
var array2:Array = new Array("first position = 0", "second position = 1");

Similar to the Array object we can customize our own objects and create a class that represents that object. Lets now create a class with the attributes we assign the first car object but with a constructor:


package
{
public class Car
{
// defining attributes to the car
private var _name:String = "My custom Porche";
private var _engine:Number = 6.3;
private var _numberWheels:int = 4;
private var _doors:String = "Elevating doors";

// The Car constructor
public function Car(name:String="custom car", engine:Number=0, numberWheels:int=4, doors:String="")
{
// setting the global attributes to the incoming variables
_name = name;
_engine = engine;
_numberWheels = numberWheels;
_doors = doors;
}
}
}

If we now what to create the car object, the simple line of code is as follows:


// using the input parameters in the constructor to create my car
var mycar:Car = new Car("Porche", 3.2, 4, "Normal doors");

Hope this little tutorial helps you understand more of the world in object oriented language and gives you a hint in the direction of actionscript 3. I plan to post a continuance of this tutorial containing how to use the designer i Flash to your advantage(create visual content) and combine this to the actionscript 3 language. Stay put, it will arrive soon.

1 comment:

  1. Roger, 15. September 2008, 20:02

    All very good, but how do I add optional parameters to the class constructor? Also, how do i include optional parameter objects.

    For instance in your example here, you might want to set numWheels to a class wide default value of 4, therefore avoiding having to declare it at every new instance. Instead it appears as an optional parameter or params Object that can be passed only when required.

    I notice that in your call to the constructor (new Car(“Porsche”, 3.2, 4, …. etc you pass a value of 4 for the number of wheels, but this value is already set, twice?, when you’ve declared the class attributes and again in the class constructor method.

    What would the class building syntax look like? and how would the class constructor method determine whether you were choosing to set an optional parameter or not?

    The alternative I guess would be to provide getter and setter methods for these optional parameters, then just call them after first building an instance of the class

    var mycar:Car = new Car(….)
    mycar.setNumWheels(3)

     

Write a comment:

Captcha
Enter the letters you see above.