Centering the camera on a node in swift spritekit

前端 未结 3 1372
北恋
北恋 2021-02-10 22:19

I am creating a Terraria-style game in Swift. I want to have it so the player node is always in the center of the screen, and when you move right the blocks go left like in Terr

3条回答
  •  北海茫月
    2021-02-10 22:46

    You have to create World node that contains nodes. And you should put anchorPoint for example (0.5,0.5). Center on your player. And then you should move your player.

    func centerOnNode(node:SKNode){
    
       let cameraPositionInScene:CGPoint = self.convertPoint(node.position, fromNode: world!)
       world!.position = CGPoint(x:world!.position.x - cameraPositionInScene.x, y: world!.position.y - cameraPositionInScene.y)
    }
    
    
    override func didSimulatePhysics() {
      self.centerOnNode(player!)
    }
    

提交回复
热议问题