SpriteKit,复制物理体不会复制设置?
SpriteKit, copying a physics body does NOT copy the settings?
这似乎令人难以置信,但是,
let pb = model.physicsBody?.copy() as! SKPhysicsBody
print("orig .. \(model.physicsBody!.linearDamping)")
print("pb .. \(pb.linearDamping)")
它们不一样。真的没有。
这太奇怪了,我一定是做错了什么。我做错了什么?
除了手动复制所有属性,并随着代码的发展永远维护代码之外,还有什么方法可以做到这一点??
如果有人需要,这里有一个 dupe-qualities 函数,可以节省打字时间。 (这是 2017 年的 - 当然,随着 Apple 增加品质,它必须永远维护。)
extension SKSpriteNode
{
func dupe() -> Any {
// annoyingly, Apple does not provide a dupe function as you would use
// when spawning from a model.
let c = self.copy() as! SKSpriteNode
c.physicsBody = self.physicsBody!.copy() as? SKPhysicsBody
c.physicsBody?.affectedByGravity = self.physicsBody!.affectedByGravity
c.physicsBody?.allowsRotation = self.physicsBody!.allowsRotation
c.physicsBody?.isDynamic = self.physicsBody!.isDynamic
c.physicsBody?.pinned = self.physicsBody!.pinned
c.physicsBody?.mass = self.physicsBody!.mass
c.physicsBody?.density = self.physicsBody!.density
c.physicsBody?.friction = self.physicsBody!.friction
c.physicsBody?.restitution = self.physicsBody!.restitution
c.physicsBody?.linearDamping = self.physicsBody!.linearDamping
c.physicsBody?.angularDamping = self.physicsBody!.angularDamping
c.physicsBody?.categoryBitMask = self.physicsBody!.categoryBitMask
c.physicsBody?.collisionBitMask = self.physicsBody!.collisionBitMask
c.physicsBody?.fieldBitMask = self.physicsBody!.fieldBitMask
c.physicsBody?.contactTestBitMask = self.physicsBody!.contactTestBitMask
return c
}
}
(只是一个人的意见,我觉得最好不要覆盖 NSCopy,因为,整个事情是一个棘手的问题,为了下一位工程师的缘故,简单明了可能更好。这很常见"dupe" 将一个游戏对象的品质转换为另一个,所以,这没问题。)
这与 SKPhysicsBody
作为 class 到 PKPhysicsBody
的包装器有关
本质上,当您创建 SKPhysicsBody
的副本时,它会创建 PKPhysicsBody
的新实例,而不是它的副本。
要解决这个问题,您需要编写一个扩展来为您填充值:
extension SKPhysicsBody
{
override open func copy() -> Any {
guard let body = super.copy() as? SKPhysicsBody else {fatalError("SKPhysicsBody.copy() failed")}
body.affectedbyGravity = affectedByGravity
body.allowsRotation= allowsRotation
body.isDynamic= isDynamic
body.mass= mass
body.density = density
body.friction = friction
body.restitution = restitution
body.linearDamping = linearDamping
body.angularDamping = angularDamping
return body
}
}
请注意,这是我手写的,目前我没有 XCode 来测试它是否有效。
这似乎令人难以置信,但是,
let pb = model.physicsBody?.copy() as! SKPhysicsBody
print("orig .. \(model.physicsBody!.linearDamping)")
print("pb .. \(pb.linearDamping)")
它们不一样。真的没有。
这太奇怪了,我一定是做错了什么。我做错了什么?
除了手动复制所有属性,并随着代码的发展永远维护代码之外,还有什么方法可以做到这一点??
如果有人需要,这里有一个 dupe-qualities 函数,可以节省打字时间。 (这是 2017 年的 - 当然,随着 Apple 增加品质,它必须永远维护。)
extension SKSpriteNode
{
func dupe() -> Any {
// annoyingly, Apple does not provide a dupe function as you would use
// when spawning from a model.
let c = self.copy() as! SKSpriteNode
c.physicsBody = self.physicsBody!.copy() as? SKPhysicsBody
c.physicsBody?.affectedByGravity = self.physicsBody!.affectedByGravity
c.physicsBody?.allowsRotation = self.physicsBody!.allowsRotation
c.physicsBody?.isDynamic = self.physicsBody!.isDynamic
c.physicsBody?.pinned = self.physicsBody!.pinned
c.physicsBody?.mass = self.physicsBody!.mass
c.physicsBody?.density = self.physicsBody!.density
c.physicsBody?.friction = self.physicsBody!.friction
c.physicsBody?.restitution = self.physicsBody!.restitution
c.physicsBody?.linearDamping = self.physicsBody!.linearDamping
c.physicsBody?.angularDamping = self.physicsBody!.angularDamping
c.physicsBody?.categoryBitMask = self.physicsBody!.categoryBitMask
c.physicsBody?.collisionBitMask = self.physicsBody!.collisionBitMask
c.physicsBody?.fieldBitMask = self.physicsBody!.fieldBitMask
c.physicsBody?.contactTestBitMask = self.physicsBody!.contactTestBitMask
return c
}
}
(只是一个人的意见,我觉得最好不要覆盖 NSCopy,因为,整个事情是一个棘手的问题,为了下一位工程师的缘故,简单明了可能更好。这很常见"dupe" 将一个游戏对象的品质转换为另一个,所以,这没问题。)
这与 SKPhysicsBody
作为 class 到 PKPhysicsBody
本质上,当您创建 SKPhysicsBody
的副本时,它会创建 PKPhysicsBody
的新实例,而不是它的副本。
要解决这个问题,您需要编写一个扩展来为您填充值:
extension SKPhysicsBody
{
override open func copy() -> Any {
guard let body = super.copy() as? SKPhysicsBody else {fatalError("SKPhysicsBody.copy() failed")}
body.affectedbyGravity = affectedByGravity
body.allowsRotation= allowsRotation
body.isDynamic= isDynamic
body.mass= mass
body.density = density
body.friction = friction
body.restitution = restitution
body.linearDamping = linearDamping
body.angularDamping = angularDamping
return body
}
}
请注意,这是我手写的,目前我没有 XCode 来测试它是否有效。