【问题标题】:How add physics to Phaser 3 sprite?如何将物理添加到 Phaser 3 精灵?
【发布时间】:2019-08-13 13:57:24
【问题描述】:

我正在尝试在打字稿项目中实现Phaser 3 documentation for physics sprites,但这似乎不如预期的那么简单:

不工作

export class BMO extends Phaser.Physics.Arcade.Sprite {

    constructor(scene) {
        super(scene, 100,150, "bmo")
        this.scene.add.existing(this)
    }

    create(){
        this.setVelocity(100, 200);
        this.setBounce(1, 1);
        this.setCollideWorldBounds(true);
    }
}

但是在创建new BMO() 时,精灵没有物理特性。 物理引擎本身正在工作,因为我可以传递图像并且它可以工作:

工作中

export class GameScene extends Phaser.Scene {

  create(){
    let logo = this.physics.add.image(400, 100, 'bmosmall')
    logo.setVelocity(100, 200);
    logo.setBounce(1, 1);
    logo.setCollideWorldBounds(true);
  }
}

修复?

所以也许精灵仍然需要手动添加到物理引擎中(即使它是物理精灵),但不可能将整个精灵作为参数传递:

 export class BMO extends Phaser.Physics.Arcade.Sprite {
     create(){
        this.scene.physics.add.sprite(this)
     }
 }

如何创建 Phaser.Physics.Arcade.Sprite 实例?

【问题讨论】:

    标签: typescript phaser-framework


    【解决方案1】:

    我认为您只需将scene.physics.add.existing(this); 添加到您的班级

    export class BMO extends Phaser.Physics.Arcade.Sprite {
    
        constructor(scene) {
            super(scene, 100,150, "bmo")
            scene.physics.add.existing(this); // add this line
            this.scene.add.existing(this)
        }
    
        create(){
            this.setVelocity(100, 200);
            this.setBounce(1, 1);
            this.setCollideWorldBounds(true);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我刚刚遇到了同样的问题。我在这里找到了答案:

      https://rexrainbow.github.io/phaser3-rex-notes/docs/site/arcade-body/

      简而言之:

      scene.physics.add.existing(gameObject, isStatic)
      

      我还没有检查可以通过这种方式添加或扩展哪些类型的游戏对象,但我猜它们中的任何一个都可以,而不仅仅是Physics.*.*

      【讨论】:

        【解决方案3】:

        您应该在 Phaser.Game 配置中包含一个物理部分。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-01
          • 2020-01-27
          • 2020-11-24
          • 2014-10-02
          • 2019-04-14
          • 1970-01-01
          • 2019-09-18
          • 1970-01-01
          相关资源
          最近更新 更多