2017-10-24 19 views
1

私はスワイプジェスチャをSpriteKitのゲームに左右に実装しようとしています。ここでは、プレーヤーが落ちるオブジェクトを画面上部から生成します。私が直面している問題は、タッチが終了して最後のタッチがどこで終了したかをプレイヤーがとどまるまで、指が画面上にある間にプレーヤーの連続的な動きを維持しようとすることです。スワイプのジェスチャー以外にこれを実装するより良い方法があるかもしれません、なぜ私はあなたに頼んでいるのですか?どんな助けも素晴らしいです、ありがとうございました!SpriteKitのスワイプ認識機能の使用

答えて

2

これはスワイプ(私はあなたがパンを使用していると思う)ジェスチャーを使用したいものではありません。あなたがしたいのは、シーン上でtouchesBegantouchesMoved、およびtouchesEndedコールを上書きし、これらの3つの方法に従って動きを計画することです。

おそらく、これらの方法でSKAction.move(to:duration:)を使用し、一定の速度を維持するための数学を計算したいと思うでしょう。

E.G.

func movePlayer(to position:CGPoint) 
{ 
    let distance = player.position.distance(to:position) 
    let move = SKAction.move(to:position, duration: distance/100) // I want my player to move 100 points per second 
    //using a key will cancel the last move action 
    player.runAction(move,withKey:"playerMoving") 
} 

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) 
{ 
    let touch = touches.first 
    let position = touch.location(in node:self) 
    movePlayer(to:position) 
} 

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) 
{ 
    let touch = touches.first 
    let position = touch.location(in node:self) 
    movePlayer(to:position) 
} 


override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    let touch = touches.first 
    let position = touch.location(in node:self) 
    movePlayer(to:position) 
} 
+0

素晴らしいですが、これは大変感謝しています! @KnightOfDragon – Alex

+0

問題はありませんが、他の人に知らせるために受け入れたことを確認してください – Knight0fDragon

関連する問題