3
votes

I have an SCNScene for a 3D game I am building. I have positioned a HUD over this scene using SCNScene.overlaySKScene.

In order to receive taps on my 3D scene, I use a gesture recogniser and hitTest and this works fine.

I am trying to determine the SKSpriteNodes that I have added to the HUD SKScene on taps now. This does not work with the gesture recogniser.

Having looked at the WWDC 2014 Banana sample, I don't think I am doing anything all that different in my use of the alternative touchesBegan method (below).

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    println("touchesBegan")

    let scnView = self.view as SCNView
    let skScene = scnView.overlaySKScene
    let touch: AnyObject? = touches.anyObject()
    let p = touch?.locationInNode(skScene)
    let touchedNode = skScene.nodeAtPoint(p!)

    println("touchedNode \(touchedNode.name)")

    super.touchesBegan(touches, withEvent: event)
}

The 2nd println is finding a nil result.

I create by HUD:

    scnView.overlaySKScene = SKScene(fileNamed: "HUDScene.sks")
    scnView.overlaySKScene.name = "HUD"
    scnView.overlaySKScene.delegate = self

I then add a root SKNode to this scene

var hudRoot = SKNode()

And add SKSpriteNodes with userInteractionEnabled=true to this hudRoot.

touchesBegan is being called, but nil is returned when touching the SKSpriteNodes. Interestingly, if I touch outside of the SKSpriteNodes and NOT on any 3D nodes, then it returns the hudRoot node even though that is defaulted to userInteractionEnabled=false.

Now that I have checked my implementation with Apple's own Bananas example, I am without any remaining ideas.

1
Did you get an answer to this? Did you try this: forum.yourwebapp.mobi/… - Aviad Ben Dov

1 Answers

1
votes

Try changing:

let p = touch?.locationInNode(skScene)
let touchedNode = skScene.nodeAtPoint(p!)

To:

let p = touch?.locationInNode(hudRoot)
let touchedNode = hudRoot.nodeAtPoint(p!)

The reason I suggest this is because the nodes you're trying to interact with are children of hudRoot and therefore their positions are in hudRoots coordinate system. The touch location you're currently getting is in the skScene's coordinate system.

If the above doesn't solve your problem try logging the touch location to the console and comparing what you're getting with what you're expecting.

Hopefully this helps or at least gives you a hint on what your problem may be.