Jump to content

Generate objects at dynamic intervals


Recommended Posts

 I've been at this for hours. I'm trying to make an endless runner game. The idea is that the player is standing still, but the stage is moving, giving the illusion that the player is moving. I want to generate some obstacles at a random distance from each other. Like so:




At some point maybe I'd like to set the minimum space between each obstacle but I'm far from there.


Here's what I got for now:

create() {   spawn = 1000;   timer = game.time.events.loop(spawn, this.addObstacle, this);},update() {   this.updateTimer();},updateTimer: function() {        random = Math.floor((Math.random() * 500) + 100);        spawn = 1000;        timer = game.time.events.loop(spawn, this.addObstacle, this);     }....

I'm getting Uncaught TypeError: Cannot read property 'reset' of null because I have 10 obstacles and they all pop up on screen at the same time. And there's no 'dead' obstacle for getFirstDead().


Another thing I tried was this:

update() {   random = Math.floor((Math.random() * 500) + 100);   // I think this causes lags and it doesn't feel right   this.updateTimer(random);},updateTimer: function(r) {   timer.delay(r);}....

But here timer.delay is not a function.


So far I can spawn obstacles at even intervals with just this line:

timer = game.time.events.loop(<Integer>, this.addObstacle, this);


P.S. And what is 'this' in this.updateTimer()? I thought it was 'game'. :huh: I see it constantly in the API. Guess the creators know pretty darn well its usage. 

Link to comment
Share on other sites

I find this strategy quite weird, using time loops for obstacle generation. I don't know exactly what your game is about but it sounds strange : if the player doesn't move, or moves back to start point, you will still generate obstacles ahead. Does that make sense?


What I would rather do is check that the player has moved forward, and check a random number to know if the tile at player.x + distance will have an obstacle. No timers. This way, if the player stands still or if he runs very fast, there will be no more obstacle than necessary, and no less than required.


Also, regarding the "this". this is a JS keyword, and references the closest parent. So in a Game State, for example, this will reference the Game State. But for convenience, Game States (and maybe a few other objects) are being injected the game into their scope, so that this also refers to the game. That's just because we use the game object for almost anything, so we avoid always writing this.game.doStuff(), we just write this.doStuff(). This can be confusing at first, but it will come very handy.

Link to comment
Share on other sites

Figured it out!

var arr = [700, 1500, 2500];   // The values I chose update() {    var ranNum = mainState.pickRandom(arr);                if (game.time.now > obstacleTime) {            obstacleTime = game.time.now + ranNum;            this.addObstacle();    // 'this' is the game state        } } // The pickRandom function: pickRandom: function(a) {	return element = a[Math.floor(Math.random() * a.length)]; }
Link to comment
Share on other sites


  • Recently Browsing   0 members

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