【问题标题】:Camera Movement Not Fast Enough相机移动速度不够快
【发布时间】:2015-07-10 23:24:29
【问题描述】:

我目前正在尝试将相机置于 SpriteKit 中的 SKNode 上。 在更新功能中,我有以下内容:

override func update(currentTime: NSTimeInterval) {
    if self.camera != nil {
        self.camera!.position = CGPointMake(CGRectGetMidX(super.frame) / 10, wayPoints.last!.y)
        self.centerOnNode(self.camera!)
    }
}

这是我的 centerOnNode 函数:

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)
}

这就是 Apple 建议将相机置于节点中心的方式(请注意,这些都位于 World SKNode 中)。这类作品;它使相机居中,但更新速度不够快。 这会产生以下效果(请注意,在我的情况下,'CGRectGetMidX(super.frame) / 10' 将始终是屏幕的中心,而 'wayPoints.last!.y' 将始终是顶点的 Y 位置线): http://gyazo.com/95f02f98e5707b0065e74f90ac547139

如您所见,相机在移动,但线移动得更快。我不确定为什么会发生这种情况,因为相机不应该以与线相同的速度移动吗?

编辑: 我在一个单独的函数中添加/更新 wayPoints 数组,该函数每 0.5 秒调用一次:

NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "addPoint", userInfo: nil, repeats: true)

但是,即使我在 addPoint 函数中更新了相机位置,仍然会出现相同的效果。

【问题讨论】:

  • 确保ui更新方法在主线程中运行?
  • 你在哪里更新wayPoints?如果它在update 方法中,那么您应该将相机更新代码(当前在update)中的代码移动到didFinishUpdate。此外,请确保 wayPoints.last 是您希望相机跟随的点。
  • @Mark 我很确定 SKScene: update 在主线程中运行,所以我相信它在主线程中。
  • @0x141E 我的错,我应该在原始问题中提到这一点。我实际上是在一个单独的函数中更新航点,该函数每 0.05 秒运行/更新一次。我认为这将是问题所在(因为每帧只调用一次更新),但是即使我在更新航点的函数末尾更新相机位置,相机仍然“慢”
  • 看视频我觉得不是更新不够快的问题。看起来您的数学在相机或您正在移动的父级的 y 位置上是错误的。我最好的猜测是你根本不应该打电话给self.centerOnNode(self.camera!)

标签: ios swift sprite-kit sknode


【解决方案1】:

我不确定执行此操作的“正确”方法是什么。但是我将相机节点速度设置为中心节点的速度,并且我将相机的位置设置为节点的位置。例如:

camera?.position = player!.position
camera?.physicsBody?.velocity = player!.physicsBody!.velocity

为了做到这一点,你需要给相机节点一个物理体。当你这样做时,我建议确保它上面有它自己的碰撞、场和接触掩码。

【讨论】:

    猜你喜欢
    • 2022-09-28
    • 2022-07-26
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 2012-10-17
    相关资源
    最近更新 更多