Centering the camera on a node in swift spritekit

前端 未结 3 1376
北恋
北恋 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:39

    Since iOS 9 / OS X 10.11 / tvOS, SpriteKit includes SKCameraNode, which makes a lot of this easier:

    • positioning the camera node automatically adjusts the viewport
    • you can easily rotate/zoom the camera by transform in the camera node
    • you can fix HUD elements relative to the screen by making them children of the camera node
    • the scene's position stays fixed, so things like physics joints don't break the way they do when you emulate a camera by moving the world

    It gets even better when you combine camera nodes with another new feature, SKConstraint. You can use a constraint to specify that the camera's position is always centered on a character... or add extra constraints to say, for example, that the camera's position must stay within some margin of the edge of the world.

提交回复
热议问题