Jump to content

random obstacles


crffty
 Share

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

  }


}

    

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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