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

Testing and debugging<br />

If you have ‘Examples/Chapter01/pingpong03.fla’ open then you will see that there is a fourth<br />

layer, labelled ‘Debug’. No developer ever gets it all right first time and when something doesn’t<br />

work discovering where the errors are can be something of an art. One useful method is <strong>to</strong> send<br />

information <strong>to</strong> a text box that you can see on screen. You can create a text box by choosing<br />

the Text <strong>to</strong>ol (the A icon) and then placing your mouse on the stage and typing in some text.<br />

You can set the text box <strong>to</strong> operate as a Dynamic Text box using the Properties panel for the<br />

Text <strong>to</strong>ol.<br />

Figure 1.17 Setting up a Dynamic Text box<br />

In this example the settings are for a Multiline box that tracks the variable ‘debug’. Now if you<br />

set the variable ‘debug’ <strong>to</strong> ‘Hello’ using <strong>ActionScript</strong> then that would appear in the box as the<br />

program runs. We can use this <strong>to</strong>ol <strong>to</strong> test lots of useful information about our program. We will<br />

look in<strong>to</strong> debugging in detail in Chapter 11.<br />

The movement of the ball is a little more complicated than the movement of the bats. Open<br />

‘Examples/Chapter01/pingpong04.fla’ and press ‘Ctrl + Enter’ <strong>to</strong> test it. Now the ball bounces<br />

off the walls and the bats. S<strong>to</strong>p playing the game and try clicking the ball and selecting Actions.<br />

Take a look at the code and see if you can work out what is going on. Don’t worry if you can’t,<br />

this chapter is just a quick introduction and although the code in the ball isn’t <strong>to</strong>o hard, it is much<br />

more complicated than the script for the bats. By the time you have worked through the later<br />

chapters of this book it will all be second nature.<br />

Adding a scoreboard<br />

One thing that we can do <strong>to</strong> improve the game is <strong>to</strong> add a score. Those strange boxes that both<br />

have 123 in them will be where we put the score. The LeftBat scores every time the ball goes out<br />

of screen right and the RightBat scores every time the ball goes out of screen left. So how do we<br />

accomplish this?<br />

Take a look at the section of code in the ball clip event that follows the comment ‘//Ball moving<br />

right’. See how useful the comment is. Now you know that the ball is moving right in this section.<br />

If it is moving right then the RightBat should hit it, if it doesn’t then the LeftBat scores a point.<br />

12<br />

...<br />

//Ball moving right<br />

if (_x > 370){

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

Saved successfully!

Ooh no, something went wrong!