我有一个玩家移动触摸的游戏(如果触摸移动则更新目的地)。一切都很完美,直到相机移动而触摸没有移动(手指在屏幕上停留,因此touchMoved
和touchesEnded
都没有被调用。玩家移动到与他开始的位置相关的正确位置,但与移动相机无关。 (我不想在相机的参考框架中保存位置..如果这甚至可以工作,因为屏幕一侧的单击会移动pl
艾尔到世界尽头。)
这里的代码很简单:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
location = touch.location(in: self)
player.goto = location
player.moving = true
}}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
player.goto = location
player.moving = true
}}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
player.goto = location
player.moving = true
}}
override func update(_ currentTime: CFTimeInterval) {
if player.position.x > w_screen/2 && player.position.x < (lab.size.width-w_screen/2) {
cameranode.position.x = player.position.x
}
if player.moving == true {
v = CGVector(dx: player.goto.x-player.position.x, dy: player.goto.y-player.position.y)
d = sqrt(v.dx*v.dx + v.dy*v.dy)
vel = 400*atan(d/20)/1.57
if vel>1 { player.physicsBody!.velocity = CGVector(dx: v.dx*vel/d, dy: v.dy*vel/d) } else {
player.moving = false
player.physicsBody!.velocity = CGVector.zero
}}
任何帮助将不胜感激。
答案 0 :(得分:1)
if player.position.x > w_screen/2 && player.position.x < (lab.size.width-w_screen/2) {
cameranode.position.x = player.position.x
}
这就是为什么相机没有移动到您想要的位置,您将相机移动到player.position.x,但是您永远不会更新您的转到位置。
只考虑相机移动的程度并相应调整goto。
if player.position.x > w_screen/2 && player.position.x < (lab.size.width-w_screen/2) {
let camShiftX = player.position.x - cameranode.position.x
let camShiftY = player.position.y - cameranode.position.y
cameranode.position.x = player.position.x
player.goto.x += camShiftX
player.goto.y += camShiftY
}