Particularly i think that it's not a good idea to put this code on the update func as it will run your code before render every frame...so it will run it like 60 times per second.
A better solution is create a Function to spawn enemies and call it with a timer. First of all declare a variable in your class to handle the enemy count and the timer:
var enemies = 0
var enemyTimer = Timer()
func spawn(){
enemies += 1
}
to start the timer:
enemyTimer = Timer.scheduledTimer(timeInterval: YOUR TIME INTERVAL TO SPAWN ENEMIES, target: self, selector:
Start the timer when the game starts, invalidate it when the game ends and when you move to another scene!
This timer will call the spawn func every "n" seconds!
About the out of screen you may use the update func:
if scene?.frame.contains(enemyNode.position){
}else{
enemyNode.removeFromParent()
}
UnsafePointer<EnemyClass>
with 4x+ the capacity of the active enemies, and keep two values:offset
andcount
. Then, you'll only need to move all theclass
references when thecount
value exceeds the capacity of the pointer. This approach is of course not limited to SpriteKit, but I can't say if it helps your specific case. – Andreas detests censorship