2016-12-21 15 views
-1

私はプログラミングの初心者です。現在、私の最初のプログラミングクラスの最終プロジェクトに取り組んでいます。私はいくつかの問題に遭遇しましたが、これは私が解決できなかった最初のものです。私の機能が動作していません(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; 
} 

私がいる唯一の問題は、キル機能である:ここで

は問題に関連するすべてのコードです。私は同様に機能している同様のフォーマットされた関数を持っていますが、kill関数は正しくありません。誰かが私が間違っていたことを指摘できましたか?

+0

キル関数内の浮遊ブレークは何であるcase statementで使用されますか? – bugscoder

+0

フィードバックいただきありがとうございます。私は休憩を取り除いたので、すべてが適切に動作しています。このような小さな間違いを犯したことに対する私の謝罪、私はまだ学んでいます。 – Ragnaraq

+0

'if(metBo = true)'は 'if(metBo == true)'または単に 'if(metBo)'でなければなりません。 [JSHint](http://jshint.com/)を使用して、すぐにコードの問題を見つけてください。 – Xufox

答えて

1

キル機能の中にブレークを入れることはできません。あなたがkill関数を呼び出した後、breakはあなたのforループの中になければなりません。

0

break;は無効です。 kill functionからbreak;ステートメントを削除します。

break;loopsまたは

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."; 
    } 

} 
関連する問題