Search code examples
swiftswift3sprite-kitskemitternode

Issue with SKEmitterNode?


So I am having an issue with one of my SKEmitterNodes. I have an SKSpriteNode that when touched opens a new scene. Here is the code in touches began:

 for touch: AnyObject in touches {
        let location = (touch as! UITouch).location(in: self)
        if let nodeName = self.atPoint(location).name {

            if nodeName == "playBox" || nodeName == "playButton" {
                buttonSound()
                pulse(playBox, scene: "GameScene")
            }else if nodeName == "shopBox" || nodeName == "shopButton"{
                buttonSound()
                pulse(shopBox, scene: "shop")
            }}}

At this point, everything works great. My issue occurs when I add my SKEmitterNode to the scene. The emitter is a starfield effect, so little dots are going from the top of the scene to the bottom. Once I add this emitter, my button stops working!

I have tried everything lowering the spawn rate, lowering the zPosition, but nothing seems to work.

Please let me know if you have any advice. Thanks!

-Matt


Solution

  • Clearly the particle system is being detected by the hit test instead of the buttons. You could use nodes(at:) to get the list of all the nodes in that point (instead of just the first one). Then you need to iterate or filter that array and find out which of these nodes are buttons.

    for touch: AnyObject in touches 
    {
        let location = (touch as! UITouch).location(in: self)
        let nodes = self.nodes(at:location)
    
        let filtered1 = nodes.filter{ $0.name == "playBox" || $0.name == "playButton" }
        let filtered2 = nodes.filter{ $0.name == "shopBox" || $0.name == "shopButton" }
    
        if let node = filtered1.first {
            buttonSound()
            pulse(playBox, scene: "GameScene")
        }
    
        if let node = filtered2.first {
            buttonSound()
            pulse(shopBox, scene: "shop")
        }