2016-04-24 66 views
0

我用enumerateChildNodesWithName命令給我所有的塊物理學的,就像這樣:如何讓一個孩子在使用enumerateChildNodesWithName之後做些什麼?

func findBlock(theName:String){ 

     self.enumerateChildNodesWithName("//*"){ 
     node, stop in 

      if node.name == theName{ 
       node.physicsBody?.categoryBitMask = physicsCategory.block 
       node.physicsBody?.collisionBitMask = physicsCategory.laser 
       node.physicsBody?.contactTestBitMask = physicsCategory.laser 
      } 
     } 
    } 

現在我只想要塊之一,當它被用激光打消失。但是,如果沒有讓所有其他街區同時消失,我一直無法做到這一點。
我試圖用這行代碼在didBeginContact找到哪個塊代表第一主體,將其取下:

if firstBody.categoryBitMask == physicsCategory.block && secondBody.categoryBitMask == physicsCategory.laser{ 

     let block = SKSpriteNode() 
     block.physicsBody = firstBody 
     block.alpha = 1 
     let byeBlock = SKAction.fadeOutWithDuration(0.5) 
     let gone = SKAction.removeFromParent() 
     let run = SKAction.sequence([byeBlock, gone]) 
     block.runAction(run) 
     self.removeChildrenInArray([laser]) 

    } 

但也結束了沒有工作。 請幫忙!提前致謝!

+0

當塊受到激光擊中時代碼是什麼?我假設你在'didBeginContact'中處理這個... – Whirlwind

+0

@Whirlwind它在'didBeginContact'中。起初,我嘗試使用相同的'enumerateChildNodesWithName'命令刪除該塊,但很明顯,它刪除了屏幕上的每個塊。最近,我試圖這行代碼: –

+0

如果firstBody.categoryBitMask == physicsCategory.block && secondBody.categoryBitMask == physicsCategory.laser { 設塊= SKSpriteNode() block.physicsBody = firstBody block.alpha = 1 讓byeBlock = SKAction.fadeOutWithDuration(0.5) 設消失= SKAction.removeFromParent() 設運行= SKAction.sequence([byeBlock,消失]) block.runAction(運行) self.removeChildrenInArray([激光]) } –

回答

0

我認爲你應該處理聯繫人,這樣if阻止你提供的工作正常。如果是這樣,這就是你想要的:

if firstBody.categoryBitMask == physicsCategory.block && secondBody.categoryBitMask == physicsCategory.laser{ 
{ 
    //Downcast it to SKSpriteNode or whatever your block is 
    if let block = firstBody.node as? SKSpriteNode { 

     //We don't want to run removing action twice on the same block 
     if block.actionForKey("removing") == nil { 

      block.alpha = 1 
      let fadeOut = SKAction.fadeOutWithDuration(0.5) 
      let remove = SKAction.removeFromParent() 
      block.runAction(SKAction.sequence([fadeOut, remove]), withKey: "removing") 
     } 

    } 
    //Downcast it to SKSpriteNode or whatever your laser is 
    if let laser = secondBody.node as? SKSpriteNode { 
     self.removeChildrenInArray([laser]) 
    } 

} 
+0

這工作完美!非常感謝你 –

相關問題