Join Todd Perkins for an in-depth discussion in this video Winning the game, part of ActionScript 3.0 Projects: Game Development.
- [Instructor] In this movie we'll take a look at how to react to winning or losing our game. If your following along, I'm in winning.fla in the Chapter One folder inside of the Exercise Files folder. I'm going to go back to Scene One, select the First Key from the Actions Layer, and open the Actions Panel. Then I'm going to scroll to the top of the code. The first thing we need to create is a way to win, and a way to lose. So the way we can create a way to win is to create a variable called Score, that holds the players score. So below the variables we've created so far, we'll create a new variable called Score, and the data type will be Integer.
And we'll set the value inside of the initializeGame function. And we'll create a variable called Energy, and this is how you can lose if your energy runs out. And that will also be an integer. Inside of the initializeGame function, at the bottom of all the code that we've written so far, let's set values for the variables that we created. First we'll set the Score value = 0; and that's the current score, so the game starts and the player has a score of zero. On the next line, we'll set the value for the Energy variable.
Energy will equal energy_mc, and that's the energy bar movie clip, .totalFrames. Now let's just step away for a second from this real quick, and take a look at that movie clip. So I'm going to close the Actions Panel, and I'm going to double click mc_energy in the library. And it's really simple, it just has three frames. And so the energy would be three. So each time we get hit, we go down one.
And if energy is less than one, then we lose the game. So, let's go back to Scene One. Go back to our Actions, and below the last line of code we wrote, let's type energy_mc.gotoAndStop. In parentheses we'll pass in (energy); Then we can copy and paste that line of code later, so we can reuse it. Let's scroll down. So now that we have defined some variables that can help us win or lose, let's define functions that determine whether we've won or lost.
So below the Create Monsters function, I'm going to define a new function, and this is going to be called increaseScore. Alright, so the first thing we'll do to increase the score, is we'll add one to it, so type score ++; and if that's unfamiliar or haven't used it in awhile that increments a value by one, so if it's zero when this code runs, it'll go to one, and if it's one it'll go to two, and so on.
On the next line we'll write a conditional statement to see if the score is a winning score. Type if(score >= monstersInGame. So in other words, if they killed all the monsters. Then we'll do something, and for now we'll stop the timers so type monsterMaker.stop(); and then we'll just trace("you win!").
Pretty straightforward. Now we'll create a function that will happen to decrease our energy, so type function decreaseEnergy():void. First thing you want to do is subtract one from our current energy. Just like we can increment by one, we can decrement by one as well. So type energy --; And now we could use the code where we said energy_mc.gotoAndStop energy, but what if energy is too low, in other words, what if their energy is zero, and we try to go to and stop at zero? We might get an error in our code, so I'm going to just type if(energy <= 0).
Type monsterMaker.stop() just because we want to stop the timer. And then we'll trace("you lose!"); and below that we'll put an else statement. And so if energy is one or greater, then we'll have the engery movie clip go to and stop at the energy frame, so type energy_mc.gotoAndStop and we'll pass in (energy), because we know this is going to be one or more.
Now setting up an energy bar this way, is just the way I did it for this exercise. If you wanted to do it a different way you could be creative. You could have a hundred frames, and then you could take away ten from energy each time. So you're not stuck just doing it this way. When does our score increase or decrease? It increases when we kill a monster, and it decrease when we get hit by a monster. So if we want to make these functions run, we should run them in the same place where the code runs, where we hit the monster, or where the monster hits us, and that's inside of the monster movie clip.
So I'm going to close the Actions Panel, and I'm going to double click Monster in the Library. Select the First Key Frame in the Actions Layer, and open up the Actions Panel. I'm just going to add a little bit of code inside of here, to make those functions run from the main timeline. Let's scroll to about line 29, and that's in my code, on your code it might look a little bit different. It might be a different line, and what we need to do is run a function that's on the main timeline. So how do we access a function that's on the main timeline from inside of another movie clip? Well we know that the parent of the monster movie clip is container_mc that's created on the main timeline.
The parent of that movie clip is the main timeline that holds all of our functions. So what we need to do is create a variable, and I'm creating this inside of the hurtPlayer function, and I'm just going to call this Main, to represent the main timeline. And the data type is going to be MovieClip. And I'm going to set that equal to, and I could type this.parent.parent, but Flash might give me a error because parents are always display objects, and so we need to tell Flash that I want to speak about a movie clip. And so that way I can have access to its functions.
So this is an important step. So you want to wrap this inside of MovieClip, and some parentheses. And inside of the parentheses I'm going to type (this.parent); which is again, container_mc. And when I type .parent again, I'm referencing the main timeline. So now I can speak to this main variable as if it were a movie clip, and so I can call a function that's on the main timeline in this way, so I'm going to go to the next line, and I'm going to type main.decreaseEnergy(); and this function is going to run whenever the hurtPlayer function runs, or whenever the movie clip scales too big, or the monster is too big, and then (mumbles), so that hurts the player and the energy goes down.
What I'm going to do is copy these two lines of code, because we're basically doing the same thing both times, so I'm going to just select these two lines, press Command or Control + C to copy them, then scroll down and find the die function. At the very top of the die function, I'm going to paste code that I copied by pressing Command or Control + V, and instead of main.decreaseEnergy, I'm going to type main.increaseScore. So when I kill a monster our score goes up, and when we hit a monster our energy goes down.
So now, we test the movie, and watch the code in action. And then click on all the monsters. And I should get the you win message, if I get all of them in time. And there we go, great. And now I'm going to close this window, and test the movie again, and I should get the you lose message, if I don't hit all of them in time. Now I'm waiting and I'm going to watch my energy bar, and it goes down each time I get hit by a monster.
And I lost. And so now we can control winning and losing the game. Now you don't obviously have to just trace you win or you lose, you can do whatever you want, but you put that code there that happens whenever you win or lose the game. And now that we can control winning and losing the game, let's give a little visual to our user to show them that they won, so it's not just a message in the output window. We'll do that in the next movie.
- Understanding game development
- Building a shooting game
- Making a Ping-Pong game
- Developing the enemy's artificial intelligence
- Creating a word-guessing game
- Placing tiles in a slider puzzle game
- Creating a tank battle game
- Winning and losing a game