Changing .delaysTouchesBegan = true to = false USUALLY works in my game, however, every once in awhile it becomes stuck on true and will not switch back to false unless I close the app out and reopen.
The game is similar to Flappy Birds, but before touchesBegan starts the game, you have the option to swipe left or right to change colors, music, etc. So I need the .delaysTouchesBegan to be 'true' before touches begin. This is an intermittent issue, so I'm not sure what the cause could be.
I've created my swipe gestures in my GameScene class:
let swipeRightRec = UISwipeGestureRecognizer()
let swipeLeftRec = UISwipeGestureRecognizer()
Then I added this function to my didMove(to view):
func addSwipes(){
swipeRightRec.addTarget(self, action: #selector(GameScene.swipedRight) )
swipeRightRec.direction = .right
self.view!.addGestureRecognizer(swipeRightRec)
swipeLeftRec.addTarget(self, action: #selector(GameScene.swipedLeft) )
swipeLeftRec.direction = .left
self.view!.addGestureRecognizer(swipeLeftRec)
if gameState == .showingLogo {
swipeLeftRec.delaysTouchesBegan = true
swipeRightRec.delaysTouchesBegan = true
}
}
Then in touchesBegan:
if swipeLeftRec.delaysTouchesBegan == true || swipeRightRec.delaysTouchesBegan == true {
swipeLeftRec.delaysTouchesBegan = false
swipeRightRec.delaysTouchesBegan = false
self.view?.removeGestureRecognizer(swipeLeftRec)
self.view?.removeGestureRecognizer(swipeRightRec)
print("Touches should not be delayed")
}
I've tried it in and outside of the if statement, and used the removeGestureRecognizer just as a precaution, but I still get the same result.
Even if I get the print(Touches should not be delayed), ocassionally they will still be delayed and will not switch back, even after re-presenting my GameScene after my "Flappy Bird" dies.
I've even tried adding the above code to my update method and still no luck.
I ended up placing the code into my update function and it solved the problem. It works consistently now. I'm sure this isn't the best place to put the code, but it's the only place that solved the issue.
override func update(_ currentTime: TimeInterval) {
if gameState == .startScreen {
swipeLeftRec.delaysTouchesBegan = true
swipeRightRec.delaysTouchesBegan = true
} else {
swipeLeftRec.delaysTouchesBegan = false
swipeRightRec.delaysTouchesBegan = false
}