2017-07-08 47 views
1

我在我的遊戲中顯示字符,並通過觸摸移動功能從一邊移動到另一邊這個字符菜單。觸摸移動功能影響按下按鈕

enter image description here

我在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) 

    } 

如果我讓他們在接觸開始正常工作方法沒有任何問題與需要多次按下它才能正常工作。但是如果玩家偶然輕觸它,那麼如果他們不想解鎖角色或改變主意,它就會解鎖出現問題的角色。

觸摸移動的功能,使菜單從一邊移動到另一邊影響我的能力,使按鈕按下,因此我需要多次按下按鈕才能使其工作。

如果按下其中一個按鈕,是否有停止觸發移動功能的功能? (或被按下並保持)這裏是我完整的觸摸功能代碼。

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