Applications open until spots fill up - Apply now.

Implementing points

Implementing points

April 13, 2017

There is one thing missing, the player's score! It's not much fun dodging obstacles without any reward.

Adding the score label

Drag a Label object into the GameScene.sks, set the Name to scoreLabel and set the font to something you like and will also be easy for the player to read. You could try Helvetica Neue, Bold, 72. Set the Z Position to 10 as you want to ensure it's always in the foreground.

Add label

Position it somewhere clear and easy to read, near top of the screen is good.

If you are having difficulty moving the scoreLabel or just want finer control over node placement in general. You can use the Arrow Keys to move any node a pixel at a time or hold Shift + Arrow Keys to move in a bigger step.

Can you add a code connection in GameScene.swift for scoreLabel? Tip: The node type will be SKLabel

Open GameScene.swift and add the following property:

var scoreLabel: SKLabelNode!

Add the following code after the buttonRestart connection:

scoreLabel = self.childNode(withName: "scoreLabel") as! SKLabelNode

Great the connection has been made, if you did it yourself, virtual high five.

Tracking score

The scoreLabel will display game score. However, as it stands there is no way to track this, let's add a score counter.

Open GameScene.swift is open and add the following property to the GameScene class:

var points = 0

When the game starts you want to ensure the label is reset to 0, let's make that happen.

Add the following code to the bottom of the didMove(to view:) method:

/* Reset Score label */
scoreLabel.text = "\(points)"

scorLabel.text expects a string points is an Int, wrapping points in "()" converts it into a string.

So far if the bunny collides with anything it will trigger the death sequence. When there is a collision with the goal you want to score a point instead of ending the game. When a collision occurs you need some way of knowing if this was a collision between the bunny and the goal, it it's a collision with anything else the game is over.

The didBegin(_ contact:) method provides contact which an SKPhysicsContact object. This object contains two properties: bodyA and bodyB which represent the two objects that just made contact with each other. In this next step you will check them both and if one is the player and the other the goal you will add 1 to points and ignore the rest of code that ends the game.

Open GameScene.swift and add this code to the start of the didBegin(...) method, before the gameState check.

/* Get references to bodies involved in collision */
let contactA = contact.bodyA
let contactB = contact.bodyB

/* Get references to the physics body parent nodes */
let nodeA = contactA.node!
let nodeB = contactB.node!

/* Did our hero pass through the 'goal'? */
if nodeA.name == "goal" || nodeB.name == "goal" {

  /* Increment points */
  points += 1

  /* Update score label */
  scoreLabel.text = String(points)

  /* We can return now */
  return
}

When a collision takes place between two bodies the information is stored in a SKPhysicsContact object. You can use this to find out more information about the collision, so first you grab a reference to the SKPhysicsBodies. However, you may have a custom class with your own properties and want to access those, so you go up a level and get a reference to the parent node this body belongs.

With the reference to SKSpriteNode node you can check for the Name of goal. You can then update the players points and use that to update the score label. After that you return from the didBegin(...) method, otherwise the game would end. Important! a function ends when it hits a return statement!

Run the game. With a bit of skill you should be able to pass through the goal and get a point. You can always make the goal area bigger for testing :]

Summary

Congratulations on finishing Hoppy Bunny, give the person next to you a high five.

In this chapter you learned to:

  • Adding a SKLabel
  • Manage the player score
  • Identifying specific physics collisions

The next chapter will be a recap of everything you have covered so far, well done.

Feedback

If you have feedback on this tutorial or find any mistakes, please open issues on the GitHub Repository.

Join Make School

Prepare for your career as a founder or software developer

Apply Now

Talk to Us

If you have any questions, please send us an email

Email Us