BalloonsNow there's not much left to do on our simple game. Otto will get a bit bored bouncing about with no purpose in life. If you were a yellow ball-like object, what better thing to do than burst some colourful balloons. Okay, maybe a better thing would be hanging around Wimbledon in June and hope you end up in Anna Kournikova's pocket. Sorry Otto, it's balloons for you. Here's the code to put some balloons up on the Stage, this is in our init frame (frame 5): var mcBalloon = new Array(); The purpose of this code is place our balloons alternately with red, blue and yellow colours near the top of our stage. If you go ahead and run the code now, you should see the balloons, however Otto doesn't interact with these, in fact he passes right through them There's a couple of ways to detect if and when your sprites (or movieclips) collide with each other. You can try to use hitTest(), but because our balloons have some pesty string attached that would count as a hit, and we don't want that. Or you can use x/y coordinates to detect of the objects are within a rectangle of each other, this is the method we will use here. The code for detecting the collisions will go into the start frame: // main program loop When we move withing 20 pixels of the centre of a balloon, we are moving the balloons y cooridinate off the screen (to -100) this will give the effect of 'popping' the balloon. Along with a nice sound effect this should do the trick. Download tutorial-4.fla. This concludes the tutorial on writing a simple game. We have covered these basic concepts:
Almost every game you encounter will be based on these simple principles. With a little imagination you should be able to create almost any 2D game! I hope you have enjoyed this tutorial.
( categories: )
|
User login |