Phaser 我的 Phaser.Physics.Arcade.Sprite 缺少一些方法

Phaser some methods are missing on my Phaser.Physics.Arcade.Sprite

我正在尝试使用 Phaser 3 游戏引擎构建游戏。我正在使用 TypeScript 并为我的每个实体编写 classes。

我从背景中的一些云开始。我开始移除街机重力并为风添加 velocityX。我的 class 看起来像这样

class Cloud extends Phaser.Physics.Arcade.Sprite {
    constructor(scene, x, y) {
        super(scene, x, y, 'cloud');
        this.setGravityY(0);
        return this;
    }

    static preload(scene) {
        scene.load.image('cloud', cloudAsset);
    }
}

但是我遇到类型错误

Uncaught TypeError: Cannot read property 'gravity' of null

这对我来说毫无意义,因为方法 setGravityY 是一个扩展,您可以在下面的屏幕截图中看到它。

那么为什么 body 未定义?我认为扩展 Phaser.Physics.Arcade.Sprite 应该有一个主体。就像这里的文档一样

Arcade Sprite 应该有一个 body,你是对的。但是场景的物理还不知道这个游戏object。所以你必须像这样将它添加到场景物理中:

class Cloud extends Phaser.Physics.Arcade.Sprite {
    constructor(scene, x, y) {
        super(scene, x, y, 'cloud');
        scene.physics.add.existing(this); //here you add this existing sprite object to scene's physics 
        this.setGravityY(0);
        return this;
    }
}