SKSpriteNode’s child blocks mouse down events

Xcode 7 beta 4


I have a custom SpriteKit button class, which is derived from SKSpriteNode. I create a button in a scene by adding a SKSpriteNode and setting its custom class, then adding SKLabel as a child to display button text. My button class initialiser enables user interaction, thus allowing to catch mouse events. It works, when I click parts of the button which aren't covered with the label. But if I click the text on the label, event is lost.


I've checked the source code for DemoBots. There's a similar button class too: SKSpriteNode with custom class with text label as a child. It is too, loaded from the scene. But it works!


Can anybody explain me what's going on?

Any suggestions for workarounds which let me continue using the editor for designing buttons?

Replies

Can you show the code you're using to enable user interaction with the button?


If you're enabling interaction for the sprite node, then presumably you'll have to enable it for the label node too (or deal with clicks at a higher level that doesn't depend on the particular node structure of your game elements).

All of a sudden it started working and I cannot reproduce this behaviour anymore...


But here it is, the code:

class ButtonNode: SKSpriteNode {
  required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
       userInteractionEnabled = true
  }
  override func mouseDown(event: NSEvent) {
       print("Click")
  }
}


It really is that simple. According to documentation, you should set userInteractionEnabled only for the nodes you intend to handle events, i.e. the ones that have NSResponder or UIResponder methods, like mouseDown or touchesBegan. Thus, the label on top shouldn't have user interactions enabled.


Anyway, I'm really worried now why this bug which dogged me for the whole day suddenly disappears. What I did is that I fixed a bug I noticed in the code of my scene class, namely the call to super was missing in this override:

override func didMoveToView(view: SKView) {
    super.didMoveToView(view)
}


Okay, that could break something. But when I comment out that call to super again, the button keeps on working. The label is fully click transparent now, as it should be!


Probably this is some hidden issue in Xcode 7 beta 4 / El Capitan beta 5. And I can't submit the bug because I can't reproduce it anymore 😟

I don't know what happened, but it seems like the earlier behavior is what you would get if the label node had 'userInteractionEnabled' set to true. Since you (presumably) don't have a mouseDown override for that node, clicks on it would be silently swallowed.


Perhaps there is/was a bug in the sks editor that set the property incorrectly, but you did something that caused the sks file to be recompiled.