Swift SpriteKit:TouchesMoved 在手指停止和相机停止时不更新
Swift SpriteKit: TouchesMoved doesn't update when finger stops and camera
我有一个游戏,玩家移动到一个触摸点(如果触摸移动则更新目的地)。一切正常,直到相机移动而触摸不动(手指停留在屏幕上,所以 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
}}
如有任何帮助,我们将不胜感激。
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
}
我有一个游戏,玩家移动到一个触摸点(如果触摸移动则更新目的地)。一切正常,直到相机移动而触摸不动(手指停留在屏幕上,所以 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
}}
如有任何帮助,我们将不胜感激。
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
}