Keep the Player on the Screen with SpriteKit Constraints

If you have a game where the scene doesn’t scroll, you want to prevent the player from moving off the screen. SpriteKit constraints provide a nice way to keep the player on the screen at all times.

Creating a Position Constraint

Start by creating a range that specifies the lower and upper limits for a sprite’s position. Call the SKRange initializer and supply the lower and upper limits. The initializer for SKRange takes floating point values. You will get a compiler error if you supply integers, such as the following:

After creating the range, call the SKConstraint class function positionX or positionY and supply the range.

The SKConstraint class has two positionX functions. One version constrains the sprite’s x value. The other version constrains both the x and y values. I’m not sure why they didn’t name the other version positionXY.

After creating the constraints, add them to the sprite by setting its constraints property. The constraints property takes an array of constraints.

Constraint Example

Suppose you have a brick breaking game where the paddle moves horizontally. The following code creates a constraint to prevent the paddle from moving off the screen:

Keeping the Sprite Completely on the Screen

One issue with the code listing from last section is half of the paddle may move off the screen. If you want to prevent any part of the sprite from going off the screen, you must account for the sprite’s size when setting the range.

Why can part of the sprite move off the screen with the code from the last section? SpriteKit uses the center of the sprite to determine its position. Suppose the player moves left. The sprite will keep going left until the center of the sprite reaches zero. Because the sprite can move left until its center is at the left edge of the screen, half the sprite can move off the screen.

To keep the sprite completely on screen, set the lower range limit to be half the sprite’s width (or height for vertical constraints). Set the upper limit to be the edge of the screen minus the lower limit.

The following code prevents any part of the paddle from moving off the screen:

Full Example

I have an example on GitHub that demonstrates keeping the player on the screen in a simple game.

The placePlayer function in the GameScene class has the code to keep the player from going off the screen.