ios - Spawn enemies does not work good in spritekit / swift -
i want sequentially spawn enemies. wrote code that, can't understand why 2 of first enemies appears on right position, , rest of them spawn on wrong positions. here code:
func adddoor() { in 0...1 { let doorsprite = skspritenode(imagenamed: "door") doorsprite.name = "door\(i)" if == 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() { 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 }
for 1 thing, logic in both if
, else
blocks same.
i'm not quite sure you're trying here or how randdoor
variable supposed used, whole if - else
block isn't doing anything.
Comments
Post a Comment