In the last section, we created a way to detect collisions, created particle explosions, and broke the asteroids up into smaller pieces when hit.
In this section, we’ll keep track of the lives, score, make levels, and create a GUI for starting, restarting, and pausing the game.
In the last section, created as asteroids class to easily create a variety of asteroids, as well as move them around on screen.
In this section, we’ll create a way to detect collisions between the asteroids and the ship or bullets, and create particle explosions when the collisions happen, as well as break apart asteroids upon collision.
In the last section, we created bullets, and a method of wrapping the ship and bullets to the other side of the screen when they reach an edge of the screen.
In this section, we’ll create an asteroids class that allows us to make a variety of asteroids quickly and easily. We’ll also move them around, and wrap them on the screen.
In the last section, we added keyboard input to the ship, so that we could rotate in any direction, and move forward.
In this section, we’ll create bullets that are fired from the front of the ship at a limited rate, and make both the ship and the bullets wrap around when they reach the edge of the screen.
In the previous section, we created a base class for all our game objects, and created and rendered our space ship.
In this section, we’ll add keyboard control to make our ship be able to move around on screen, and actually see our game start coming to life!
Getting Keyboard Input, and Storing Multiple Key Presses
When the flash window is in focus, whatever is “clicked on” will generally have focus. Using blitting, luckily we only have to worry about the stage having focus, and handling keyboard input from it.
To be able to get when a key has been pressed, we have to modify ‘Main.as’.
In the last section we setup our main game class, created our main game loop, and created the background we will be rendering all of our game objects on to.
In this section, we’ll make a base class for all our game objects, create our ship, and use the arrow keys to move it around on screen.
Creating the GameSprite Base Class
The first thing we need to do is create our GameSprite class.
Right click on the folder on your ‘src’ folder on the right, and add a new folder called ‘Entities’. Next right click on that folder, and create a new class called ‘GameSprite’.
This object will include the basic things that all of our game objects will need, including our ship, bullets and asteroids.
In the previous section, we planned out what we are going to accomplish with the game, and setup the flashdevelop environment for creating our asteroids game,.
The game can’t be asteroids without being in space. For this, we’ll create and add the background bitmapdata to our stage, and create a game class to handle everything associated with creating and handling the game.
Creating the Game class
First, lets create the game class. Right click on the ‘src’ folder and go to “Add-> New Class”, and name it ‘Game’ and click ‘ok’.
Now that you have your Game class, we’ll start filling it in. To be able to draw on the screen, we’ll need to have a bitmap to be able to add to the stage, and a bitmapdata to add to the bitmap.
For the game constructor, we’ll want to pass in the size and width of the screen, so that we can draw on it. So we change Game.as to:
This tutorial will create a vector based asteroids game using the completely free, and best solution for creating flash games, FlashDevelop.
Why use FlashDevelop?
FlashDevelop is open source, and much better for programming than the flash IDE (and with a $800 savings!). It is truly a remarkable program, and I suggest you donate and let the guys know you appreciate their hard work and effort.
- built for programming -you wont get bogged down by the flash IDE which is primarily an animation tool.
- great code hinting: This will help you learn flash commands quicker, and see function parameters without needing to refer to the documentation
- does not crash (compare to flash IDE of several times a day, or as much as every time it is run, when using JSFL or components)
- free + open source (you can literally download the source, open it up, and see how it was made, or add custom plugins to it – if you want)
- faster (compiles faster, faster to switch between files, built for speed)
- Easier to transition to programming in another language/environment (most other programming languages aren’t centered around “timelines” and “stages”, and “adding children to display lists”. I like to be able to have control over the order my objects are rendered in, and be able to know when each piece is updated.