I want to sequentially spawn enemies. So wrote a code for that, but can't understand why just two of first enemies appears on right position, and the rest of them spawn on wrong positions. Here is the code:
func addDoor() {
for i in 0...1 {
let doorSprite = SKSpriteNode(imageNamed: "Door")
doorSprite.name = "door\(i)"
if i == 0 {
doorSprite.position = CGPointMake(self.frame.size.width / 2.0 - 40.0 - doorSprite.frame.size.width, self.frame.size.height - doorSprite.frame.size.height / 2.0)
} else {
doorSprite.position = CGPointMake(self.frame.size.width / 2.0 + 40.0 + doorSprite.frame.size.width, self.frame.size.height - doorSprite.frame.size.height / 2.0)
}
gameLayer.addChild(doorSprite)
}
}
func enemySpawn() {
for i in 0..<15 {
let sprite = Enemy(imageNamed: "Enemy")
sprite.name = "enemy\(i)"
let randDoor = Int(arc4random_uniform(2))
if randDoor == 0 {
if let door = gameLayer.childNodeWithName("door\(randDoor)") {
sprite.position = CGPointMake((door.position.x - door.frame.size.width / 2.0) + spawnPoint(UInt32(door.frame.size.width - sprite.frame.size.width)) + sprite.frame.size.width / 2.0, door.position.y)
}
} else {
if let door = homeLayer.childNodeWithName("door\(randDoor)") {
sprite.position = CGPointMake((door.position.x - door.frame.size.width / 2.0) + spawnPoint(UInt32(door.frame.size.width - sprite.frame.size.width)) + sprite.frame.size.width / 2.0, door.position.y)
}
}
}
enemiesLayer.runAction(SKAction.waitForDuration(NSTimeInterval(i)), completion: {
self.enemiesLayer.addChild(sprite)
})
}
}
func spawnPoint(num: UInt32) -> CGFloat {
let randomSpawn: CGFloat = CGFloat(arc4random_uniform(num))
return randomSpawn
}