Você está na página 1de 2

TUTORIAL

Page 1
PLACING VARIABLES INTO OBJECTS

NOTE: It is useful to keep a block of variables together, and one way is to create an
Object.

var Clown:Object = new Object();
var RingMaster:Object = new Object();
var Elephant:Object = new Object();

Clown.feet = 14;
RingMaster.feet = 10;
Elephant.feet = 21;

trace(Clown.feet);
trace(RingMaster.feet);
trace(Elephant.feet);

Explanation

In this instance we create three objects and assign them to variables Clown,
RingMaster and Elephant.

Then we set the feet size and finally we display this size using a trace statement.

You can access an objects variables by using the dot operator. You give the objects
name, a dot and finally the variable name you wish to access.

CREATING A NEW CLASS

class Performer {
var feet:Number;
var job:String;

//Constructor function
function Performer (myJob:String, myFeet:Number){
job = myJob;
feet = myFeet;
if (job==undefined) job = "Jack of all trades";
if (feet==undefined) feet = 9;
}

//Method to return property values
function dump():String {
return("Hi, my job is " + job + " and my feet are " + feet + " inches long.");
}
}

TUTORIAL

Page 2

NOTE: This file should be saved as Performer.as as an external file using a text editor.
Must

A class definition always starts with the keyword class, followed by the name you have
chosen
for the class.

All remaining code is between curly brackets. Any variables the class contains are
usually declared next along with their data type.

A class usually will have what is called a constructor function. A function is simply a
block of code that is given a name; following the name will be ordinary brackets.
Contained inside the brackets will be none or several parameters separated by commas.
The code for the function is contained between curly brackets.

A function can return a value; if so, it is a good idea to specify the type of value that is to
be returned. If you dont actually create a constructor function for your class then Flash
will create one for you. In this simple example we have the class Performer that
contains two variables, feet and job.

It has one method dump, which returns a string description of the current instance of
the class. It also has a constructor function; these must have the same name as the class.

USING A USER-DEFINED CLASS

var Clown:Performer = new Performer("Clown", 14);
var RingMaster:Performer = new Performer("Ringmaster", 10);
var Helper:Performer = new Performer();

trace(Clown.dump());
trace(RingMaster.dump());
trace(Helper.dump());

NOTE: Put the above codes in your flash and execute it.

What is the result for trace(Helper.dump());?

Você também pode gostar