TommyBs
TommyBs

Reputation: 9646

Overring touchesEnded in SKSpriteNode - check if any node in scene is under point

I've had to override touchesBegan and touchesEnded in an SKSpriteNode subclass that has children nodes (because I want to propagate a touch event on the child node)

touchesBegan is working fine, but the problem I'm having is in touchesEnded checking that the same sprite is still under a users finger.

No matter what I try I never get a reference to 'self' in the node list of places at that point

 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touched ended")
        for t in touches {
            let allNodes = nodes(at: t.location(in: self.scene!))
            print(allNodes)
        }
    }

Should t.location(in: self.scene!) return what I'm looking for? I've also tried self.scene?.view but this also returns an empty node list.

How could I go about checking that the touchesEnded touch was still on this SKSpriteNode? or indeed if they have moved their finger check if it's over an entirely different node? I guess I need to convert this back to the whole scene coordinates some how which I thought t.location(in: self.scene.view)) would do. My other thought was using some kind of delegate method on the main scene file, but would the touch.location be relative to the node that was tapped?

Upvotes: 1

Views: 183

Answers (1)

Fluidity
Fluidity

Reputation: 3995

You're checking nodes in self.. self here IS the subclassed parent node.. You should check for nodes in your main scene, which would show the subclassed node properly:

let allNodes = self.scene!.nodes(at: t.location(in: self.scene!))

You can also use the various convert and convertPoint methods to do this.

Upvotes: 1

Related Questions