Swift SpriteKit:TouchesMoved不';当手指停止和相机停止时,t更新
我有一个游戏,玩家移动到触摸屏(如果触摸屏移动,则更新目的地)。在触摸未移动的情况下(手指停留在屏幕上,因此既不调用Swift SpriteKit:TouchesMoved不';当手指停止和相机停止时,t更新,swift,camera,sprite-kit,move,touchesmoved,Swift,Camera,Sprite Kit,Move,Touchesmoved,我有一个游戏,玩家移动到触摸屏(如果触摸屏移动,则更新目的地)。在触摸未移动的情况下(手指停留在屏幕上,因此既不调用touchMoved也不调用touchSedend)相机移动之前,一切都会正常工作。玩家移动到与开始位置相关的正确位置,但与移动的相机无关。(我不想将位置保存在相机的参考框架中..如果这样做可行的话,因为在屏幕侧面点击一下就会移动pl (直到世界末日。) 以下是代码的基本内容: override func touchesBegan(_ touches: Set<UITo
touchMoved
也不调用touchSedend
)相机移动之前,一切都会正常工作。玩家移动到与开始位置相关的正确位置,但与移动的相机无关。(我不想将位置保存在相机的参考框架中..如果这样做可行的话,因为在屏幕侧面点击一下就会移动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
}}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
用于触摸{
位置=触摸。位置(in:self)
player.goto=位置
player.moving=true
}}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
用于触摸{
让位置=触摸。位置(in:self)
player.goto=位置
player.moving=true
}}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
player.goto=位置
player.moving=true
}}
覆盖函数更新(currentTime:CFTimeInterval){
如果player.position.x>w_-screen/2和&player.position.x<(lab.size.width-w_-screen/2){
cameranode.position.x=player.position.x
}
如果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)
标高=400*atan(d/20)/1.57
如果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
}}
如果player.position.x>w_screen/2和player.position.x{
if player.position.x > w_screen/2 && player.position.x < (lab.size.width-w_screen/2) {
cameranode.position.x = player.position.x
}
cameranode.position.x=player.position.x
}
这就是为什么相机没有移动到你想要的地方,你正在移动你的相机到player.position.x,但是你永远不会更新你的goto位置
只需考虑相机的移动量,并相应地调整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
}
if player.position.x>w_-screen/2和&player.position.x<(lab.size.width-w_-screen/2){
让camShiftX=player.position.x-cameranode.position.x
让camShiftY=player.position.y-cameranode.position.y
cameranode.position.x=player.position.x
player.goto.x+=camShiftX
player.goto.y+=camShiftY
}