randomly generated obstacles for endless runner

Recommended Posts

I'm making an endless runner as my first dive into phaser and I've working on getting the 'obstacles' for my player to jump over. I have 3 obstacles for the player to jump over, 2 on the 'ground' and 1 hanging from the top of the game. 

My plan is to have one obstacles randomly generated every 2-6 seconds and for them to move towards the player (the player is static except for being able to jump). But I'm not sure how to go about this.

Can anyone point me in the right direction? just some sudo code or ideas how you would approach the problem?


My repo is if anyone would like to take a look. 

Screen Shot 2016-11-28 at 7.26.36 PM.png

Share this post

Link to post
Share on other sites


i've tried to just create one obstacle for now. but I'm getting a whole load of errors.  


game.js = function() {};

  this.obstacleRate = game.rnd.integerInRange(2000, 6000);
  this.obstacleTimer = 0; = {
  create : function() {

    // obstacles
    this.obstacle =;

  update : function() {

    if (this.obstacleTimer < {
      this.obstacleTimer = + this.obstacleRate;

  shutdown : function() {


  createObstacle: function() {
    var x =;
    var y =;
    var obstacle = this.obstacle.getFirstExists(false);
    if (!obstacle) {
      obstacle = new obstacle(, 0, 0);

    obstacle.reset(x, y);


var obstacle = function(game, x, y, key, frame) {
  key = 'box';, game, x, y, key, frame);


  // enable physics on the sprite without gravity;
  this.body.allowGravity = false;

  // if the sprite is out of the game window remove the sprite
  this.checkWorldBounds = true;
  this.onOutOfBoundsKill = true;

  this.event.onKilled.add(this.onKilled, this);, this);


box.prototype = object.create(phaser.sprite.prototype);
box.prototype.constructor = box;

// when created
box.prototype.onRevived = function () {
// give the sprite some velocity
  this.body.velocity.x = -180;

^ obstacle.js prefab ^ 

'box' is from preload. 

Share this post

Link to post
Share on other sites

This is my sample code from my game, it will create a row of a spike/obstacle coming from you:

function addOneSpike(x, y, speed) {
    if(player.alive) {
    var spike = game.add.sprite(x, y, 'spikes');
    spike.body.kinematic = true;
    spike.body.loadPolygon('blocks_physicsData', 'spikes');
    spike.checkWorldBounds = true;
    spike.outOfBoundsKill = true;

function addRowOfSpikes_Straight() {
    var hole = Math.floor(Math.random() * 5) + 1;

    for (var i = 0; i < 8; i++) {
        if (i != hole && i != hole + 1) {
       //x, y, speed
          this.addOneSpike(1140, 470, 300);

And call the function every 2 seconds:

//create function..., addRowOfSpikes_Straight);

I don't know if you want this, but try it.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.