Julia Posted October 30, 2014 Share Posted October 30, 2014 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 More sharing options...
lewster32 Posted October 30, 2014 Share Posted October 30, 2014 This example has a routine for placing objects randomly on a grid (which you'll want for platforms no doubt): http://examples.phaser.io/_site/view_full.html?d=groups&f=depth+sort.js&t=depth%20sort Link to comment Share on other sites More sharing options...
Julia Posted October 30, 2014 Author Share Posted October 30, 2014 But should I be using tilemaps for that example? I prefer not to... If I would do it that way, would I be able to jump on them or would they be completely random and sometimes not reachable? Link to comment Share on other sites More sharing options...
lewster32 Posted October 30, 2014 Share Posted October 30, 2014 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 More sharing options...
Recommended Posts