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 13: Small games <strong>to</strong> keep them loading<br />

The code that is contained within the ‘if ’ statement flips the ‘moveX’ variable and updates the<br />

value for ‘ballX’ so that it is at the position of the previous frame. It is possible that the next time<br />

this code is run the value for ‘ballX’ is still greater than 57, in which case if we had not added the<br />

further condition that ‘moveX’ has <strong>to</strong> be greater than zero then the code within the ‘if ’ statement<br />

would be executed again, flipping ‘moveX’ a second time, so the ball would stick <strong>to</strong> the wall. But<br />

the test for ‘moveX’ ensures that the ball will bounce freely from the wall.<br />

The next ‘if ’ statement tests for a collision with the <strong>to</strong>p wall and behaves identically <strong>to</strong> the test<br />

for the sides; this time we test for the value of ‘ballY’ only when the direction is up, ‘moveY’ is<br />

less than zero. At this point we update the ball. Then we test for a collision with the ‘board’ clip.<br />

This can only happen when the ball is moving down, ‘moveX>0’ and the y value of the ball is<br />

19. The use of an exact value like this means that you cannot change the direction value or the<br />

initial start position without adjusting this value. I point this out because it is a coding technique<br />

<strong>to</strong> discourage. Because the initial y position for the ball is −23, and the increments are in threes<br />

the ball will eventually hit the value 19.<br />

−23 + 14 ∗ 3 = 19<br />

But if you change anything it won’t. Yuck! It would be much better <strong>to</strong> make 19 the goal value.<br />

If we want <strong>to</strong> make sure the test only occurs once then add in another test <strong>to</strong> make<br />

if (moveX>0 && ballY>=19 && ballY0 && ballY>=19 && ballY

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

Saved successfully!

Ooh no, something went wrong!