I am trying to respond to a press in a subclass of SKScene. I can override the responder pressesEnded inside of my ViewController just fine, but when I move my pressesEnded override into my SKScene sub class I no longer receive any calls.
Below is my pressesEnabled override, which works as intended inside of ViewController
override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
print("press")
}
Anybody know how to receive button presses inside of a SKScene?
You need to forward your press events from your ViewController to the SKScene, like so...
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
gameScene.pressesBegan(presses, withEvent: event)
}
override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
gameScene.pressesEnded(presses, withEvent: event)
}
Then in gameScene (SKScene) do something like:
override func pressesBegan(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
for press in presses {
switch press.type {
case .UpArrow:
print("Up Arrow")
case .DownArrow:
print("Down arrow")
case .LeftArrow:
print("Left arrow")
case .RightArrow:
print("Right arrow")
case .Select:
print("Select")
case .Menu:
print("Menu")
case .PlayPause:
print("Play/Pause")
}
}
}
override func pressesEnded(presses: Set<UIPress>, withEvent event: UIPressesEvent?) {
print("Presses Ended.")
}
The "Arrow" presses are sent when the user taps the edges of the touchpad, but aren't recognized in the simulator. "Select" is sent when the center of the touchpad is tapped.
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With