BrunoHautenfaust Posted November 5, 2015 Share Posted November 5, 2015 Hi! 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'. I see it constantly in the API. Guess the creators know pretty darn well its usage. Link to comment Share on other sites More sharing options...
Skeptron Posted November 6, 2015 Share Posted November 6, 2015 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. BrunoHautenfaust 1 Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 6, 2015 Author Share Posted November 6, 2015 Sorry. I should've mentioned the type of game. I updated my top comment. It's an endless runner with a scrolling stage. The player can only jump.I was following this tutorial. And I'm using the addOnePipe() logic for generating obstacles. Ah, the GameState. Forgot about that. Link to comment Share on other sites More sharing options...
BrunoHautenfaust Posted November 7, 2015 Author Share Posted November 7, 2015 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 More sharing options...
jmp909 Posted November 8, 2015 Share Posted November 8, 2015 Phaser already has a random array pick function by the way game.rnd.pick http://phaser.io/docs/2.4.4/Phaser.RandomDataGenerator.html#pick BrunoHautenfaust 1 Link to comment Share on other sites More sharing options...
Recommended Posts