Setting A Restore Point For A Character In SpriteKit

Published on: 1 Sep 2017

Last updated on: 21 Aug 2019

Adding assets

You will need some graphics to complete this part of the tutorial. You can use the same graphics as I have used. Download

Open Assets.xcassets

Drag into the middle column a wide background. My image size is 4096 x 768 pixels.

You also need an image to represent the player and one for the bomb.

Given them appropriate names if you want to.

The scene editor

Open up GameScene.sks

Press alt, command and 7 to open up the utilities inspector.

At the bottom, click the Show Object Library button.

From the Object Library drag a camera onto the scene.

The camera will be used to scroll across the scene when the witch flies.

Show the Media Library (next to the Object Library)

Drag the background onto the scene and set its position to x 0 and y 0.

Drag the witch or your player sprite to the left hand side of the scene.

Set the name of the sprite to Player

Change the physics definition body type to Bounding rectangle.

Disable all the other physics options.

Drag the bomb to the right hand side of the scene.

Change the physics definition body type to Bounding rectangle.

Disable all the other physics options.

Select the Custom Class Inspector and change the custom class to Bomb. This tells the scene that the bomb sprite is an instance of the Bomb class.

Now we are going to create our restore point.

From the Object Library, drag a Color Sprite onto the scene.

You can keep its colour as red because when the game runs, this sprite will be hidden.

Resize and position the sprite. In a functional game you may have a restore point as being a certain door or platform. In this example it goes from top to bottom of the scene so the player would always trigger the contact no matter what the y position is.

Ensure the restore point sprite is selected and again choose the Custom Class Inspector. This time change its custom class to RestorePoint

The restore point also needs a physics body type of Bounding rectangle in the Physics Definition section.

The restore point in my example looks like this:

And that is it.

The witch will fly across the screen. When she comes into contact with the hidden restore point, her position will be saved.

When she hits the bomb and is killed, the scene restarts. But this time the lastRestorePoint variable in the Game struct will have a value and the scene will move the witch to that position and start the game again.

You can download the example project.