【问题标题】:How to duplicate a sprite with physics properties then add it to the scene?如何复制具有物理属性的精灵然后将其添加到场景中?
【发布时间】: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


    【解决方案1】:

    您正在获取原始主体并将其放在 2 个节点上。你不能那样做。

    据我所知,physicsBody 现在在复制命令中被复制(并非总是如此)。所以你应该不需要把原来的身体转移过来。如果它不复制,那么做:

    node.physicsBody = self.physicsBody.copy()

    【讨论】:

    • 难以置信,它不会复制physicsBody的设置,当你做一个深拷贝时,它似乎。 (2017 年末)多么可笑。
    猜你喜欢
    • 1970-01-01
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多