触摸影响按下按钮的移动功能

touches moved function affecting pressing a button

我的游戏中有这个角色菜单,它显示角色并通过触摸移动功能从一侧移动到另一侧。

我在 skscene 中有一个按钮(灰色按钮)随着中心字符的变化而变为某个按钮,具体取决于某个字符是否已解锁决定将显示哪个按钮。

我遇到的问题是,当我尝试按下按钮 select 角色或按下以解锁角色时,需要多次按下才能使其工作,我可以触摸touches ended 函数如下所示。

  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)

    }

如果我在 touches begin 方法中使用它们,它可以正常工作,无需多次按下它即可正常工作。但是,如果玩家不小心触摸到它,即使是轻微的触摸也会解锁角色,如果他们不想解锁角色或改变主意,这就是一个问题。

使菜单从一侧移动到另一侧的触摸移动功能影响了我按下按钮的能力,因此导致我需要多次按下按钮才能使其正常工作。

有没有办法在按下其中一个按钮时停止触发触摸移动功能? (或被按住)这是我的完整触摸功能代码。

  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)

    }

我会使用 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
}

}