2017-07-08 7 views
1

私はこのゲームのキャラクターメニューに文字を表示し、タッチ移動機能によって左右に移動します。ボタンに影響を与える移動した機能に触れる

enter image description here

Iは、特定の文字が表示されるかを判断するボタンロック解除であるかどうかに応じて中心文字変更などの特定のボタンに変更sksceneにおけるボタン(灰色のボタン)を有します。

私の問題は、文字を選択するためにボタンを押すか、または文字をロック解除するために複数の押しが機能するように押すと、私はタッチの終了した機能のボタンに触れていることです以下。私はタッチでそれらを持っている場合

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 

    let touch: UITouch = touches.first! 
    let location: CGPoint = touch.location(in: self) 
    let node: SKNode = self.atPoint(location) 
    let duration = 0.25 

    if node == selectButton { 

     setNewPlayerSprite(nameOfPlayer: (centerPlayer?.name)!) 

    } else if node == lockedButton || node == priceLabel { 

     unlockPlayer(nameOfPlayer: (centerPlayer?.name)!) 

    } else if node == otherButton { 

     getSpecialUnlockData() 

    } else if node == purchaseButton || node == purchasePriceLabe { 

     purchaseCharacter(ID: characterProductID) 

    } else if node == buyButton { 

     giveItemAmount(itemName: "Item2", giveAmount: 1) 

    } 

はそれが仕事を得るために複数回、それを押すために必要で何の問題もなく、それが正常に動作方法を始めました。しかし、プレイヤーが誤って触れてしまった場合、キャラクターのロックを解除したり、マインドを変えたりしたくない場合は、問題のキャラクターのロックを解除します。

メニューを左右に移動させるタッチ移動機能は、自分のボタンを押す能力に影響を及ぼしているため、ボタンを複数回押して機能させる必要があります。

ボタンの1つが押された場合、タッチ移動機能が起動しなくなることはありますか? (または押されて保持されている)ここに私のフルタッチの機能コードです。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 

    //let duration = 0.01 
    let touch: UITouch = touches.first! 
    let newPosition = touch.location(in: self) 
    let oldPosition = touch.previousLocation(in: self) 
    let xTranslation = newPosition.x - oldPosition.x 

    if centerPlayer!.frame.midX > size.width/2 { 
     if (leftPlayer != nil) { 
      let actualTranslation = leftPlayer!.frame.midX + xTranslation > leftGuide ? xTranslation : leftGuide - leftPlayer!.frame.midX 
      movePlayerByX(player: leftPlayer!, x: actualTranslation) 
     } 
    } else { 
     if (rightPlayer != nil) { 
      let actualTranslation = rightPlayer!.frame.midX + xTranslation < rightGuide ? xTranslation : rightGuide - rightPlayer!.frame.midX 
      movePlayerByX(player: rightPlayer!, x: actualTranslation) 
     } 
    } 

    movePlayerByX(player: centerPlayer!, x: xTranslation) 
    priceLabel.isHidden = true; selectButton.isHidden = true; lockedButton.isHidden = true; otherButton.isHidden = true 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 

    let touch: UITouch = touches.first! 
    let location: CGPoint = touch.location(in: self) 
    let node: SKNode = self.atPoint(location) 
    let duration = 0.25 

    if node == selectButton { 

     setNewPlayerSprite(nameOfPlayer: (centerPlayer?.name)!) 

    } else if node == lockedButton || node == priceLabel { 

     unlockPlayer(nameOfPlayer: (centerPlayer?.name)!) 

    } else if node == otherButton { 

     getSpecialUnlockData() 

    } else if node == purchaseButton || node == purchasePriceLabe { 

     purchaseCharacter(ID: characterProductID) 

    } else if node == buyButton { 

     giveItemAmount(itemName: "Item2", giveAmount: 1) 

    } 

答えて

1

私はタッチがif文で特定のビューにあるかどうかを確認します。このような

何か:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 

//let duration = 0.01 
let touch: UITouch = touches.first! 
if touch.view == self.view{ 
    let newPosition = touch.location(in: self) 
    let oldPosition = touch.previousLocation(in: self) 
    let xTranslation = newPosition.x - oldPosition.x 

    if centerPlayer!.frame.midX > size.width/2 { 
     if (leftPlayer != nil) { 
      let actualTranslation = leftPlayer!.frame.midX + xTranslation > leftGuide ? xTranslation : leftGuide - leftPlayer!.frame.midX 
      movePlayerByX(player: leftPlayer!, x: actualTranslation) 
     } 
    } else { 
     if (rightPlayer != nil) { 
      let actualTranslation = rightPlayer!.frame.midX + xTranslation < rightGuide ? xTranslation : rightGuide - rightPlayer!.frame.midX 
      movePlayerByX(player: rightPlayer!, x: actualTranslation) 
     } 
    } 

    movePlayerByX(player: centerPlayer!, x: xTranslation) 
    priceLabel.isHidden = true; selectButton.isHidden = true; 
    lockedButton.isHidden = true; otherButton.isHidden = true 
} 

} 
+0

種類の作品...それは、中央の文字を変更することができないで動作しますが、私は押したときに、それが特定のボタンをタッチしているかどうかを検出する方法がわかりませんそれら。自分のボタンに同じif文を使用できますか?彼らはspritenodesだから – Astrum

+0

ええ、私はそう信じて、このポストは、同様の回答を持っているhttps://stackoverflow.com/questions/27922198/how-do-i-detect-if-an-skspritenode-has-been-touched – Ali

+0

ありがとうすべてが働いた – Astrum

関連する問題