我正在使用Swift和Sprite Kit在XCode Beta 6上开发游戏。为了检测所有节点是否处于休眠状态,我检查它们的physicsBody.resting属性。在update方法中,我打印出结果。
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var hero:SKSpriteNode!
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, 0)
self.physicsWorld.contactDelegate = self
self.physicsBody = SKPhysicsBody(edgeLoopFromRect:self.frame)
hero = SKSpriteNode(imageNamed: "Spaceship")
hero.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
hero.zPosition = 10.0
hero.physicsBody = SKPhysicsBody(circleOfRadius: hero.size.width/2)
hero.physicsBody.allowsRotation = false
hero.physicsBody.linearDamping = 0.5
self.addChild(hero)
}
override func update(currentTime: CFTimeInterval) {
if hero.physicsBody.resting {
println("resting")
} else {
println("moving")
}
}
}
我很惊讶,结果是这样的:
移动 休息 移动 (重复n次) 移动 休息
那么为什么英雄会移动,尽管我没有做任何事情。节点移动N次并休息一下,之后继续移动。
有人能解释这种行为吗?这是一个错误还是我漏掉了什么?提前谢谢。
sqrtf
是不必要的,因为你可以将结果与verySmallValue
的平方进行比较(这可以预先计算)。 - James