2015-04-01 53 views
0

我試圖獲取GameScene的所有孩子(通過名稱標識)以便與設備同步旋轉某個SKSpriteNode。獲取GameScene的所有孩子

目前我有這樣的代碼:

override func update(currentTime: CFTimeInterval) { 
    if manager.deviceMotionAvailable { 
     manager.deviceMotionUpdateInterval = 0.01 
     manager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()) { 
      [weak self] (data: CMDeviceMotion!, error: NSError!) in 
      let rotation = atan2(data.gravity.x, data.gravity.y) - M_PI 

      for (var i = 1 ; i < self?.children.count ; i++){ 
       var child = ??? // How can I get the child i 
       if (child.name == 'Monster'){ 
        let action = SKAction.rotateByAngle(CGFloat(DegreesToRadians(rotation)), duration:0) 
        child.runAction(action) 
       } 
      } 
     } 
    } 
} 

我怎樣才能得到孩子嗎?這是做這件事的好方法嗎?否則,我可以處理我所有的孩子加入到GameScene數組列表...

回答

3

在SWIFT,您可以通過使用下面的方法具有特定名稱的所有子節點枚舉:

enumerateChildNodesWithName("Monster", usingBlock: { 
    let action = SKAction.rotateByAngle(CGFloat(DegreesToRadians(rotation)), duration:0) 
       child.runAction(action) 
} 

文檔: https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKNode_Ref/index.html#//apple_ref/occ/instm/SKNode/enumerateChildNodesWithName:usingBlock

+0

完美!謝謝:)我也使用這個鏈接http://stackoverflow.com/questions/24213436/how-to-use-enumeratechildnodeswithname-with-swift-in-spritekit正確使用enumerateChildNodesWithName。 – Bogy 2015-04-01 23:14:26