代码之家  ›  专栏  ›  技术社区  ›  Astrum

触摸移动功能影响按键

  •  1
  • Astrum  · 技术社区  · 7 年前

    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 回复  |  直到 7 年前
        1
  •  1
  •   Ali    7 年前

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