I have this character menu in my game that displays characters and is moved from side to side by the touches moved function.
I have a button in the skscene (the grey button)that changes to a certain button as the center character changes depending on if that certain character is unlocked or not determines what button will be displayed.
The problem I have is that when I try and press the button to select the character or press to unlock the character it takes multiple presses to make it work, I have the touches for the buttons in the touches ended function as seen below.
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 I have them in the touches began method it works fine without any problems with needing to press it multiple times to get it to work. But if the player accidentally touches it even slightly then it unlocks the character which is a problem if they didn’t want to unlock the character or changed their mind.
The touches moved function that makes the menu move from side to side is affecting my ability to make button presses and therefore causes me to need to press the button multiple times to get it to work.
Is there someway of stopping the touches moved function from firing if one of the buttons is pressed? (or being pressed and held) here is my full touches functions code.
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)
}