Title: How to detect a node, acting as a button, in a SpriteKit scene
Tags: ios;swift
Question: I have a SpriteKit scene. The entire scene is touch-disabled, using the following line of code:
self.view?.isUserInteractionEnabled = false
The scene is touch-disabled in order to prevent the user from interfering with a few nodes (that will later become interactive) too early on in the process.
However, I want to add a button to the scene in the form of a SpriteKit node. This button should always be active.
There are a few approaches here on StackOverflow, but I'm really unsure as to how to implement them.
First approach:
Something that involves subclassing the UIView and then applying a "hittest". I don't have a class for this, and I don't know whether I should; and I don't know how I would go about doing that in this context.
Second approach:
I've tried disabling nodes individually. The code that I used to do that replaced the line above with these lines:
for child in fgNode.children {
child.isUserInteractionEnabled = false
for child in bgNode.children {
child.isUserInteractionEnabled = false
The fgNode/bgNode are parents to all nodes in the scene. The nodes are subclassed and have user interaction activated by default, which this code attempts to disable, but it doesn't work to do this when the scene is set-up.
Comment: SKNodes are not supposed to be buttons. The better approach would be using an UITapGestureRecognizer on top of the SKNode.

Comment: @LouFranco have edited above with more information.

Comment: @ThiagoAM If that were used, wouldn't it still be in the view and disabled?

Comment: "I've tried disabling nodes individually" -- what did you do to do that?

Comment: What code are you using to detect that an SKNode was hit?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:06/01/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><swift>', 'Title': 'How to detect a node, acting as a button, in a SpriteKit scene', 'ViewCount': 52, 'id': 44311459, 'user_AboutMe': None, 'user_AccountId': None, 'user_DisplayName': None, 'user_Reputation': None, 'user_Views': None, 'user_id': None}}