Jump to content

Placing platforms randomly in world


Julia
 Share

Recommended Posts

I'm making a vertical scrolling platform game using Phaser, but I can't figure out how to create randomly placed platforms to jump on. This is the code I have so far (removed unneccesary stuff):

Platformer.Game = function (game) {
this._platforms = null;
this._platform = null;
this._numberOfPlatforms = 15;
this._x = this.x;
this._y = this.y;
};

Platformer.Game.prototype = {
create: function (){
this.physics.startSystem(Phaser.Physics.ARCADE);
this.physics.arcade.gravity.y = 200;

this._platforms = this.add.group();
Platformer.platform.createPlatform(this);
Platformer.platform.createPlatform(this);
Platformer.platform.createPlatform(this);
Platformer.platform.createPlatform(this);
Platformer.platform.createPlatform(this);
Platformer.platform.createPlatform(this);

game.camera.follow(this._player);

},

managePause: function () {
this.game.paused = true;
var pausedText = this.add.text(100, 250, "Game paused. Click anywhere to continue.", this._fontStyle);
this.input.onDown.add(function(){
pausedText.destroy();
this.game.paused = false;
}, this);
},

update: function () {

}
};

Platformer.platform = {
createPlatform: function (game) {
var posX = Math.floor(Math.random() * Platformer.GAME_WIDTH * this._numberOfPlatforms * 70);
var posY = Math.floor(Math.random() * Platformer.GAME_HEIGHT * this._numberOfPlatforms * 50);
var platform = game.add.sprite(posX, posY, 'platform');


game._platforms.add(platform);
platform.events.onOutOfBounds.add(this.removePlatform, this);
},

removePlatform: function (game) {
this._platform.kill();
}

}

I can get it to work to place them randomly, but the idea of a platformer should be you could actually jump on it... With enough distance but not too much, so I guess not entirely random.

Hope you have some ideas!

Thanks in advance.

Link to comment
Share on other sites

No, that example doesn't use tilemaps. As for being able to jump on them, that becomes a more difficult problem - my approach there would be to start at the player's starting point, and place a platform randomly within a certain distance of the player (the distance being how far they can jump) and then keep going, placing the next platform within a certain distance of the last. You'd also want to influence the overall direction otherwise you'd just end up with a cloud of platforms around your player, so you'd maybe want to bias the platforms to always be above or to the right of the previous platform.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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