2017-06-22 21 views
2

所以我遇到了一个我的SKEmitterNodes问题。我有一个SKSpriteNode,当触摸打开一个新的场景。这里是触摸代码开始:问题与SKEmitterNode?

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") 
      }}} 

在这一点上,一切都很好。当我将SKEmitterNode添加到场景时,出现了我的问题。发射器是星场效应,所以从场景的顶部到底部都有小点。一旦我添加这个发射器,我的按钮停止工作!

我试过一切降低产卵率,降低zPosition,但似乎没有工作。

如果您有任何建议,请让我知道。谢谢!

-Matt

回答

1

显然粒子系统正在被命中测试,而不是按钮检测。您可以使用节点(在:)获得该点所有节点的列表(而不是第一个节点)。然后你需要迭代或过滤该数组,并找出哪些节点是按钮。

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") 
    } 
+1

是!这工作! –