Why am I getting no nodes in touchesBegan?

I am using touchesBegan within the class of MyBall, which is an SKSpriteNode, itself, so it gets called when I click on the MyBall, and I do go past the guard line.

However, when I print the touchedNodes.count I get zero, and touchedNodes does have a raw value.

If anyone knows what I'm doing wrong here....or missing?

p.s. I tried doing this from GameScene itself, but touchesBegan was never received.

Code Block
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let location = touch.location(in: self)
let touchedNodes = self.nodes(at: location)
print ("\(touchedNodes.count)")
//combine next two lines
for node in touchedNodes.reversed(){
var thisBall = node as? MyBall
if thisBall!.nodeType == "ball" {
self.currentNode = node
}
}
}

Answered by SergioDCQ in 629059022
Found the answer below. It turns out that I have to set everyone else's

Code Block
isUserInteractionEnabled = false

Except the GameScene's to true

Now it works
Controlling User Interaction on Nodes
Please check the doc of nodes(at:).

nodes(at:)

Returns an array of all visible descendants that intersect a point.

If your MyBall does not have any descendants, nodes(at:) returns an empty Array.

I guess you want to do something like this:
Code Block
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
(self.scene as? GameScene)?.currentNode = self
}

(To make this work, you need to define the property currentNode in your GameScene.)



p.s. I tried doing this from GameScene itself, but touchesBegan was never received.

It is not clear what you have tried, but there might be something wrong if you could not make touchesBegan work.
Posted in the wrong spot...sorry

I tried your suggestion in both MyBall (SKSpriteNode) and GameScene (SKScene)

Code Block
class GameScene: SKScene, SKPhysicsContactDelegate
{
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
(self.scene as? GameScene)?.currentNode = self
}
...
}

But all I get is the following error:

Value of type 'GameScene' has no member 'currentNode'




I tried your suggestion in both MyBall (SKSpriteNode) and GameScene (SKScene)

I wrote my code for MyBall not for GameScene.
self in MyBall represents an instance of MyBall. So, the code (self.scene as? GameScene) does not make sense in GameScene.

But all I get is the following error: 

Please do not ignore the very important part of my suggestion.

(To make this work, you need to define the property currentNode in your GameScene.)

Code Block
class GameScene: SKScene, SKPhysicsContactDelegate {
var currentNode: MyBall? //<- This might be `: SKNode` or `:SKSpriteNode`, based on what you want to do with `currentNode`
//...
}


If you had shown more context, I could have shown you a more easy code to apply to your project.
I actually see what's wrong, but am unsure how to fix it, or if I can do it the way I like.

I have my:

GameScene zPosition = -1

SKScene
// takes up entire scene

BallHolder zPosition = 0

SKSpriteNode
// base holder for all the balls, so I just have to move it to move all the balls at once (child of GameScene)

MyBall zPosition = 1

SKSpriteNode
// multiple balls (each child of BallHolder)

It turns out, if I click on the MyBall I get touchesBegan,
if I click on the BallHolder, it gets the touchesBegan
and if I touch an individual ball, it gets the touchesBegan

I would like to receive all touchesBegan through GameScene, then go through of the array of all nodes, in reverse, at that location.

So my question should be...How do I make it so that GameScene handles all the touches_Began?


How do I make it so that GameScene handles all the touchesBegan?

That's completely different thing than you described in your opening post.
I recommend you to start a new thread dedicated for the issue, with enough info to solve.

That's completely different thing than you described in your opening post.
I recommend you to start a new thread dedicated for the issue, with enough info to solve.

I only just realized I asked the wrong question, so abandoning this post. Wish they allowed you to delete or close a thread as wrong, or wrong question, etc.

It is not clear what you have tried, but there might be something wrong if you could not make touchesBegan work.

Code Block
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
super.touchesBegan(touches, with: event)
guard let touch = touches.first else { return }
print ("\(touches.count)")
let location = touch.location(in: self)
}


Well technically it does, but in only each class of the item touched. If it's the GameScene that is touched, it get's the touchesBegan. If the MyBall nodes touched. it gets the touchesBegan. Am trying to make it so that ALL touchesBegan go through GameScene.

By the way I opened this in a new question once I understood what the problem was (originally I thought it the MyBall class wasn't getting it, because I was always looking in GameScene.)

Is there a way to make one SKScene receive all touch notifications?
Accepted Answer
Found the answer below. It turns out that I have to set everyone else's

Code Block
isUserInteractionEnabled = false

Except the GameScene's to true

Now it works
Controlling User Interaction on Nodes
Why am I getting no nodes in touchesBegan?
 
 
Q