【问题标题】:How do I adjust Player's physical bounds?如何调整玩家的物理界限?
【发布时间】:2020-09-16 10:25:55
【问题描述】:

对象边界

我是一名新的 Phaser 开发者,正处于我的第一款游戏的初期阶段。我的问题是,当我的球员落地时,定义他的边界的边界框比他大得多(见图)。因此,在所示示例中,玩家将在边界接触时弹跳,而不是他的脚。

在行动

物理问题

为了实现我的目标,我是否必须从 ARCADE 物理更改为更强大的物理引擎?

代码

真的没什么可展示的,但这是我所拥有的:

/// <reference path="../defs/phaser.d.ts" />

class MainGame extends Phaser.Scene {
  constructor() {
    super("MainGame");
  }

  preload() {
    this.load.image("bg1", "assets/bg-level1.png");
    this.load.image('ground', 'assets/platform.png');
    this.load.spritesheet('dude', 'assets/final-jump.png', {
      frameWidth: 118,
      frameHeight: 118
    });
  }

  create() {

    this.setupBackground();
    this.setupPlayer();

    this.add.text(0, 0, 'Use Cursors to scroll camera.\nQ / E to zoom in and out', {
      font: '18px Courier',
      fill: 'black'
    });

    this.physics.add.collider(this.player, this.platforms);

  }

  update() {

    // Set Player Animations
    this.setPlayerAnimation();

    if (this.player.body.onFloor()) {
      console.log("FLOOR");
      this.player.body.setAccelerationX(31);
      this.player.body.setAccelerationY(31);

    }

  }

  //---------------------------------
  // CREATE RELATED FUNCTIONS
  //---------------------------------

  setupBackground() {
    this.background = this.add.image(0, 0, "bg1");
    this.background.setOrigin(0, 0);

    //this.background.setInteractive();
    this.background.setAlpha(.2, .2, .2, .2);

    //  The platforms group contains the ground and the 2 ledges we can jump on
    this.platforms = this.physics.add.staticGroup();

    //  Here we create the ground.
    //  Scale it to fit the width of the game (the original sprite is 400x32 in size)
    this.platforms.create(400, 568, 'ground').setScale(2).refreshBody();

  }



  setupPlayer() {

    this.player = this.physics.add.sprite(100, 283, 'dude');
    console.log(this.player.body.touching.down);

    this.player.setVelocity(200, 100).setBounce(.8, .8).setCollideWorldBounds(true);

    // Re-Size Player Size
    this.player.setDisplaySize(220, 210);

    // Collision Handler
    this.physics.add.overlap(this.player, this.platforms, this.showJump, null, this);


    // ANIMATIONS
    this.anims.create({
      key: 'jump-up',
      frames: this.anims.generateFrameNumbers('dude', {
        start: 1,
        end: 2
      }),
      frameRate: 5,
      repeat: 1
    });

    this.anims.create({
      key: 'falling',
      frames: this.anims.generateFrameNumbers('dude', {
        start: 0,
        end: 1
      }),
      frameRate: 5,
      repeat: 1
    });

    this.anims.create({
      key: 'onGround',
      frames: this.anims.generateFrameNumbers('dude', {
        start: 4,
        end: 4
      }),
      frameRate: 24,
      repeat: 1
    });

  }


  showJump() {

    this.player.anims.play('jump-up', true);
  }


  //---------------------------------
  // UPDATE RELATED FUNCTIONS
  //---------------------------------

  setPlayerAnimation() {
    //this.player.anims.play('jump-up', true);

    if (this.player.body.deltaY() > 0 && this.player.body.onFloor()) {
      this.player.anims.play('falling', true);
    }

  }


}

哦,是的……

我下载并安装的本地文档包的功能给我留下了深刻的印象,但是,我对从哪里开始寻找东西感到困惑。例如,假设我正在努力让我的玩家在空中跳跃并播放动画,并在他落地时变回来。鉴于这些信息,我如何找到我需要的正确类或函数???

【问题讨论】:

    标签: phaser-framework


    【解决方案1】:

    你不必改变街机物理。试试这样的:

    // Change the size of the bounding box.
    this.player.setSize(32, 32);
    // Change the location of the bounding box.
    this.player.setOffset(0, 28);
    
    // If that doesn't work, try this instead:
    // this.player.body.setOffset(0, 28);
    

    显然您需要为您的场景调整宽度/高度和偏移量。

    参考资料:

    【讨论】:

      猜你喜欢
      • 2022-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多