Remember how we used the addChild method to put the tank on the stage? We’ll use the same method to make the shots appear, but this time connect it to the space bar. In addition, we need to create a way for the computer to keep track of each shot fired so it can track and move each, as well as remove them from play if they leave the screen. Later we’ll also need to see if they hit something.
To make this happen we’ll start by adding a few lines of code to the detectKeyDown, detectKeyUp, and moveAround functions, then create some new functions to handle the placement and movement of the shots.
- Begin by saving your file with a new version number. I used spaceInvadersV0.4. The 4 is for lesson 4, and I’m using numbers less than one (0.4) to note versions in early development. Once the game works as I want on a basic level (V 1.0) I’ll start advancing in whole number increments with better improvements like score board and level upgrades.
- Our first code change in this piece is a new variable in the form of an array variable. An array is a really just a container that holds a collection of variables. In this case we’ll use an array to hold all the shots fired by the player. Go back up to the part of the code where you wrote your variables earlier in the project and add it where shown here:
- Test the code by using Ctrl+Enter. At this point you should have no errors. The tank object goes on screen and moves back and forth with ease. The new array has no effect at this time as long as your syntax is correct.
- Next, find the detectKeyDown function. You can press Ctrl+F and search your code to find it.
- Add the code indicated to the function:
- Test your code by pressing Ctrl+Enter. You will get an error in the Compiler Errors Panel indicating Scene 1, Layer ‘Layer 2’, Frame 1, Line 41 1180: Call to a possibly undefined method addBullet. This means that there is an undefined function, addBullet, that has no definition. That’s because we haven’t written it yet, so nothing to fix.
- Next, find the detectKeyUp function. You can press Ctrl+F and search your code to find it.
- Add the code indicated to the function:
- Test your code by pressing Ctrl+Enter. You will get an error in the Compiler Errors Panel indicating the same thing explained in step 7. Don’t stress, yo?
- Next we’ll create the function that fires off shots at the enemy invaders. We’ll be using the same code structure that added the tank onto the game by assigning a variable name to each shot fired where b:shot creates an instance of the shot symbol and names it b, then uses the addChild method to put it into play As mentioned earlier we use the array variable to store the collection of shots created by this function. In addition, the instance of the shot object is located at an x,y position using the horizontal position of the tank at that moment and a vertical position slightly higher than the tank (this means a smaller y value).
- Test your code using Ctrl+Enter. This time the tank moves back and forth and is able to create shots every time the space bar is pressed, but the shots don’t move from there.
- We need a function to make the shots move. This works by using a loop that works its way through each instance in an array and changing its y property. In addition, it checks to see if the object has gone beyond the top of the game (y<0) and removes it if it has. Loop syntax is essentially for( loop code ) {what to do with each instance}. The loop code itself sets up a counting loop in the parenthesis by setting up a variable (usually i), determining the range of counting by referencing the length of the array, and counting through it (i–), performing the instructions contained in the braces that follow.
- Test your code. As long as you have no errors, you are okay.l Because we don’t have a listener in place that calls out the moveShots function.
- Finally (for this phase) we’ll add the listener. Just below the moveShots function, add the following code:
- Test your code. Now you should have a tank that moves back and forth, shots fly forth from the tank. and the shots moving up and across the screen.
- Save your work. Be sure you have been versioning your file names. This one should be V4 or V.4.