AddThis Social Bookmark Button
 Should you learn Object Oriented Programming with Actionscript ? (part 3)

In part 2 of this article you have learned how to create a class in actionscript and how to create an instance of that class on the timeline with code or by associating a movie clip with the class through a linkage id.

In Part 3 we are going to have a look at properties and methods, what they are in relation to a class and how to interact with them using actionscript.

What are properties and methods in OOP

Let's take the example of a fruit class. There are many types of fruits but we can define a set of properties that will help us to describe each of them more precisely. We could for example create a fruitSize property, a fruitColor property and a last property that we can call fruitEdible that will indicate if the fruit can be eaten or not.

In a class, properties need to be declared before the constructor just after the class declaration.

You will also notice that I am using the keyword public when I declare the properties. This means that I want to be able to set and retrieve the value of the properties directly on the instance of the class. You could also set it to private but in that case you wouldn't be able to set and retrieve the values through the instances of the class, it would only be available from INSIDE the class or from the sub classes. This is a very important concept in OOP; properties and methods can be set to private or public. Making properties and methods private ensures that your code follows the perfect rule of encapsulation. Encapsulation basically means that your class code is self contained and that the properties and methods are hidden from the rest of the world, the code, therefore, cannot be broken.
// AS 2.0 class definition



class
Fruit {

public var fruitSize:Number;
public var fruitColor:String;
public var fruitEdible:Boolean;

function Fruit (sizeValue, colorValue,edibleValue) {

fruitSise =sizeValue;
fruitColor = colorValue;
fruitEdible = edibleValue;

trace("fruit created");

}
}

// AS 3.0 class definition

package {

public class Fruit {

public var fruitSize:Number;
public var fruitColor:String;
public var fruitEdible:Boolean;

function Fruit (sizeValue, colorValue,edibleValue) {

fruitSize = sizeValue;
fruitColor = colorValue;
fruitEdible = edibleValue;

trace("fruit created");

}
}
}


If we strictly want to follow the rules of encapsulation we should really make the 3 properties above private but if we do that they would not be accessible through the class instances. The trick here is to use a getter and setter method that will act as a proxy between the private properties and the code outside the class that wants to interact with those properties. The beauty of using a getter and setter function is also that you have more control on the value that can be set to the associated property.
For those of you who want to have a look at it now you can see the code below, but we will come back to that subject later. I have implemented a getter and setter method just to set and get the fruitSize value (notice that the internal name is _fruitSize, fruitSize is the value that we use from the outside world) but you could also do that for fruitColor and fruitEdible.

// AS2.0

class Fruit {

private var _fruitSize:Number;
private var _fruitColor:String;
private var _fruitEdible:Boolean;

function Fruit(sizeValue,colorValue,edibleValue) {
_fruitSize = sizeValue;
_fruitColor = colorValue;
_fruitEdible = edibleValue;
trace("Fruit created");
}

public function get fruitSize(){
return _fruitSize;
}

public function set fruitSize(fValue:Number){
if(fValue>=200){
trace("too high");
} else {
_fruitSize = fValue;
}

}
}

//AS3.0

package
{

public class Fruit {

private var _fruitSize:Number;
private var _fruitColor:String;
private var _fruitEdible:Boolean;

function Fruit(sizeValue,colorValue,edibleValue) {
_fruitSize = sizeValue;
_fruitColor = colorValue;
_fruitEdible = edibleValue;
trace("Fruit created");
}

public function get fruitSize(){
return _fruitSize;
}

public function set fruitSize(fValue:Number){
if(fValue>=200){
trace("too high");
} else {
_fruitSize = fValue;
}

}
}
}


That's it, you have created a class with properties and a method. You will notice that the 3 properties that we defined at the beginning of the class are given a value in the constructor.

You can now save your class as Fruit.as and create a new FLA. In your FLA on the main timeline enter the following code to create a fruit.

import Fruit;

var myApple:Fruit = new Fruit(100,"green",true);

trace(myApple.fruitSize);

This will output Fruit created followed by 100 which is the size that we assigned when we create our fruit. If you try to set the value of fruitSize directly on the Apple instance with the property being private (if you are not using a getter and setter) you will end up with the following error message the member is private and cannot be accessed so you will need to declare the property as public or use a getter setter to maintain encapsulation.

Now we can add a method to the class. A method is an action that can be called on any instance of the class. Before you knew about OOP you have been using methods in Flash many times: in myButton.onPress you are calling the method onPress on an instance of the button class called myButton, as simple as that but now you have the luxury to create your own methods. You need to define your methods in the body of the class, for example let's create a method eatFruit, see the code below:

// AS 2.0 class definition



class
Fruit {

public var fruitSize:Number;
public var fruitColor:String;
public var fruitEdible:Boolean;

function Fruit (sizeValue, colorValue,edibleValue) {

fruitSize = sizeValue;
fruitColor = colorValue;
fruitEdible = edibleValue;

trace("fruit created");

}

public function eatFruit(){
if (fruitEdible == true){
trace("very tasty fruit!");
} else {
trace("I would not advise you to eat that fruit");
}
}
}

// AS 3.0 class definition

package {

public class Fruit {

public var fruitSize:Number;
public var fruitColor:String;
public var fruitEdible:Boolean;

function Fruit (sizeValue, colorValue,edibleValue) {

fruitSize = sizeValue;
fruitColor = colorValue;
fruitEdible = edibleValue;

trace("fruit created");

}

public function eatFruit(){
if (fruitEdible == true){
trace("very tasty fruit!");
} else {
trace("I would not advise you to eat that fruit");
}
}
}
}

As you can see methods can also be declared as private or public, since we need to be able to access directly on the instance we need to set it as public. A constructor in AS3.0 can just be public (if not specified it is public by default), a method can be public or private, if it is private it can only be called from inside the class or by the sub-classes.

Save your class again and add the following code on the first frame of your time line

import Fruit;

var myApple:Fruit = new Fruit(100,"green",true);
var myRottenOrange = new Fruit(150,"orange",false);

myApple.eatFruit(); // will outPut very tasty fruit
myRottenOrange.eatFruit(); // will output I would not advise you to eat that fruit

You have learned the basics about properties and methods but there is a lot more to it, this article is just a taste of things to come if you are willing to move further into OOP. There are a lot of good books about OOP with actionscript, I suggest you have a look at the Flashvalley books section where I have made some suggestions.


AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.

Mike: I agree! This was a very helpful first overview for me. - Thank you!
(18.06.2008, 13:26)

Vasillis: Excellent Article! - Thanx Calwen
(10.06.2008, 08:56)

Your comment:
Name:


Email or homepage (optional):
Please enter the 4 letters validation code.
CAPTCHA image

flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved