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

27 platform = "";<br />

28 initAction(STOPRIGHT);<br />

Listing 17.3<br />

In the initialization section the values of certain constants are defined so that we can write<br />

‘WALKLEFT’ in the code rather than ‘11’. When you are reading through the code it is much<br />

easier <strong>to</strong> understand if you use names in this way rather than numbers. To facilitate easier debugging<br />

the initialization also creates a string array that contains the names of the possible actions.<br />

Using this array we can get the name of an action using the code:<br />

actionNames[action];<br />

actionNames[WALKRIGHT]<br />

would return ‘WalkRight’.<br />

Now we will look at the functions defined on frame 1.<br />

‘userAction’ is called by the keyboard reader in the onClipEvent for the cat. A ‘switch’ statement<br />

is used <strong>to</strong> select which code <strong>to</strong> run.<br />

1 //===============userAction========================================<br />

2 //This is called by the onClipEvent of the Cat instance<br />

3 //parameters=======================================================<br />

4 //index Possible values WALKRIGHT, WALKLEFT and JUMP<br />

5 //=================================================================<br />

6 function userAction(index) {<br />

7 switch (index) {<br />

8 case WALKRIGHT :<br />

9 userWalkRight();<br />

10 break;<br />

11 case WALKLEFT :<br />

12 userWalkLeft();<br />

13 break;<br />

14 case JUMP :<br />

15 userJump();<br />

16 break;<br />

17 case HALT :<br />

18 if (action == WALKLEFT) {<br />

19 initAction(STOPLEFT);<br />

20 } else if (action == WALKRIGHT) {<br />

21 initAction(STOPRIGHT);<br />

22 }<br />

23 break;<br />

24 }<br />

25 }<br />

Listing 17.4<br />

264

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

Saved successfully!

Ooh no, something went wrong!