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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7: So what is a variable?<br />

Placing variables in<strong>to</strong> objects<br />

Sometimes it is useful <strong>to</strong> keep a block of variables <strong>to</strong>gether, and one way is <strong>to</strong> create an Object.<br />

1 var Clown:Object = new Object();<br />

2 var RingMaster:Object = new Object();<br />

3 var Elephant:Object = new Object();<br />

4<br />

5 Clown.feet = 14;<br />

6 RingMaster.feet = 10;<br />

7 Elephant.feet = 21;<br />

8<br />

9 trace(Clown.feet);<br />

10 trace(RingMaster.feet);<br />

11 trace(Elephant.feet);<br />

Listing 7.5<br />

Listing 7.5 shows how we can create an object. This can be a placeholder for any data that we<br />

choose. In this instance we create three objects and assign them <strong>to</strong> variables ‘Clown’, ‘RingMaster’<br />

and ‘Elephant’. Then we set the feet size and finally we display this size using a ‘trace’ statement.<br />

You can access an object’s variables by using the dot opera<strong>to</strong>r. You give the object’s name, a<br />

dot and finally the variable name you wish <strong>to</strong> access.<br />

Creating a new class<br />

New with <strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> is the ability <strong>to</strong> create new classes. Although these are easier <strong>to</strong> work<br />

with if you have the professional version you can work with them with the standard version. For<br />

the standard version you must work with a text edi<strong>to</strong>r <strong>to</strong> create the class files. In <strong>Flash</strong> <strong>MX</strong> <strong>2004</strong><br />

Professional you can write them directly in the edi<strong>to</strong>r. The most important feature is <strong>to</strong> make<br />

sure the class name and the name of the file match and that it is in same direc<strong>to</strong>ry as your current<br />

project file.<br />

‘Examples/Chapter07/Performer.as’ is a class file. All class files have the extension ‘as’, short for<br />

<strong>ActionScript</strong>.<br />

1 class Performer {<br />

2 var feet:Number;<br />

3 var job:String;<br />

4<br />

5 //Construc<strong>to</strong>r function<br />

6 function Performer (myJob:String, myFeet:Number){<br />

7 job = myJob;<br />

8 feet = myFeet;<br />

9 if (job==undefined) job = "Jack of all trades";<br />

10 if (feet==undefined) feet = 9;<br />

93

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

Saved successfully!

Ooh no, something went wrong!