Building a platform game - a beginner's guide

  • 261 favourites

Recall that, in order to tell whether Tim is moving up or down, we need to know what floor he was on when last we checked. So we'll add another global variable which we'll use to hold the number of that previous floor:

Add global variable: prevFloor (leave the default type as 'Number' and the initial value as 0).

Whenever Tim falls out the bottom of the layout, we reset the initial value of 'prevFloor' in the Player -> Is outside layout event:

Action: System -> Set value -> prevFloor: 0

We can now make use of all these added items to award Tim the appropriate number of points when he jumps up onto a floor (and stays there). But just how does our game know which floor he's jumped onto?

Well, recall that we've added an instance variable 'floor' to 'Flooring' whose value corresponds to the level of the floor. So, once the Player has landed on a floor, we can test that value and increase the score by the corresponding value of the 'points' instance variable. We just need to find out which instance of Flooring the Player has landed on. We can do that by adding a second condition to the Player | Platform On landed event:

Right-click on the Player | Platform On landed condition and add this condition:

Flooring -> (Pick) Pick nearest/furthest -> (Which) nearest / (X) Player.X / (Y) Player.Y

What does this get us? Well, as the Manual tells us in How events work, "the instances involved ... are 'picked' for the event. Then, the actions run for just the instances picked by the conditions." In this case, it means that, whenever the Player lands, whatever Flooring instance he's nearest to gets picked, so that when we add a sub-event 'System add Flooring.points to score' (as we're about to do) and that action runs, the System knows the correct number of points to add without our having to add separate events for each Flooring instance. That's part of the power of Construct 2.

So now we just have to add the additional actions that will run when the Player lands on a floor. These actions will be added as sub-events of the Player | Platform On landed / Flooring | Pick nearest ... twin condition:

Condition: System -> Compare two values -> First value: prevFloor Comparison: Less than Second value: Flooring.floor

Action: System -> (Global & local variables) Add to ->Variable: score Value:Flooring.points

Action: System -> (Global & local variables) Set value ->Variable: prevFloor Value:Flooring.floor

Condition: System -> Compare two values -> First value: prevFloor Comparison: Greater or equal Second value: Flooring.floor

Action: System -> (Global & local variables) Set value ->Variable: prevFloor Value:Flooring.floor

After all that work, we've still got nothing to show for it! It's time to create the HUD (heads-up display).

As shown in page 7 of the Beginner's guide to Construct 2, we need to add a new layer called 'HUD'. (Make sure it's at the top, and selected.) On it, insert two Text objects, one in the top right named 'StatusText' and the second in the centre of the screen named 'ResultText' (you may want to set the Text object Properties to a rather less plain font). StatusText will be used for keeping track of the score and health, and ResultText will either show 'Game over', when Tim's health declines to zero, or 'Level 2 reached' if he gets to the top floor with a score of 25 or more (we'll leave the actual change to the next level for a further part of this tutorial).

Now we can add one further sub-event to the Player | Platform On landed / Player | Is overlapping Flooring twin condition to show when Level 2 has been reached. This sub-event has a pair of conditions:

Condition: Flooring -> Compare instance variable -> floor: Equal to: 3

Condition: System -> (Global & local variables) Compare variable ->Variable: score: Greater or equal: 25

Action: ResultText -> Set text: ''Level 2 reached"

Now for the 'Game over' display, and the reset of Tim's health for the next round - this'll be a new event (we're finished with sub-events):

Condition: System -> (Global & local variables) Compare variable ->Variable: health: Equal to: 0

Action: ResultText -> Set text: ''Game over"

Action: System -> (Global & local variables) Set value -> health :3

Action: System -> (Time) Wait -> Seconds: 3

Action: ResultText -> Set text: ''" (blanking out 'Game over', ready for the next round)

Lastly, while the game is running, we need to keep the score and health status continually updated:

Condition: System -> Every tick

Action: StatusText -> Set text: "Score: " & score & " Health: " & health

Well, that is the end of this second part of this Platform tutorial. In the next part, we'll look at going up to the Level 2 and challenging Tim with moving platforms. And please post your comments about what you'd like to see in further parts (and feel free to tell me what needs correcting).

In the meantime, if you find the game too hard, remember that, as the game builder, you're free to change the Platform behavior properties for the Player :) .



Download now 608.15 KB

  • Order by
Want to leave a comment? Login or Register an account!