Yaroslav  Sarnitskiy
Yaroslav Sarnitskiy

Reputation: 75

Set boundaries to the world in Swift 3.0 SpriteKit

I am trying to make the game, but I stuck on camera movement with player. I need to set max camera and player x position, but I get infinite movement to the right or left.

I was tying to use override func didFinishUpdate()

override func didFinishUpdate() {
    cam.position.x = player.position.x
}

and here I tried to set world size

    worldNode = SKSpriteNode()
    worldNode?.size.width = backGroundImage.size.width
    self.addChild(worldNode!)

Please help

Upvotes: 2

Views: 376

Answers (1)

Fluidity
Fluidity

Reputation: 3995

func keepPlayerInBounds() {
  if player.position.x < frame.minX + player.size.width/2 {
    player.position.x = frame.minX + player.size.width/2
  }
}

put this in update, then add the other 3 boundaries (the above is the left boundary)

this also assumes player.anchorPoint is 0.5

Upvotes: 2

Related Questions