在Swift 3.0 SpriteKit中设置世界的边界

时间:2023-01-23 07:49:21

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.

我正在尝试制作游戏,但我坚持玩家的相机运动。我需要设置最大相机和玩家x位置,但我可以向右或向左移动。

I was tying to use override func didFinishUpdate()

我想要使​​用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

请帮忙

1 个解决方案

#1


2  

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)

把它放在更新中,然后添加其他3个边界(上面是左边界)

this also assumes player.anchorPoint is 0.5

这也假设player.anchorPoint为0.5

#1


2  

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)

把它放在更新中,然后添加其他3个边界(上面是左边界)

this also assumes player.anchorPoint is 0.5

这也假设player.anchorPoint为0.5