看起来几乎令人难以置信,但是,
let pb = model.physicsBody?.copy() as! SKPhysicsBody
print("orig .. \(model.physicsBody!.linearDamping)")
print("pb .. \(pb.linearDamping)")
他们不一样。不,真的。
这太离奇了,我一定做错了。我做错了什么?
除了手动复制所有属性,并在进化过程中永远维护代码,如何做到这一点?
如果有人需要,这是一种欺骗性质的功能,以节省打字。 (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会更好,因为,整个事情都是一个棘手的问题,而且只是明确表达可能更好下一位工程师的缘故。对于另一个人而言,游戏对象的质量非常普遍,所以,这很好。)
答案 0 :(得分:1)
它与SKPhysicsBody
成为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来测试它是否有效。