02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

11<br />

12<br />

}<br />

13 //Method <strong>to</strong> return property values<br />

14 function dump():String {<br />

15 return("Hi, my job is " + job +<br />

16 " and my feet are " + feet + " inches long.");<br />

17<br />

18 }<br />

}<br />

Listing 7.6<br />

A class definition always starts with the keyword ‘class’, followed by the name you have chosen<br />

for the class. All remaining code is between curly brackets. Any variables the class contains are usually<br />

declared next along with their data type. A class usually will have what is called a ‘construc<strong>to</strong>r’<br />

function. A function is simply a block of code that is given a name; following the name will be<br />

ordinary brackets. Contained inside the brackets will be none or several parameters separated by<br />

commas. The code for the function is contained between curly brackets. A function can return<br />

a value; if so, it is a good idea <strong>to</strong> specify the type of value that is <strong>to</strong> be returned. If you don’t<br />

actually create a construc<strong>to</strong>r function for your class then <strong>Flash</strong> will create one for you. In this<br />

simple example we have the class ‘Performer’ that contains two variables, ‘feet’ and ‘job’. It has<br />

one method ‘dump’, which returns a string description of the current instance of the class. It also<br />

has a construc<strong>to</strong>r function; these must have the same name as the class. So having created the code<br />

that defines the class, how do we use it?<br />

Using a user-defined class<br />

1 var Clown:Performer = new Performer("Clown", 14);<br />

2 var RingMaster:Performer = new Performer("Ringmaster", 10);<br />

3 var Helper:Performer = new Performer();<br />

4<br />

5 trace(Clown.dump());<br />

6 trace(RingMaster.dump());<br />

7 trace(Helper.dump());<br />

Listing 7.7<br />

First it is vital that the syntax for the class definition is correct, second the class definition file must<br />

be in the same folder as the ‘fla’ and must have the same name as the class being defined with an<br />

‘as’ extension. As long as these simple rules are kept <strong>to</strong>, the use of a class is simplicity itself. You<br />

use the ‘var’ keyword followed by the variable name and a colon. After the colon place your class<br />

name. Then use the equals sign, the keyword ‘new’ and then the name of the class, open brackets<br />

and then set the parameters. In this example we first give the ‘job’ name and then the feet size.<br />

Notice that at line 3 of Listing 7.7 we pass no parameters. This is OK and <strong>Flash</strong> will set the values<br />

<strong>to</strong> undefined. Lines 9 and 10 of Listing 7.6 set the values <strong>to</strong> a default value if undefined. In lines<br />

94

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!