移相器:this.game在Update函数中未定义 [英] Phaser: this.game is undefined in the Update function

查看:80
本文介绍了移相器:this.game在Update函数中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好, 很抱歉再次提出要求,但我需要在明天上学之前完成.基本上,当我尝试在更新函数中访问this.game变量时,它说它是未定义的,具体来说,我得到了这个. 未捕获的TypeError:无法读取未定义的属性'状态'". 在我的更新功能中,我有这个:

Good evening, sorry for asking again, but I need to be done with this by tomorrow for school. Basically, when I try to access the this.game variable in the update function, it says that it is undefined, to be specific, I get this; "Uncaught TypeError: Cannot read property 'state' of undefined". In my update function I have this:

create: function() {
    [...]
    map.setCollisionByExclusion([], true, doorLayer);
    [...]
}

当我尝试在碰撞操作的更新功能中访问this.game时,出现上述错误. 这是代码;

When I try to access this.game in the update function on the collision action, I get the error mentioned above. Here's the code;

update: function() {
    [...]
    this.game.physics.arcade.collide(player, doorLayer, function() {
        if (hasItem) {
            this.game.state.start('Hallway'); //This is where I get the error
        }
    });
    [...]
}

谢谢您的时间.

推荐答案

this.game.physics.arcade.collide(player, doorLayer, function() { if (hasItem) { this.game.state.start('Hallway'); //This is where I get the error } });

this.game.physics.arcade.collide(player, doorLayer, function() { if (hasItem) { this.game.state.start('Hallway'); //This is where I get the error } });

请注意,内部this指的是您传递的匿名函数,该函数显然没有名为game的成员.

Notice the inner this refers to the anonymous function you are passing which clearly does not have a member named game.

理想情况下,将this重命名为其他名称,然后再使用它. 现在,您可以在传递的匿名函数中使用myself变量,并访问myself的属性.

Ideally, rename this to something else and then use it. Now you can use myself variable inside the anonymous function passed and access myself's properties.

var myself = this;

这篇关于移相器:this.game在Update函数中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆