[英]Phaser some methods are missing on my Phaser.Physics.Arcade.Sprite
I'm trying to build a game with the Phaser 3 game engine.我正在尝试使用 Phaser 3 游戏引擎构建游戏。 I'm using TypeScript and writing classes fro each of my entities.
我正在使用 TypeScript 并为我的每个实体编写类。
I've started with some clouds in the background.我从背景中的一些云开始。 I started to remove the arcade gravity and add the velocityX for the wind.
我开始移除街机重力并为风添加速度X。 My class looks like this
我的班级看起来像这样
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);
}
}
But I get a type error但我收到一个类型错误
Uncaught TypeError: Cannot read property 'gravity' of null
未捕获的类型错误:无法读取 null 的属性“重力”
This makes no sense to me because the method setGravityY
is a extended, you can see it in the screenshot bellow.这对我来说毫无意义,因为
setGravityY
方法是一个扩展方法,您可以在下面的屏幕截图中看到它。
So why is body
undefined?那么为什么
body
是未定义的呢? I thought that extending Phaser.Physics.Arcade.Sprite is supposed to have a body.我认为扩展 Phaser.Physics.Arcade.Sprite 应该有一个身体。 Like in the docs here
就像在这里的文档中一样
You are correct that the Arcade Sprite should have a body.你是对的,Arcade Sprite 应该有一个身体。 But the scene's physics does not know about this game object yet.
但是场景的物理特性还不知道这个游戏对象。 So you have to add it to the scene physics like this:
所以你必须像这样将它添加到场景物理中:
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.