Jump to content

randomly generated obstacles for endless runner


crffty
 Share

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 https://github.com/craftycal/vaultage if anyone would like to take a look. 

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

Link to comment
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

vaultage.game = function() {};

  this.obstacleRate = game.rnd.integerInRange(2000, 6000);
  this.obstacleTimer = 0;

vaultage.game.prototype = {
  create : function() {


    // obstacles
    this.obstacle = this.game.add.group();

  },
  update : function() {


    if (this.obstacleTimer <  this.game.time.now) {
      
      this.createObstacle();
      this.obstacleTimer = this.game.time.now + this.obstacleRate;
    }


  },
  shutdown : function() {

  }

  createObstacle: function() {
    
    var x = this.game.width;
    var y = this.game.hight(290);
    
    var obstacle = this.obstacle.getFirstExists(false);
   
    if (!obstacle) {
      obstacle = new obstacle(this.game, 0, 0);
      this.obstacle.add(obstacle);
    }

    obstacle.reset(x, y);
    obstacle.revive();

    }
  }

}
var obstacle = function(game, x, y, key, frame) {
  key = 'box';
  phaser.Sprite.call(this, game, x, y, key, frame);

  this.anchor.setTo(0.5);

  // enable physics on the sprite without gravity
  this.game.physics.arcade.enableBody(this);
  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.events.onRevived.add(this.onRevived, 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. 

Link to comment
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');
    this.spikes.add(spike);
    game.physics.p2.enable(spike);
    spike.body.moveLeft(speed);
    spike.body.kinematic = true;
    spike.body.clearShapes();
    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...
game.time.events.loop(2000, addRowOfSpikes_Straight);

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

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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