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 />

up a name using a loop; we will look at using loops in Chapter 9. The format would be:<br />

//Start loop with i=0, with each loop add 1 <strong>to</strong> the variable I<br />

...<br />

eval("Ball" + I)._x += 12;<br />

...<br />

You can even assign the value returned from the ‘eval’ function <strong>to</strong> another variable that then acts<br />

as a reference <strong>to</strong> the original. For example:<br />

1 var <strong>Art</strong>hur:Object = new Object();<br />

2 <strong>Art</strong>hur.feet = 12;<br />

3 <strong>Art</strong>hur.name = "<strong>Art</strong>hur";<br />

4 var James:Object = new Object();<br />

5 James.feet = 6;<br />

6 James.name = "James";<br />

7<br />

8 var objRef = eval("<strong>Art</strong>hur");<br />

9 trace(objRef.name + " has " + objRef.feet + " size feet");<br />

10 objRef = eval("James");<br />

11 trace(objRef.name + " has " + objRef.feet + " size feet");<br />

Listing 7.10<br />

Listing 7.10 gives an output of<br />

<strong>Art</strong>hur has 12 size feet<br />

James has 6 size feet<br />

Lines 1 <strong>to</strong> 6 simply define and initialize <strong>to</strong> objects, one using the variable ‘<strong>Art</strong>hur’, the other<br />

‘James’. Line 8 assigns a reference <strong>to</strong> the object called ‘<strong>Art</strong>hur’ <strong>to</strong> the variable called ‘objRef ’ using<br />

the ‘eval’ function. This is then used for a trace action after which the value of ‘objRef ’ is changed<br />

<strong>to</strong> be a reference <strong>to</strong> the object called ‘James’. Again a trace action is used <strong>to</strong> generate some output.<br />

Although this is a very simple example and you may wonder when you will use it, movie clip<br />

and object references are used extensively in game development.<br />

Summary<br />

We have covered a great deal in this chapter, from creating a basic variable <strong>to</strong> string slicing and<br />

even user-defined classes. You have learnt how <strong>to</strong> add <strong>ActionScript</strong> <strong>to</strong> a frame action and even<br />

<strong>to</strong> use code defined in a separate file. Along the way you have come across two of <strong>ActionScript</strong>’s<br />

built-in objects: the string and the Math object. Now we want <strong>to</strong> move ahead by allowing our<br />

programs <strong>to</strong> make simple decisions, and in the next chapter we will learn how.<br />

100

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

Saved successfully!

Ooh no, something went wrong!