我正在使用Phaser 3框架开发我的第一个简单游戏. 我有《敌人》类(扩展演员类).在构造函数中,我创建了两个对象- Arc和文本.这两个物体应该附着在每个敌人身上并一起移动.

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.更改这个.x和这个.y似乎更自然.

还有更正确的方法来实现子对象到Actor类的附加吗?

推荐答案

我宁愿从Phaser.Container而不是Actor扩展,或者Actor类应该从Phaser.Container扩展并将所有gameObjects打包到容器中.然后您可以用xy移动对象.

An alternative would be to override the preupdate method and than set in there the position.
(I'm not sure, if this is a good solution)

export class Enemy extends Actor {
    ...
    preUpdate (time, delta) {
        super.preUpdate(time, delta);
        this.container.x = this.x;
        this.container.y = this.y;
    }
}

Javascript相关问答推荐

如何在NightWatch.js测试中允许浏览器权限?

Redux工具包查询(RTKQ)端点无效并重新验证多次触发

yarn安装一个本地npm包,以便本地包使用main项目的node_modules(ckeditor-duplicated-modules错误)

网页自检测外部元素无法加载

构造HTML表单以使用表单数据创建对象数组

如何调用名称在字符串中的实例方法?

在open shadow—root中匹配时,使用jQuery删除一个封闭的div类

S文本内容和值不必要的不同

将核心模块导入另一个组件模块时存在多个主题

将内容大小设置为剩余可用空间,但如果需要,可在div上显示滚动条

为什么我的getAsFile()方法返回空?

自定义确认组件未在vue.js的v菜单内打开

使用Reaction窗体挂钩注册日历组件

在JavaScript中,有没有一种方法可以迭代字符串的词法标记?

如何在TransformControls模式下只保留箭头进行翻译?

无法使用Redux异步函数读取未定义的useEffect钩子的属性';map';

Django模板中未加载JavaScript函数

如何调整下拉内容,使其不与其他元素重叠?

我如何才能让p5.js在不使用实例模式的情况下工作?

react :图表负片区域不同 colored颜色