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
相关产品推荐
相关产品推荐

