Sign in to follow this  
crffty

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.  

vaultage.game = function() {};

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

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

    // physics engine
    this.game.physics.startSystem(Phaser.Physics.ARCADE);
    this.game.physics.arcade.gravity.y = 1000;

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

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

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

    // obstacles
    this.obstacles = this.game.add.group();
    
    //  this.box = this.game.add.group();
    //  this.pole = this.game.add.group();
    //  this.cable = this.game.add.group();

    // physics on sprites
    this.game.physics.arcade.enable([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.game.physics.arcade.collide(this.player, 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.