如何将物理添加到 Phaser 3 精灵?
How add physics to Phaser 3 sprite?
我正在尝试在 typescript 项目中实现 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);
}
}
FIX?
所以也许精灵仍然需要手动添加到物理引擎(即使它是物理精灵),但不可能将整个精灵作为参数传递:
export class BMO extends Phaser.Physics.Arcade.Sprite {
create(){
this.scene.physics.add.sprite(this)
}
}
如何创建 Phaser.Physics.Arcade.Sprite 实例?
您应该在 Phaser.Game 配置中包含物理部分。
我刚刚 运行 遇到了同样的问题。我在这里找到了答案:
https://rexrainbow.github.io/phaser3-rex-notes/docs/site/arcade-body/
简而言之:
scene.physics.add.existing(gameObject, isStatic)
我还没有检查可以通过这种方式添加或扩展哪些类型的游戏对象,但我猜它们中的任何一个都可以,而不仅仅是 Physics.*.*
。
我认为您只需要将 scene.physics.add.existing(this);
添加到您的 class
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);
}
}
我正在尝试在 typescript 项目中实现 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);
}
}
FIX?
所以也许精灵仍然需要手动添加到物理引擎(即使它是物理精灵),但不可能将整个精灵作为参数传递:
export class BMO extends Phaser.Physics.Arcade.Sprite {
create(){
this.scene.physics.add.sprite(this)
}
}
如何创建 Phaser.Physics.Arcade.Sprite 实例?
您应该在 Phaser.Game 配置中包含物理部分。
我刚刚 运行 遇到了同样的问题。我在这里找到了答案:
https://rexrainbow.github.io/phaser3-rex-notes/docs/site/arcade-body/
简而言之:
scene.physics.add.existing(gameObject, isStatic)
我还没有检查可以通过这种方式添加或扩展哪些类型的游戏对象,但我猜它们中的任何一个都可以,而不仅仅是 Physics.*.*
。
我认为您只需要将 scene.physics.add.existing(this);
添加到您的 class
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);
}
}