gpt4 book ai didi

xcode - 当玩家移动时 SKCameraNode 滞后

转载 作者:行者123 更新时间:2023-11-30 10:04:07 25 4
gpt4 key购买 nike

我目前正在尝试让我的相机在玩家移动时跟随我的玩家“球体”。我已将相机的位置设置为在球体移动时移动到球体的 x 位置,但是由于某种原因,当球体快速移动时,SKCameraNode 会出现故障和卡顿,就好像它试图 catch 节点的位置一样。我读到这是因为物理在实际 update() 之后运行,但我不知道如何解决该问题。有什么办法可以解决这个问题吗?

注意:我不希望相机始终以“球体”为中心,而是在跟随球体时稍微延迟,以赋予其自由移动的感觉。 (如果这是有道理的话)

    import SpriteKit

class GameScene: SKScene {

var orb = SKSpriteNode()
var Touch: UITouch?
let theCamera: SKCameraNode = SKCameraNode()

override func didMoveToView(view: SKView) {

let orbTexture1 = SKTexture(imageNamed:"orb.png")

orb = SKSpriteNode(texture: orbTexture1)
orb.name = "Orb"
orb.position = CGPointMake(300, 95)
self.addChild(orb)

theCamera.position = CGPointMake(300, 0)
theCamera.name = "Camera"
self.addChild(theCamera)
self.camera = theCamera

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
if nodeAtPoint(touch.locationInNode(self)).name == "Orb" {
Touch = touch as UITouch
}
}
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
if Touch != nil {
if touch as UITouch == Touch! {
let location = touch.locationInNode(self)
orb.position = location
}
}
}
}


override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */

let action = SKAction.moveToX(orb.position.x, duration: 0.5)
theCamera.runAction(action)

}
}

最佳答案

您可以使用 didSimulatePhysics 方法而不是 update 方法来解决此问题。

将更新方法替换为:

>     override func didSimulatePhysics() {
> let action = SKAction.moveToX(orb.position.x, duration: 0.5)
> theCamera.runAction(action)
> }

关于xcode - 当玩家移动时 SKCameraNode 滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36987558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com