Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Phaser我的Phaser.Physics.Arcade.Sprite上缺少一些方法_Javascript_Phaser Framework - Fatal编程技术网

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

Javascript Phaser我的Phaser.Physics.Arcade.Sprite上缺少一些方法,javascript,phaser-framework,Javascript,Phaser Framework,我正在尝试用Phaser 3游戏引擎构建一个游戏。我正在为我的每个实体使用TypeScript和编写类 我从背景中的一些云开始。我开始移除拱廊的重力,并为风添加velocityX。我的班级看起来像这样 class Cloud extends Phaser.Physics.Arcade.Sprite { constructor(scene, x, y) { super(scene, x, y, 'cloud'); this.setGravityY(0);

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

我从背景中的一些云开始。我开始移除拱廊的重力,并为风添加velocityX。我的班级看起来像这样

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);
    }
}
但是我得到一个类型错误

未捕获的TypeError:无法读取null的属性“gravity”

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

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


街机精灵应该有一个身体,这是正确的。但是场景的物理特性还不知道这个游戏对象。因此,必须将其添加到场景物理中,如下所示:

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

前几天我在试图解决这个问题时遇到了这个问题。谢谢你的回答!