I have a problem with my enemies. I made 5 of them and each one is coming to a scene independently of the other. But thing is - they are pretty much the same except for looks. They likes to spawn in groups, one on another because they have one random earing mechanism for everyone. I tried to use SKConstraint to make a gap between them, but it's not working for me.
So I thought about different approach: I want to use something, like an empty node to represent enemy before it shows up on a scene (they are like lower pipes in flappy bird coming from the right side to the left). And before they will appear on screen I want my game to randomly choose between 5 nodes of them and perform correct animation. Because of that my group-appearing issue would be solved.
How could I do it?
For now I use their node. I'll provide code if it helps:
var robot = SKSpriteNode()
let robotAtlas = SKTextureAtlas(named: "robot")
var robotArray = [SKTexture]()
robotArray.append(robotAtlas.textureNamed("robot0"));
robotArray.append(robotAtlas.textureNamed("robot1"));
then I'm applying physicBodies to them
robot = SKSpriteNode(texture: robotArray[0]);
robot.position = CGPointMake(CGRectGetMaxX(self.frame), CGRectGetMidY(self.frame) - 138)
self.robot.name = "robot"
self.addChild(robot)
How can I do that or maybe there are other ways to do such thing?
For now, that's my scheme: This is the random function:
func random() -> UInt32 {
var range = UInt32(60)..<UInt32(200)
return range.startIndex + arc4random_uniform(range.endIndex - range.startIndex + 1)}
I have a custom class for appearance:
class EnemyAppear {
var nowAppear = false
var waitToAppear = UInt32(0)
var currentInterval = UInt32(0)
init(nowAppear:Bool, waitToAppear:UInt32, currentInterval:UInt32) {
self.nowAppear = nowAppear
self.waitToAppear = waitToAppear
self.currentInterval = currentInterval }
func shouldRun() -> Bool {
return self.appearInterval > self.waitToAppear }
Then I have a status to track enemies:
var enemyStatus:Dictionary<String,EnemyAppear> = [:]
enemyStatus["robot"] = EnemyAppear(nowAppear: false, waitToAppear: random(), currentInterval: UInt32(0))
enemyStatus["drone"] = EnemyAppear(nowAppear: false, waitToAppear: random(), currentInterval: UInt32(0))
And in Update function I have function that moves them:
func enemyRun() {
for(enemy, enemyAppear) in self.enemyStatus {
var thisPet = self.childNodeWithName(enemy)!
if enemyAppear.shouldRun() {
enemyAppear.waitToAppear = random()
enemyAppear.currentInterval = 0
enemyAppear.nowAppear = true
}
if enemyAppear.nowAppear {
if thisPet.position.x > petMaxX {
thisPet.position.x -= CGFloat(self.groundSpeed)
}else {
thisPet.position.x = self.originalPetPositionX
enemyAppear.nowAppear = false
self.score++
self.scoreText.text = String(self.score)
}
}
}
All I need is to set distance between enemies.