我有一个球,我向空中射击:
它初始化如下:
func initBallNode() {
// TODO: Create ball node
ballNode = SKSpriteNode(imageNamed: "Ball1") // Create a ball
ballNode.zPosition = ZPositions.ball
let offsetFromCorner: CGFloat = 20 // Offset from bottom-left of screen
ballNode.position = CGPoint(x: frame.minX + ballNode.size.width / 2 + offsetFromCorner, y: frame.minY + ballNode.size.height / 2 + offsetFromCorner)
ballNode.physicsBody = SKPhysicsBody(circleOfRadius: ballNode.size.width / 2)
ballNode.physicsBody?.affectedByGravity = true
ballNode.physicsBody?.angularDamping = 1 // <-- Does not set a minimum-speed
addChild(ballNode)
}
如何降低球的最低速度/速度,使其减少滚动并提前完全停止?
有没有一个物理属性来处理这个问题,而不会影响对象的质量(使它更早出现)?
如果您有任何问题,请询问!
你可以增加friction
的physicsBody
。默认情况下,摩擦力为0.2,但介于0.0和1.0之间
这仍然不是一个答案。这个问题仍然存在,在原始答案下方的编辑中有所描述。
由于stored properties in Swift extensions not being allowed(在Objective-C中产生了一点点),这变得相当困难。但是,经过一些研究和测试,我提出了目前运作良好的解决方案:
import SpriteKit
// Key to access the stored properties
private var minimumSpeedAssociatedKey: UInt8 = 0
extension SKPhysicsBody {
// Minimum speed for each node
var minimumSpeed: CGFloat? {
get { return objc_getAssociatedObject(self, &minimumSpeedAssociatedKey) as? CGFloat }
set(newValue) { objc_setAssociatedObject(self, &minimumSpeedAssociatedKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) }
}
// Update the speed for each node
func updateSpeed() {
guard let safeMinimumSpeed = minimumSpeed else {
assert(false, "You have tried to update an object speed, without setting the minimum for the node: \"\(self.node?.description ?? "Unavailable")\".")
}
let currentSpeed = sqrt(pow(velocity.dx, 2) + pow(velocity.dy, 2))
if currentSpeed < safeMinimumSpeed && currentSpeed > 0 {
angularVelocity = 0
velocity = CGVector(dx: 0, dy: 0)
}
}
}
将它放在项目的Swift文件中。然后,您可以设置最低速度:
ballNode.physicsBody?.minimumSpeed = 30
然后,像这样覆盖更新函数:
override func didFinishUpdate() {
ballNode.physicsBody?.updateSpeed()
}
结果,球现在不再低于30
的速度,而是停止了。停球仍然几乎无法察觉,因为之前的球只是在5秒的时间内非常缓慢。这是我正在寻找的结果。
我希望这对人们有所帮助,如果你找到了更好的解决方案(或Apple添加了新的属性),请发表你自己的答案。 :)
编辑1:
我现在有了不同的地形。然而,当球击中山丘时,这会引起奇怪的物理现象。因此,这不是一个完全解决的问题。