Espen Birk
Espen Birk

Reputation: 456

Detect tap only on main node/parent, not child nodes

I have a moving SKSpriteNode where i detect taps on the scene with TouchesBegan based on the nodes name. This worked perfectly, until i addeed a SKEmitterNode as a child to the moving node leaving a trail of circles behind the moving node.

My touches on the main/parent node is now detected when the SKEmitterNode particle trail is being touched.

How can i only detect touches on my main/parent spritenode, but not on the child nodes of this sprite?

When the sparks that ouside node1.size is tapped, why is "node1" returned in touchesbegan and not "emitter"?

Example GameScene.swift

 override func didMoveToView(view: SKView) {

    let node1 = SKSpriteNode(color: SKColor.redColor(), size: CGSizeMake(400, 300))
    node1.position = CGPointMake(size.width/2, size.height/2)
    node1.name = "node1"
    addChild(node1)

    let emitter = SKEmitterNode(fileNamed: "MyParticle.sks")
    emitter.name = "emitter"
    emitter.zPosition = -1
    node1.addChild(emitter)

}


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */
    let touch: UITouch = touches.anyObject() as UITouch
    let location = touch.locationInNode(self)

    let touchedNode = nodeAtPoint(location)

    println(touchedNode.name)
}

Upvotes: 1

Views: 835

Answers (1)

Espen Birk
Espen Birk

Reputation: 456

Solved it setting the SKEmitterNode´s targetnode property to another node other than the one im detecting taps on.

https://developer.apple.com/library/mac/Documentation/SpriteKit/Reference/SKEmitterNode_Ref/index.html#//apple_ref/occ/instp/SKEmitterNode/targetNode

emitter.targetNode = self.scene

Upvotes: 1

Related Questions