Swift SpriteKit - "Attempted to add a SKNode which already has a parent"添加关节



我在卧床休息和背部手术恢复期间学习SpriteKit。我很感激你的帮助,帮助我理解为什么我会犯这个错误。

我有一个精灵"zombie1",我可以通过计时器调用它,它会一次又一次地繁殖,然后在屏幕上移动而不会出现问题,直到我尝试添加riotShield精灵。一旦我这样做了,一旦第二次调用func,并且第一个riotShield已经存在,它就会崩溃:

    func addZ1ArmoredShield()     {
    zombie1Mask = SKSpriteNode(texture: zombie1Texture)
    var TextureAtlas = SKTextureAtlas()
    var TextureArray = [SKTexture]()
    TextureAtlas = SKTextureAtlas(named: "ZombieArmored")
    for i in 1...TextureAtlas.textureNames.count{
        let Name = "armoredZombie_(i)"
        TextureArray.append(SKTexture(imageNamed: Name))
    }
    zombie1 = SKSpriteNode(imageNamed: TextureAtlas.textureNames[0] as String)
    zombie1.name = "monster"
    zombie1.position = CGPointMake(globalVariables.skScene.frame.size.width + 50, CGFloat(randomStartPosition()))
    let moveZombie1 = SKAction.moveToX(0, duration: spawnSpeed1()   )
    let removeZombie1 = SKAction.removeFromParent()
    let moveAndRemoveZombie1 = SKAction.sequence([moveZombie1, removeZombie1])
    zombie1.runAction(moveAndRemoveZombie1)       zombie1.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(TextureArray, timePerFrame: 0.1)))
    zombie1.physicsBody = SKPhysicsBody(texture: zombie1MaskTexture, size: zombie1Mask.size)
    zombie1.physicsBody!.dynamic = true
    zombie1.physicsBody!.affectedByGravity = false
    zombie1.physicsBody!.charge = 50
    zombie1.physicsBody!.usesPreciseCollisionDetection = true
    zombie1.physicsBody!.categoryBitMask = ColliderType.monster2HPContact.rawValue
    zombie1.physicsBody!.contactTestBitMask = ColliderType.bulletContact.rawValue|ColliderType.bottemEdge.rawValue|ColliderType.topEdge.rawValue|ColliderType.monster2HPContact.rawValue|ColliderType.heroTop.rawValue
    zombie1.physicsBody!.collisionBitMask = ColliderType.bottemEdge.rawValue|ColliderType.topEdge.rawValue
    zombie1.physicsBody!.fieldBitMask = ColliderType.monsterContact.rawValue
    zombie1.physicsBody!.mass = 2
    zombie1.physicsBody!.allowsRotation = false

    riotShield.name = "RiotShield"
    riotShield.color = SKColor.blackColor()
    riotShield.size = CGSizeMake(2, 25)
    riotShield.position = CGPoint(x: (zombie1.position.x + 80  ), y: (zombie1.position.y + 40 ))
    riotShield.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(2, 25))
    riotShield.physicsBody!.categoryBitMask = ColliderType.riotShieldContact.rawValue
    riotShield.physicsBody!.contactTestBitMask = ColliderType.bulletContact.rawValue
    riotShield.physicsBody!.dynamic = true
    riotShield.physicsBody!.affectedByGravity = false
    riotShield.physicsBody!.usesPreciseCollisionDetection = true

    let riotShieldJoint = SKPhysicsJointFixed.jointWithBodyA(zombie1.physicsBody!, bodyB: riotShield.physicsBody!, anchor: CGPoint(x: (zombie1.position.x + 10  ), y: zombie1.position.y + 10 ))


    globalVariables.currentScene.addChild(zombie1)
    globalVariables.currentScene.addChild(riotShield)
    globalVariables.currentScene.physicsWorld.addJoint(riotShieldJoint)


}//end function

我不明白的是,为什么我的僵尸1创建了一个SKSpritenode的实例,但我的riotShield被视为一个试图添加到同一个父对象的单个精灵。我读了很多关于这个错误的帖子,我想我理解原因(没有将riotShield作为一个例子添加)。我只是不明白这两个精灵和乳清之间的区别,当我以基本相同的方式添加它们时,它们的行为会有所不同。

我感谢你抽出时间和你能提供的任何帮助。

基于单亲问题,通过查看我添加"riotShield"节点的方式,我发现这就是我使用SKSpritenode作为shapenode的方式,这显然改变了它的行为。

通过添加如下所示的riotShield图像,一切都成功了。

riotShield.name = "RiotShield"
riotShield = SKSpriteNode(imageNamed: "riotShield1")
riotShield.position = CGPoint(x: (zombie1.position.x + 80  ), y: (zombie1.position.y + 40 ))
riotShield.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(2, 25))
riotShield.physicsBody!.categoryBitMask = ColliderType.riotShieldContact.rawValue
riotShield.physicsBody!.contactTestBitMask = ColliderType.bulletContact.rawValue
riotShield.physicsBody!.dynamic = true
riotShield.physicsBody!.affectedByGravity = false
riotShield.physicsBody!.usesPreciseCollisionDetection = true

感谢所有发布其他问题并回答我问题的人。

相关内容

  • 没有找到相关文章

最新更新