SpriteKit isUserInteractionEnabled=false Blocks Touches

458 views Asked by At

I am building a SpriteKit game. The game scene is the parent of a transparent layer above it whose role is to display occasional messages to the player. I want this layer to be transparent and inert most of the time, and of course, never receive touches. As such, I have isUserInteractionEnabled set to false. However, when I add this later to the scene, it blocks all touches below it. What gives?

Edit: the parent of the MessageLayer is the game scene, and the game scene also has isUserInteractionEnabled = false, so I do not believe that the MessageLayer is inheriting the behavior.

Here is my code:

import Foundation
import SpriteKit

class MessageLayer: SKSpriteNode {

    init() {

        let size = CGSize(width: screenWidth, height: screenHeight)
        super.init(texture: nil, color: UIColor.blue, size: size)
        self.isUserInteractionEnabled = false

        self.name = "MessageLayer"
        alpha = 0.6
        zPosition = +200
    }

    override init(texture: SKTexture!, color: UIColor, size: CGSize)
    {
        super.init(texture: texture, color: color, size: size)
    }

    required init(coder aDecoder: NSCoder) {
        super.init(texture: nil, color: UIColor.clear, size: CGSize(width: 100, height: 100))
    }

    // MARK: - Functions

    func alphaUp() {
        alpha = 0.6
    }

    func alphaDown() {
        alpha = 0.0
    }

}
1

There are 1 answers

3
Fluidity On

Just relay the touches to the scene / other nodes you want to do stuff with.

class TransparentNode: SKSpriteNode {

  init(color: SKColor = .clear, size: CGSize) {
    super.init(texture: nil, color: color, size: size)
    isUserInteractionEnabled = true
  }

  // touches began on ios:
  override func mouseDown(with event: NSEvent) {
    // relay event to scene:
    scene!.mouseDown(with: event)
  }

  required init?(coder aDecoder: NSCoder) { fatalError() }
}


class GameScene: SKScene {

  lazy var transparentNode: SKSpriteNode = MyNode(size: self.size)

  // touches began on ios:
  override func mouseDown(with event: NSEvent) {
    print("ho")
  }

  override func didMove(to view: SKView) {
    addChild(transparentNode)
    isUserInteractionEnabled = false
  }
}

And here you can see that the invisible node is sending events properly:

enter image description here