Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tvOS UIPress gestures not being recognized in subclass of SKScene

Tags:

swift

tvos

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?

like image 948
Bryce Meyer Avatar asked Sep 19 '15 05:09

Bryce Meyer


1 Answers

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!

like image 154
user3481651 Avatar answered Sep 23 '22 20:09

user3481651