我正在构建一个 sprite 套件游戏,在我的更新方法中,我有一些检查节点速度的 if 语句。我遇到的问题是检查 x 和 y 速度是否等于 0 的语句。在游戏中,我正在检查 3 个球体的速度。现在,如果找到至少 1 个 x 和 y 速度等于 0 的球体,则此 if 语句得到满足 - 但我希望它仅在所有球体都满足该语句时才执行。
我该怎么做呢?提前致谢!
override func update(currentTime: CFTimeInterval) {
centerOrbs()
orbLayer.enumerateChildNodesWithName("player", usingBlock: { node, _ in
if let orb = node as? Orb {
orb.checkObjects()
let rotation1 = orb.physicsBody?.velocity.dy
let rotation2 = orb.physicsBody?.velocity.dx
orb.zRotation = atan2(rotation1!, rotation2!)
if (orb.physicsBody?.velocity.dx == 0 && orb.physicsBody?.velocity.dy == 0) {
self.compareOrbPositions()
}
if (orb.physicsBody?.velocity.dx != 0 || orb.physicsBody?.velocity.dy != 0) {
self.waitForOrbsToStop()
}
}
})
let count = String(moveCount)
moveCountLabel.text = NSString(string: "Moves: \(count)")
}
最佳答案
你应该在枚举循环中更新一个 bool 变量并检查结果 枚举完所有节点后:
var allVelocitiesAreZero = true
orbLayer.enumerateChildNodesWithName("player", usingBlock: { node, stop in
// ...
if (orb.physicsBody?.velocity.dx != 0 || orb.physicsBody?.velocity.dy != 0) {
allVelocitiesAreZero = false
stop.memory = true
}
})
if allVelocitiesAreZero {
// ...
}
stop.memory = true
导致枚举停止,因为在那种情况下
剩余的迭代不会再改变结果。
关于swift - 枚举 ChildNode WithName 的所有节点满足 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27098337/