Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Phaser在世界上随机放置平台_Javascript_Random_Phaser Framework - Fatal编程技术网

Javascript 使用Phaser在世界上随机放置平台

Javascript 使用Phaser在世界上随机放置平台,javascript,random,phaser-framework,Javascript,Random,Phaser Framework,我正在使用Phaser制作一个垂直滚动平台游戏,但我不知道如何创建随机放置的平台。这是我迄今为止的代码(删除了不必要的东西): 我可以让它工作的地方随机,但一个平台的想法应该是你实际上可以跳上它。有足够的距离,但不会太远,所以我想不是完全随机的 希望你有一些想法 这里有一个简单的方法,只是一个开始 其想法是建立一些基本规则,将每个平台的位置建立在之前的平台上。例如,如果最后一个在左边,那么把下一个放在右边的某个地方 在这些情况下,最小/最大范围也很好:在本例中,下一个平台始终比上一个平台高出至少

我正在使用Phaser制作一个垂直滚动平台游戏,但我不知道如何创建随机放置的平台。这是我迄今为止的代码(删除了不必要的东西):

我可以让它工作的地方随机,但一个平台的想法应该是你实际上可以跳上它。有足够的距离,但不会太远,所以我想不是完全随机的


希望你有一些想法

这里有一个简单的方法,只是一个开始

其想法是建立一些基本规则,将每个平台的位置建立在之前的平台上。例如,如果最后一个在左边,那么把下一个放在右边的某个地方

在这些情况下,最小/最大范围也很好:在本例中,下一个平台始终比上一个平台高出至少200px,不超过300px

有一个


Javascript或?Javascript:)很抱歉造成混淆。好吧,我的混淆是因为是Java,这里使用的正确标记是(显然;)。啊,我明白了!谢谢你的编辑,没人能帮忙吗?
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();
            }

        }
platforms = game.add.group();
platforms.enableBody = true;
platforms.physicsBodyType = Phaser.Physics.ARCADE;

// start off on the left 220px above the ground
var x = 0, y = height - 220;

// keep adding platforms until close to the top
while(y > 200) {
  var platform = platforms.create(x, y, 'platform');
  platform.body.immovable = true;
  platform.body.allowGravity = false;

  // find center of game canvas
  var center = width / 2;

  if(x > center) {
    // if the last platform was to the right of the 
    // center, put the next one on the left
    x = Math.random() * center;
  }
  else {
    // if it was on the left, put the next one on the right
    x = center + Math.random() * (center - platformWidth);
  }
  // place the next platform at least 200px higher and at most 300px higher 
  y = y - 200 - 100 * Math.random();
}