mercredi 23 septembre 2015

AS3 - Passing a function with variable through a timer

basically, I have a function called setEnemies, it spawns the number of enemies I decide like this:

public function Main()

Here's the setEnemies:

private function setEnemies(numbers:Number):void
        for (var i:int = 0; numbers > i; i++)
        _Enemy = new Enemy(_player.x, _player.y);       
        addChild(_Enemy.rocket); // add enemy rocket to stage   
        _Enemy.isAlive = true;



So far it works.

Well, when the enemy dies I want the game to wait for 1 sec and then spawn 1 more Enemy, like this:

var spawnTimer:Timer; // Spawn enemy timer.
spawnTimer = new Timer(1000, 1); 
spawnTimer.addEventListener(TimerEvent.TIMER, setEnemies(1));

But I get the following error:

"Implicit coercion of a value of type void to an unrelated type Function. spawnTimer.addEventListener(TimerEvent.TIMER, setEnemies(1));"

Why does it behave like that? and sure, I can probably find another method for it to wait a second and spawn, but this way seems to me the most natural way. Thank you for you time.

Aucun commentaire:

Enregistrer un commentaire