Jump to content

Time events and spawn area


Recommended Posts

Hi, I'm trying to make a timed event in which zombies will respawn. I'm currently just using the one I got from the phaser site which is


game.time.events.repeat(Phaser.Timer.SECOND * 2, 10, createZombie, this);


But I want to the zombies to spawn faster after each event, so say after the first event happens the next event will happen in 1.8 seconds and the next at 1.6 seconds and so on.


I also want to control the area where the zombies spawn, so far I have.


function createZombie(){
var zombie = game.add.sprite(game.world.randomX, game.world.randomY, 'zombie');
game.physics.enable(zombie, Phaser.Physics.ARCADE);
zombie.body.collideWorldBounds = true;
I want the zombies to only spawn on the outsides of the map and not near the center where the player is located.
Thanks any help appreciated.
PS I'm a noob at this stuff so please try to explain it like im 5 years old.
Link to comment
Share on other sites

A quick solution that comes to my mind would be to give up the 'loop/repeat pattern' (which is very regular, and not appropriate for your game) and instead program an event to happen in the future (say 1.8s), which will call your createZombie() method. And in your createZombie() method, creating a new, same event to happen again, but in 1.6s instead, etc.


Create a reference somewhere to keep a track of the timer (say var eventTimer = 1.8; to start with, and decrement it each time you use it).

Link to comment
Share on other sites

Set the zombies position outside of the camera by using `game.camera.width` and `game.camera.height` and add (Math) '20, or 40, 60, 80 or whatever' to spawn them far away from the player.


You can also use:

function rand(min,max)
    return Math.floor(Math.random()*(max-min+1)+min);
and do game.camera.width-rand(1,300) to spread them out, outside of the camera's view (players)  sporadically.
For the timer, set a global variable to 2000 and each time you run the function to spread the monsters, reduce 200 from that variable.  Use that variable as the 2nd parameter in the setTimeout function
2000 ms = 2 second -> run function & do variable-=200, now 1800 ms, run function, now 1600 ms and so on
Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...