Jump to content

Random Generate along X Axis


Recommended Posts

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

Maybe try something like this


in the update

if (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)



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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