向左或向右推Phaser3时,沿边框的垂直移动被阻止

时间:2018-11-28 23:03:41

标签: javascript phaser-framework

我已经在Phaser3中创建了一个磁贴图和一个角色来环绕它,几乎所有东西都可以正常工作。

但是我可以看到,当我沿着地图的边界垂直移动时,有时我有时(如果我也相应地向左或向右推),就无法再进入新的图块。

首先,我很有趣,如果有人知道为什么会这样?

接下来,我对如何调试这些东西一无所知,并且非常想知道将来如何处理类似问题。

class GameScene extends Phaser.Scene
{
  constructor()
  {
    super({key:'GameScene'});
    this.player = undefined;
    this.map = undefined;
  }

  create()
  {
    this.map = new TileMap(this);
    this.player = new Player(this);
    this.physics.add.collider(this.player.sprite, this.map.impasable);
    this.cameras.main.startFollow(this.player.sprite);
  }

  update()
  {
    this.player.update();
  }
}



class TileMap
{
  constructor(scene)
  {
    this.tileWidth = 64;
    this.tileHeight = 64;
    this.impasable = scene.physics.add.staticGroup();
    this.tiles = [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
      [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
      [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
      [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
      [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ];

    for (var y = 0; y < this.tiles.length; y++) {
      for (var x = 0; x < this.tiles[y].length; x++) {
        switch (this.tiles[y][x]) {
          case 0:
            this.impasable.create(x * this.tileWidth, y * this.tileHeight, 'water');
            break;
          case 1:
            scene.add.sprite(x * this.tileWidth, y * this.tileHeight, "grass");
            break;
        }
      }
    }
  }
}

class Player
{
  constructor(scene)
  {
    this.sprite = scene.physics.add.sprite(scene.game.renderer.width / 2, scene.game.renderer.height / 2, "player");

    scene.anims.create({
      key: "PlayerWalkLeft",
      frameRate: 4,
      repeat: -1,
      frames: scene.anims.generateFrameNumbers("player" , {
        frames : [1, 5, 9, 13]
      })
    });
    scene.anims.create({
      key: "PlayerWalkRight",
      frameRate: 4,
      repeat: -1,
      frames: scene.anims.generateFrameNumbers("player" , {
        frames : [3, 7, 11, 15]
      })
    });
    scene.anims.create({
      key: "PlayerWalkUp",
      frameRate: 4,
      repeat: -1,
      frames: scene.anims.generateFrameNumbers("player" , {
        frames : [2, 6, 10, 14]
      })
    });
    scene.anims.create({
      key: "PlayerWalkDown",
      frameRate: 4,
      repeat: -1,
      frames: scene.anims.generateFrameNumbers("player" , {
        frames : [0, 4, 8, 12]
      })
    });

    this.cursors = scene.input.keyboard.createCursorKeys();
  }

  update()
  {
    var velX = 0;
    var velY = 0;
    var speed = 200;
    if (this.cursors.left.isDown) { velX -= speed; }
    if (this.cursors.right.isDown) { velX += speed; }
    if (this.cursors.up.isDown) { velY -= speed; }
    if (this.cursors.down.isDown) { velY += speed; }
    this.sprite.setVelocityX(velX);
    this.sprite.setVelocityY(velY);

    if (velY > 0 && velX == 0) {
      this.sprite.anims.play("PlayerWalkDown", true);
    } else if (velY < 0 && velX == 0) {
      this.sprite.anims.play("PlayerWalkUp", true);
    } else if (velX > 0) {
      this.sprite.anims.play("PlayerWalkRight", true);
    } else if (velX < 0) {
      this.sprite.anims.play("PlayerWalkLeft", true);
    } else {
      this.sprite.anims.stop();
    }
  }
}

0 个答案:

没有答案