Platform-Style Video Games with Construct

Running out of Health and Losing

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This video shows how to display a “lose” message when the character runs out of health points.

Keywords

  • Construct 2
  • Sprites
  • Obstacles

About this video

Author(s)
Lee Stemkoski
First online
15 February 2019
DOI
https://doi.org/10.1007/978-1-4842-4446-3_18
Online ISBN
978-1-4842-4446-3
Publisher
Apress
Copyright information
© Lee Stemkoski 2019

Video Transcript

Lee Stemkoski: In this video, we’ll add some more Lose and Win conditions. You might recall from a previous video that we added the lose condition. If the player runs out of time, the game is over. In this video, we’ll add a second lose condition, if the character’s health equals zero. We will also add our first win condition. If the character reaches a goal object, a flag, which we’ll also add to the game in this video.

To begin, let’s open up our project from last time. Last time we had added an object which can damage you and take away health and losing too much health shouldn’t cause you to lose the game. We’ve already added a message which has an animation frame that displays the game over message. So we’ll just make use of the objects we already have, and we’ll go right to the Event sheet to begin. So in the Main panel, clicking to go over to the Event sheet. At this point, we have 15 events. We’re going to add event number 16. So we’ll click on the Add event.

And this time, we’re going to check if the Koala’s on screen, but the health has gone down to zero. You might recall that we’ve added an event similar to this before. Right clicking on Add event. First, let’s add this System condition. We’re checking the value of a variable. So we’ll choose the condition Compare variable. And then in the window that appears, we’ll change the variable from Coins to Health. And we’re going to check if Health is equal to zero. We’ll also add a second condition. So right click the condition we just added and from the pop-up menu, select add another condition. And the second condition will be a Koala condition. Click on Next and we’ll scroll down to the category Size & Position and choose Is on-screen. So if the Koala is still in the game but the Health has gone down to zero, well, will display the game over message and we’ll remove the Koala from the game.

So we’ll add a series of actions clicking on the words Add action for this event you just created. First, we’ll choose the Message object and underneath Appearance, choose the action Set visible that allows you to change the visibility. We’ll make the message visible. We also need to change it to the correct animation, so it shows the correct image. So we’ll click to add another action, another Message action. Click on Next and underneath Animations, choose the action Set animation. And here, in quotation marks, we’ll type Lose. We want to display the lose picture. This is a bad ending. Finally, we’ll also destroy the Koala in this case, so we’ll add another action. This will be a Koala action. We’ll scroll down to the Miscellaneous category and choose the action Destroy. We’ll save the game and we’ll give it a test.

In this case, we want the Health to drop down to zero, so immediately go. We’ll walk over on the spikes. You might notice that even though we’re still in the spikes, we’re not losing additional health. That’s because of the condition we used on collision, so we actually have to leave the vicinity of the spikes and go step on them again. Sure enough, when the health goes to 0, the quality is destroyed, no more Koala, and the game over message appears. However, it would be good to address that issue to keep taking damage when you hit the spikes.

So before we had a win condition, let’s change that. Let’s close the Preview browser. Go back to our Event sheet. So let’s take a look at this event we created in a previous video Koala on collision with spikes. Let’s right click on that condition and delete it. We’ll replace it with a slightly different condition. Right click and we want to Add another condition. This will be a Koala condition and this time, instead of On collision with another object which only triggers once when you first hit the object, let’s instead choose the condition Is overlapping another object. This can happen continuously. Click to choose the object and as before, we’ll choose the Spikes.

However, this would subtract health extremely quickly. This would subtract one health about 60 times a second, so touching the spikes once would cause the game to end very quickly. So in order for this to only happen once in a while, this Flash animation that we added that causes the Koala to get damaged when it’s first hit, we’ll consider this as a temporary invincibility period. So you can take damage, but then you have these two seconds while you’re flashing to escape from the spikes before you take additional damage. To implement this, we’re going to add a second condition to this event. Let’s right click the condition to Add another condition. This will be a Koala condition. And underneath the Flash category, let’s choose Is flashing. And then right click that condition and invert it. So the way this event now reads, if the Koala is overlapping the spikes and the Koala is not flashing, then you subtract one from the Health.

Let’s test this out and see how it plays differently from our previous arrangement. Save the game, back to the layout. Previewing the game. All right. So the Koala’s health is at 3. Let’s move the Koala onto the spikes. The health goes down to 2. While it’s flashing, it doesn’t take additional damage, but after those two seconds go by, if you haven’t escaped from the spikes, you will get additional damage. That seems slightly more realistic. Very good. So we’ve added our second lose condition. The next step is to add a win condition.