You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Phaser 3:自定义NPC类chase方法调用提示‘不是函数’问题

调用NPC类的chase方法时报错:Uncaught TypeError: npc_spr.chase is not a function

我定义了一个NPC类,其中包含名为chase的方法,在update函数中调用该方法时,控制台提示该方法不存在或未定义。但以相同方式调用bullet对象的updateMe方法却能正常工作。

相关代码

NPC类的chase方法(npcClass.js)

chase()
{
    this.Phaser.Physics.moveToObject(NPC_spr, player_spr, 100);
    this.anims.play('NPC',true);
}

update函数中的调用逻辑

bullet_ary = bulletGroup.getChildren();

for (let bullet_spr of bullet_ary)
{
     bullet_spr.updateMe();
}

NPC_ary = npcGroup.getChildren()

for (let npc_spr of NPC_ary)
{
    npc_spr.chase();
}

npcGroup的创建代码

npcGroup = this.add.group({key: 'NPC'});

npcGroup.createMultiple({key: 'NPC', repeat: 10 })

npcGroup.children.iterate(function(child)
{
child.setPosition(Phaser.Math.Between(0,  config.height, 0), Phaser.Math.Between(0,  config.width, 0));
})

错误信息

控制台输出错误:

Uncaught TypeError: npc_spr.chase is not a function

我原本期望chase方法能像updateMe一样正常调用,但多次修改代码仍出现“未定义或不是函数”的错误。

内容的提问来源于stack exchange,提问作者kraken1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 13:12:37