加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将相机置于swift spritekit中的节点上

发布时间:2020-12-14 04:50:05 所属栏目:百科 来源:网络整理
导读:我在斯威夫特创造了一个Terraria风格的游戏.我想拥有它,所以玩家节点总是在屏幕的中心,当你向右移动时,这些块就像Terraria一样向左移动. 我目前正在试图弄清楚如何将视图保持在角色的中心.有谁知道实现这个的好方法? 解决方法 以下将使相机居中于特定节点.
我在斯威夫特创造了一个Terraria风格的游戏.我想拥有它,所以玩家节点总是在屏幕的中心,当你向右移动时,这些块就像Terraria一样向左移动.

我目前正在试图弄清楚如何将视图保持在角色的中心.有谁知道实现这个的好方法?

解决方法

以下将使相机居中于特定节点.它还可以在设定的时间范围内平滑过渡到新位置.

class CameraScene : SKScene {
    // Flag indicating whether we've setup the camera system yet.
    var isCreated: Bool = false
    // The root node of your game world. Attach game entities 
    // (player,enemies,&c.) to here.
    var world: SKNode?
    // The root node of our UI. Attach control buttons & state
    // indicators here.
    var overlay: SKNode?
    // The camera. Move this node to change what parts of the world are visible.
    var camera: SKNode?

    override func didMoveToView(view: SKView) {
        if !isCreated {
            isCreated = true

            // Camera setup
            self.anchorPoint = CGPoint(x: 0.5,y: 0.5)
            self.world = SKNode()
            self.world?.name = "world"
            addChild(self.world)
            self.camera = SKNode()
            self.camera?.name = "camera"
            self.world?.addChild(self.camera)

            // UI setup
            self.overlay = SKNode()
            self.overlay?.zPosition = 10
            self.overlay?.name = "overlay"
            addChild(self.overlay)
        }
    }


    override func didSimulatePhysics() {
        if self.camera != nil {
            self.centerOnNode(self.camera!)
        }
    }

    func centerOnNode(node: SKNode) {
        let cameraPositionInScene: CGPoint = node.scene.convertPoint(node.position,fromNode: node.parent)

        node.parent.position = CGPoint(x:node.parent.position.x - cameraPositionInScene.x,y:node.parent.position.y - cameraPositionInScene.y)
    }

}

通过移动相机来改变世界上可见的内容:

// Lerp the camera to 100,50 over the next half-second.
self.camera?.runAction(SKAction.moveTo(CGPointMake(100,50),duration: 0.5))

资料来源:swiftalicio – 2D Camera in SpriteKit

有关其他信息,请查看Apple的SpriteKit Programming Guide(示例:在节点上居中显示场景).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读