我正在使用 Phaser 3 框架开发我的第一个简单游戏。 我有敌人类(扩展演员类)。在构造函数内,我创建了两个对象 - Arc 和 Text。这两个物体应该附加到每个敌人身上并一起移动。
export class Enemy extends Actor {
constructor(
scene: Phaser.Scene, x: number, y: number
) {
super(scene, x, y, 'enemy')
this.x = x
this.y = x
const circle: GameObjects.Arc = this.scene.add.circle(x, y, 20, 0x333333)
const elSizeText: GameObjects.Text = this.scene.add.text(x, y, 'label, {font: 'bold 20px Arial'})
this.scene.physics.add.existing(this)
this.setVelocity(0, 500)
}
}
我通过创建容器并在其中包含所有 3 个对象来实现这一点。
this.container = this.scene.add.container(x, y)
this.container.add([circle, elSizeText, this])
问题是,现在如果我需要更改(或获取)敌人的位置,我应该更改 this.container.x、this.container.y。改为更改 this.x 和 this.y 似乎更自然。
是否有更正确的方法来实现将子对象附加到 Actor 类?
我宁愿从
Phaser.Container
而不是 Actor
扩展,或者 Actor
类应该从 Phaser.Container
扩展并将所有游戏对象打包到容器中。然后您可以使用 x
或 y
移动对象。
另一种方法是重写
preupdate
方法并在那里设置位置。export class Enemy extends Actor {
...
preUpdate (time, delta) {
super.preUpdate(time, delta);
this.container.x = this.x;
this.container.y = this.y;
}
}