2016-12-16 4 views
1

touchesBeganを使用してUIButtonsの機能を提供し、タップジェスチャーを使用してメインプレイヤーSKSpriteNodeがトリガー時にジャンプする機能を提供しました。特定の場所に触れるときにジェスチャーをタップしないようにする - SpriteKit

//コードタップに関するUIButtonタッチ

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    //touches began is only used for GUI buttons -> not to affect player 

    for touch: AnyObject in touches { 
     //We get location of the touch 
     let locationOfTouch = touch.location(in: self) 

     if muteButton.contains(locationOfTouch) { //mute the game 
      timer.invalidate() 
      audioPlayer.volume = 0 
     } 

//コードに関する

let tap = UITapGestureRecognizer(target: self, action: #selector(GameScene.tapped(gesture:))) 
    tap.cancelsTouchesInView = false 

    self.view!.addGestureRecognizer(tap) 

...... 

func tapped(gesture: UIGestureRecognizer) { //used to make the player jump  
      player.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 60)) 
      player.physicsBody!.affectedByGravity = true */ 

      } 

私の問題は、私はrestartButtonに押したときにタップジェスチャは、後で活性化されることであるとき、タッチが終了する。何か私にできることはありますか?

+0

別個のタップジェスチャ認識機能を使用する具体的な理由はありますか?ユーザーがボタンに触れないと、プレーヤーがジャンプコードを 'touchesBegan'の中に入れないのはなぜですか? – nathan

+1

私は別のタップジェスチャーを使用しています。これはスライド機能も使用しています。その場合、touchesBeganを使用するとスワイプを識別して認識できません。 –

+0

シーンを再起動するときに、新しいシーンを作成してプレゼンテーションしていますか? – Knight0fDragon

答えて

2

主な問題は、タッチを検出する(ジェスチャ認識機能を使用し、touchesBegan/Moved/Endedメソッドを使用する)2つの別々のシステムが競合していることです。

タッチが1つのボタンの内側にある場合、ジェスチャ認識機能を有効または無効にする方法があります。 touchesBegan方法において

、タッチボタンの内側にある場合に、タップジェスチャー認識無効:ジェスチャ認識を再度有効、touchesEnded及びtouchesCancelledに続い

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches { 
     let locationOfTouch = touch.location(in: self) 
     if muteButton.contains(locationOfTouch) { 
      // mute action 
      tap.isEnabled = false 
     } 
    } 
} 

を:

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    tap.isEnabled = true 
} 

override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { 
    tap.isEnabled = true 
} 

このように、タッチがボタンの内側にある場合、タップジェスチャ認識装置は起動しません。タッチが完了するたびに、次のタッチがプレイヤーをジャンプさせるためにジェスチャーレコグナイザーを再度有効にします。

私はこれを空のプロジェクトでテストし、うまくいきました。

うまくいけば助けてください!あなたのゲームで幸運。

+1

ありがとうございました!それは完全に動作します –

関連する問題