我相信我對循環對象文字和數組有問題。 我想使用一個對象字面值''地圖'我之前使用過一個數組。我想使用對象文字的原因是因爲我想要一個變量來顯示它所在位置的名稱。現在我總是遇到loc [2] [1] = player;的問題。遍歷對象文字和數組
var room0 = [
[blank, blank, blank, blank],
[blank, blank, blank, blank],
[blank, player, blank, doorRight],
[blank, blank, blank, blank]
];
var room1 = [
[blank, blank, blank, blank],
[blank, blank, blank, blank],
[blank, blank, blank, blank],
[blank, blank, blank, blank]
];
var loc = room0;
var map = {
"living room": room0,
"Bedroom": room1,
"Outside": room2
};
function moveRight() {
for (var y = 0; y < loc.length; y++) {
var posX = loc[y].indexOf(player);
if (posX <= -1) continue;
// if going to the next room to the right
if (loc[y][posX+1] == doorRight) {
for (var name in map) {
if (loc == map[name]) {
var rightLoc = map[name+1];
}
}
loc[y][posX] = blank;
loc = rightLoc;
loc[2][1] = player;
break;
}
if (loc[y][posX+1] != blank) break;
if (posX < loc[y].length - 1) {
loc[y][posX] = blank;
loc[y][posX + 1] = player;
}
break;
}
}
哪裏是房間2定義? – Piyuesh 2013-05-02 05:55:29
這與實際問題無關,但是:這已經是我建議使用類的那種複雜性。這也會使得房間的命名更容易。 – 2013-05-02 05:55:36
@IngoBürk:這有什麼用?那麼所有這些變量將是對象屬性,並...? – Bergi 2013-05-02 06:00:10