Swift SpriteKit - “添加联合

时间:2016-04-01 00:19:49

标签: sprite-kit swift2 skphysicsjoint

我正在学习SpriteKit,同时从背部手术中恢复过来。感谢您的帮助,帮助我理解为什么会收到此错误。

我有一个精灵“zombie1”,我可以通过计时器调用它,它一遍又一遍地产生,然后在屏幕上移动而没有问题,直到我尝试添加riotShield Sprite。一旦我这样做,一旦第二次调用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

我不明白的是我的zombie1如何创建SKSpritenode的实例,但我的riotShield被视为一个奇异的精灵,试图添加到同一个父级。我已经阅读了大量有关此错误的帖子,我想我理解原因(riotShield没有被添加为实例)。我只是不明白两个精灵和乳清之间的区别,当我加入它们(对我来说)时,它们的表现方式基本相同。

感谢您的时间和任何帮助。

1 个答案:

答案 0 :(得分:0)

根据单亲问题并查看我添加“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

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