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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With