Search the Community
Showing results for tags 'place'.
-
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.