ขณะนี้ฉันกำลังพยายามให้กล้องติดตามผู้เล่นของฉัน "orb" ทุกครั้งที่ผู้เล่นถูกย้าย ฉันได้ตั้งค่าตำแหน่งของกล้องให้ย้ายไปยังตำแหน่ง x ของ orb ทุกครั้งที่มีการเคลื่อนย้าย orb อย่างไรก็ตาม ด้วยเหตุผลบางประการ เมื่อ orb ถูกเคลื่อนที่อย่างรวดเร็ว SKCameraNode จะผิดพลาดและติดอ่างราวกับว่ามันกำลังพยายามไล่ตามตำแหน่งของโหนด ฉันได้อ่านแล้วว่าเป็นเพราะหลักฟิสิกส์ทำงานหลังจากการอัพเดตจริง () แต่ฉันไม่รู้ว่าจะแก้ไขปัญหาอย่างไร มีวิธีแก้ไขปัญหานี้หรือไม่?
หมายเหตุ: ฉันไม่ต้องการให้กล้องอยู่ตรงกลาง "ลูกแก้ว" เสมอไป แต่ควรหน่วงเวลาเล็กน้อยในขณะที่กล้องติดตามลูกแก้ว เพื่อให้กล้องรู้สึกเคลื่อนไหวได้อย่างอิสระ (ถ้ามันสมเหตุสมผล)
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)
}
}