I'm trying to fetch all children (identified by a name) of a GameScene in order to rotate some SKSpriteNode synchronously with the device.
For the moment I have this code:
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)
}
}
}
}
}
How can I get the child i? Is it the good way to do it? Otherwise, I can handle an array list with all my children added to the GameScene...