I am using the accelerometer to move a sprite around the screen, and I am using spriteKit collisions to keep the sprite in the bounds of the screen. Is there a way I can prevent the sprite from bouncing off the sides of the screen? (the sprite should just stop on the edge of the screen)
3
votes
2 Answers
10
votes
Try SKConstraint The code would look something like this, and would constrain sprite to the scene. This code goes in your scene file:
let xRange = SKRange(lowerLimit:0,upperLimit:size.width)
let yRange = SKRange(lowerLimit:0,upperLimit:size.height)
//sprite.constraints = [SKConstraint.positionX(xRange,Y:yRange)] // iOS 9
sprite.constraints = [SKConstraint.positionX(xRange,y:yRange)] // iOS 10