2017-06-23 6 views
1

私は、プレイヤーがタッチに移動して(タッチが動くと宛先を更新する)ゲームを持っています。タッチが動いていない間にカメラが動くまで指は完全に動きます(指は画面上に静止しているので、touchMovedでもtouchesEndedも呼び出されません)。プレイヤーは開始位置に関して正しい位置に移動しますが、移動に関しては移動しませんカメラ。 (私は、カメラのフレームの位置を保存したくありません。もしそれがうまくいけば、画面の片側にあるタップは、 ayerを世界の終わりに移動させるでしょう)Swift SpriteKit:指が止まってカメラが動いたときにTouchesMovedが更新されない

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 
}} 

ご協力いただければ幸いです。

答えて

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にカメラを移動している、あなたがそれをしたい場所に移動されていませんが、あなたは後藤の位置を更新したことがない理由です。

カメラがどのくらい移動して、それに応じて移動を調整するかを考慮します。

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 

    } 
関連する問題