繁体   English   中英

移相器:this.game在Update函数中未定义

[英]Phaser: this.game is undefined in the Update function

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

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

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

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的成员。

理想的情况下,重命名this别的东西,然后使用它。 现在,您可以在传递的匿名函数中使用myself变量,并访问myself的属性。

var myself = this;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM