Asteroids

2:30 pm Flash, Games, Programming, ade's stuff

Lazy reader’s link to the current version of the game

I went to a short flash-based videogame course a few weeks back. The exam is in form of a homework assignment where you select one of three games to implement. I chose to make a asteroids-style game clone. The platform is Flash CS3 and Actionscript 3.

I started out with the ship, drawing it as a movieclip with 4 frames, for idling, turning left and right, and moving forward. Then i made a SpaceShip-class that handled as much as possible of the ship and its movement. First, I made it turn, with some basic turning-speed acceleration and deceleration, using the three different frames to animate it. Click to try, use left and right arrows to turn.

Then there was some coding involved with making the ship move. The ship uses a vector for movement and modifying its course uses vector addition, which creates a pretty cool skidding effect. When the ship moves toward the edge of the game area, it needs to warp the opposide side of the screen, and there should be visual evidence before the transfer is complete that the spaceship is going to show up on the other end. So, there needed to be a “clone” involved that acts as a copy of the spaceship until the original ship is outside the screen, at which point the clone will take over. I ended up using a border of movieclips and using hit-testing to see if the ship was getting close to the edge. Try version two. Use left, right and up arrows.
When the ship movement was complete, i started working on the “asteroids”. So I put some rocks from my plant in my scanner and put them as a single movieclip in flash. Then i made the Rock class, to handle the rocks movement across the screen. They use a similar edge-detection and clone method, as well as movement. They start with a random position, speed and apperance. Click here to see.

Then of course, I needed to make a end-game screen when the ship hit an asteroid. So i had this vision in my head, how i would call rockMovieClip.hitTest(shipMovieClip) and just find out at once if the ship was hitting the rock. But a quick test revals that flash uses boxes for hit detection and a not-so-fast googling of the situation revealed that there was not really a practical way to do this. I tried to make the pixel-level hitdetection that was described in the BitmapData class but it just wasn’t a very clean solution, so i just made the objects check for their distance instead, since the rocks are kinda round and the ship too. Try here.

Finally, the ship obviously needs to shoot something. I made a green little round particle thingy and copied the movement code from the other classes and wired everything together in the flash file (first frame). Tanja decided my green projectiles were unsatisfactory and suggested I made them pink instead which turned out quite fabulous. Use spacebar to shoot. You may notice that the shots are aware of the ship’s movement.

There’s a slight bug still, where asteroids can “leak” outside the playing area. Need to check my edge-handling code a bit. The “A:” counter shows how many rock instances you are currently dealing with. Get it to 0 and you win (but I haven’t made the win-screen yet.)

Update: The final version is complete.

Leave a Comment

Your comment

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please note: Posting more than 2 links in your comment will put the comment in the moderation queue.