acott Posted December 18, 2014 Share Posted December 18, 2014 Hey All, I recently started playing with Phaser to make a small christmas game. I'm completely new to this and my game comprises of multiple snippets taken from here and there. It's basically a side scroller where santa drops presents in to chimneys. I have the houses starting from the right to left they are created on a timer game.time.events.loop(1500, addRowOfHouses, this); The addRowOfHouses function is below ( this code was taken from a flappy bird clone )function addOneHouse(x,y) { var house = houses.getFirstDead(); house.reset(x, y); house.body.velocity.x = -200; house.checkWorldBounds = true; house.outOfBoundsKill = true;} function addRowOfHouses() { var houseSpacing = Math.floor(Math.random() * 5) + 1; for (var i = 0; i < 1; i++) if (i != houseSpacing && i != houseSpacing + 1) addOneHouse(790, i * 1 + 400); }The code makes a house every 1.5 seconds, so it is continuous. I wanted to change this to have it randomly create them between every 0.8 - 1.5 seconds. I thought I could do this using Math.random in the timer but that didn't work. I actually think the method I'm using to generate the houses is probably wrong ( having two functions - addOneHouse and addRowOfHouses ) or at least not the best way to do it, perhaps there's a simpler way to do it. If anyone has a suggestion or any help that would be much appreciated. Link to comment Share on other sites More sharing options...
rtlehr Posted December 18, 2014 Share Posted December 18, 2014 Maybe try something like this in the updateif (this.game.time.now > createHouseTime){ createHouse(); createHouseTime = (this.game.time.now + Phaser.RandomDataGenerator.between(800,1500));}Phaser has a handy-dandy random number generator in it, and I'm not 100% sure that syntax is correct, so you may want to check it (or just use Math.random) Ross Link to comment Share on other sites More sharing options...
acott Posted December 19, 2014 Author Share Posted December 19, 2014 So would you rename the addRowOfHouses function to createHouse ? Link to comment Share on other sites More sharing options...
rtlehr Posted December 19, 2014 Share Posted December 19, 2014 Not necessarily, I just choose "createHouse()" as a quick way of saying this is where, in the code, you would create the houses. I should have added a comment. Ross Link to comment Share on other sites More sharing options...
Recommended Posts