I'm learning SpriteKit while laid up and recovering from back surgery. I appreciate your help in helping me understand why I'm getting this error.
I have a sprite "zombie1" that I can call via timer and it spawns over and over before moving across the screen without an issue until I try to add the riotShield Sprite. Once I do this it crashes as soon as the func is called the 2nd time and the first riotShield is already present:
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
What I don't understand is how come my zombie1 creates an instance of the SKSpritenode but my riotShield is seen as a singular sprite trying to be added to the same parent. I've read a ton of posts about this error, and I think I understand the cause (riotShield not being added as an instance). I just don't understand the difference between the two sprites and whey they behave differently when I'm adding them (to me) essentially the same way.
I appreciate your time and any help you may provide.