Sign in to follow this  

random obstacles

Recommended Posts

I'm making an endless runner and need to generate an obstacle every 2-6 seconds for the player to jump over. I have three obstacle sprites, two along the 'ground' and one hanging from the top of the game window.

I was thinking about using a group and randomly selecting a key from that group. sprites must enter from the right and move along y until they go of screen on the right then are killed and re-used again. I've been trying multiple methods to active this but with no luck, its all bit over my head, I'm not even sure whats going wrong. So i'm looking for a bit of direction and advice on how to active this.  


this is the code (mostly) without my attempts as it got a bit missy. = function() {};

  // generate a obstacle at a random time between 2 seconds and 6 seconds
  this.obstacleRate = game.rnd.integerInRange(2000, 6000);
  this.obstacleTimer = 0; = {
  create : function() {

    // physics engine; = 1000;

    // sprites
    // background
    this.background =, 0,, 360, 'background');
    this.background.autoScroll(-100, 0);

    // ground
    this.ground =, 290,, 8, 'ground');
    this.ground.autoScroll(-180, 0);

    // player
    this.player = this.add.sprite(45, 200, 'player');
    this.player.animations.add('run');'run', 15, true);

    // obstacles
    this.obstacles =;
    // =;
    //  this.pole =;
    //  this.cable =;

    // physics on sprites[this.player, this.ground]);
    this.ground.body.immovable = true;
    this.ground.body.allowGravity = false;
    this.player.body.collideWorldBounds = true;

    // input
    cursors = game.input.keyboard.createCursorKeys();
    jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

  update : function() {, this.ground);

    if (jumpButton.isDown && (this.player.body.touching.down))
            this.player.body.velocity.y = -400;

  shutdown : function() {


  createObstacles : function() {




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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.