gpt4 book ai didi

swift SpriteKit : TouchesMoved doesn't update when finger stops and camera

转载 作者:行者123 更新时间:2023-11-28 06:17:32 24 4
gpt4 key购买 nike

我有一个游戏,玩家 move 到一个触摸点(如果触摸 move 则更新目的地)。一切正常,直到相机 move 而触摸不动(手指停留在屏幕上,因此既不调用 touchMoved 也不调用 touchesEnded)播放器 move 到正确的位置与他开始的地方有关,但与 move 的摄像机无关。 (我不想将位置保存在相机的引用系中..如果这样可以的话,因为在屏幕的一侧点击一下就会 move pl直到世界尽头。)

finger movement on screen

actual movement

desired movement

这是代码的骨架:

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
}

这就是为什么相机没有 move 到你想要的位置,你正在将你的相机 move 到 player.position.x,但你永远不会更新你的 goto 位置。

只需考虑相机 move 多少并相应地调整 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

}

关于 swift SpriteKit : TouchesMoved doesn't update when finger stops and camera,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44726347/

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