为什么不更新y坐标属性?

时间:2018-11-30 00:59:39

标签: javascript if-statement while-loop

我一直在使用CodeCombat进行学习。有了这段代码,我被困了一个星期,没有人可以帮助我。我试图理解为什么Y坐标属性(尤其是hero.pos.y)没有更新。我在谈论这部分:

else if (hero.pos.y === 24) {//works only when x===18
hero.say("moving to the steel door");
...
}

尽管我的英雄移动到x:18 y:24的位置,但它并没有记录为已更改。欢迎愿意的人答复。

hero.moveXY(18, 40);
while (true) {
hero.say("starting while");
var enemy = hero.findNearestEnemy();
var friend = hero.findNearestFriend();
var item = hero.findNearestItem();
if (enemy) {
   ...do smth
} else if (friend) {
   ...do smth
} else if (item) {
    ...do smth
} else if (hero.pos.y === 40) {//this works
    hero.say("moving down");
    hero.moveXY(hero.pos.x, hero.pos.y - 16); //this works
    hero.say("At this point I SHOULD have changed my hero.pos.y to 24");
} else if (hero.pos.y === 24) {//this works only with x===18
    hero.say("moving to the steel door");
} else if (hero.pos.y !== 24) {//so this runs even when I check hero.pos, which is 24!
    hero.say("My Y-position hasn't changed!");
}

}

*现在请注意,这是一个在线游戏,这显然意味着我不是从头开始创建代码的,如果您不在游戏环境中进行编码,则无法对其进行验证。

0 个答案:

没有答案