【发布时间】:2016-10-08 15:31:20
【问题描述】:
我的场景编辑器中有一个名为“redsprite”的精灵,具有一些物理属性。我真的很想用这个精灵做下一个。
我想“复制”这个精灵,保留他的物理属性,但有一个新的位置。
let sprite:SKSpriteNode = (self.childNode(withName: "redsprite") as! SKSpriteNode).copyWithPhysicsBody()
sprite.position = CGPoint(x:100,y:100)
self.addChild(sprite)
我使用 SKSpriteNode 扩展来复制精灵。
extension SKSpriteNode {
func copyWithPhysicsBody()->SKSpriteNode {
let node = self.copy() as! SKSpriteNode
node.physicsBody = self.physicsBody
return node
}
}
我有这个经典错误:
Terminating app due to uncaught exception 'Cant add body, already exists in a world', reason: 'Cant add body <SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> name:'redsprite' texture:['nil'] position:{100, 100} scale:{1.49, 1.40} size:{149.15391540527344, 140.06228637695312} anchor:{0.5, 0.5} rotation:0.00], already exists in a world'
我理解错误,很清楚,但我没有找到避免它的方法。
【问题讨论】:
标签: swift sprite-kit skspritenode skphysicsbody