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.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

3<br />

4 //Bounce off the sides<br />

5 if ((ballX>57 && moveX>0) || (ballX= 31) {<br />

32 go<strong>to</strong>AndPlay ("ballstart");<br />

33 } else {<br />

34 prevFrame ();<br />

35 }<br />

Listing 13.3 Frame ‘ballplay’ – ‘Examples/Chapter13/pong.fla’<br />

Here is the ‘meat’ of the game. First we update the ‘ballX’ and ‘ballY’ variables by adding the<br />

values for ‘moveX’ and ‘moveY’. Then we test <strong>to</strong> see if the ball has hit the side. This happens if the<br />

x value for the ball is greater than or less than 57. If we only test for the ball location then we could<br />

end up trapping the ball. What we need <strong>to</strong> do is detect for a collision with the side only when the<br />

ball is heading <strong>to</strong>wards that side. This ensures that when the ball bounces off the wall it does not<br />

immediately set itself up <strong>to</strong> hit the wall, then bounce off, then hit in an infinite loop. If we test for<br />

the right side, when the ‘ballX’ variable is greater than 57 and only react if the ‘moveX’, the ball’s<br />

direction, is greater than zero then we only react <strong>to</strong> the collision when the ball is heading right.<br />

196

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

Saved successfully!

Ooh no, something went wrong!