0

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

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();
            }

        }

我可以让它随机放置它们,但平台游戏的想法应该是你实际上可以跳上它。有足够的距离但不是太多,所以我猜不是完全随机的。

希望你有一些想法!

4

1 回答 1

2

这是一个简单的方法,实际上只是一个开始。

这个想法是建立一些基本规则,将每个平台的位置建立在之前的平台上。例如,如果最后一个在左侧,则将下一个放在右侧的某个位置。

在这些情况下,最小/最大范围也很好:在这个例子中,下一个平台总是比上一个平台高至少 200 像素,并且不超过 300 像素。

在 codepen 上有一个可播放的示例

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();
}
于 2014-11-04T00:17:47.293 回答