我的功能不起作用(javascript)

时间:2016-12-21 06:00:28

标签: javascript

我是编程的初学者,目前正在为我的第一个编程课程完成最后的项目。我遇到了几个问题,但这是我第一次无法解决的问题。

以下是与该问题相关的所有代码:

var actionsIKnow = ["north", "east", "south", "west", "take", "drop", "use", "say", "fight", "kill", "equip", "help", "commands"];
var action = "";

function playGame() {
    playersInput = input.value;
    playersInput = playersInput.toLowerCase();

gameMessage = "";
action = "";

for (i = 0; i < actionsIKnow.length; i++) {
    if (playersInput.indexOf(actionsIKnow[i]) !== -1) {
        action = actionsIKnow[i];
        console.log("player's action: " + action);
        break;
    }
}

for (i = 0; i < wordsIKnow.length; i++) {
    if (playersInput.indexOf(wordsIKnow[i]) !== -1) {
        word = wordsIKnow[i];
        console.log("player's word: " + word);
        break;
    }
}

for (i = 0; i < itemsIKnow.length; i++) {
    if (playersInput.indexOf(itemsIKnow[i]) !== -1) {
        item = itemsIKnow[i];
        console.log("player's item: " + item);
        break;
    }
}

switch(action) {
    case "north":
        if (mapPath[mapLocation][0]) {
            mapLocation -= 4;
        } else {
            gameMessage = blockedPathMessages[mapLocation];
        }
        break;

    case "east":
        if (mapPath[mapLocation][2]) {
            mapLocation += 1;
        } else {
            gameMessage = blockedPathMessages[mapLocation];
        }
        break;

    case "south":
        if (mapPath[mapLocation][1]) {
            mapLocation += 4;
        } else {
            gameMessage = blockedPathMessages[mapLocation];
        }
        break;

    case "west":
        if (mapPath[mapLocation][3]) {
            mapLocation -= 1;
                if (metBo = true) {
                    map[13] = "The once bright basement is now dark and empty. The old man seems to have disappeared."
                }
        } else {
            gameMessage = blockedPathMessages[mapLocation];
        }
        break;

    case "help":
        if(helpMessages[mapLocation] !== "") {
            gameMessage = helpMessages[mapLocation] + " ";
        }

        gameMessage += "For usable actions, use commands"
        break;

    case "commands":
        gameMessage += "Here are actions you may use: " 
        gameMessage += "North, East, South, West, Take, Drop, ";
        gameMessage += "Use, Say, Fight, Kill, Equip, Help, Commands";
        break;

    case "say":
        speak();
        break;

    case "take":
        takeItem()
        break;

    case "drop":
        dropItem();
        break;

    case "equip":    
    case "use":
        useItem();
        break;

    case "kill":
        kill();
        break;

    case "fight":
        fight();
        break;

    default:
        gameMessage = "You cannot do that.";
}
render();
}

function kill() {

        if (mapLocation === 13) {
            map[13] = "";
            gameMessage = "You slaughter the old man, using his own robes to choke him to death. His body collapses to the ground";
            killedSecretBo = true;
        } else {
            gameMessage = "You cannot kill a target at the moment.";
        }
        break;
}

我遇到的唯一问题是杀戮功能。我有类似格式的功能,工作正常,但杀死功能不是。有谁可以指出我做错了什么?

2 个答案:

答案 0 :(得分:1)

你不能在kill函数中有中断。在调用kill函数后,Break必须在for循环中。

答案 1 :(得分:0)

您写的break;无效。从break;删除kill function声明。

break;用于loopscase statement

function kill() {

    if (mapLocation === 13) {
        map[13] = "";
        gameMessage = "You slaughter the old man, using his own robes to choke him to death. His body collapses to the ground";
        killedSecretBo = true;
    } else {
        gameMessage = "You cannot kill a target at the moment.";
    }

}