2015-10-31 30 views
-3

一些人非常慷慨,並幫助我將以下代碼放在一起。它不會在Chrome控制檯中引發任何錯誤,但輸入不起作用。您輸入的命令都不會執行其功能。我盯着這直到我是藍色的,但沒有看到我創建錯誤的地方。有任何想法嗎?爲什麼輸入不起作用?

var rooms = { 
    northWest: { 
     name: "North West", 
     hasSword: true, 
     paths: { 
      east: "northEast", 
      south: "southWest" 
     } 
    }, 
    northEast: { 
     name: "North East", 
     paths: { 
      west: "northWest", 
      south: "southEast" 
     } 
    }, 
    southWest: { 
     name: "South West", 
     paths: { 
      east: "southEast", 
      north: "northWest" 
     } 
    }, 
    southEast: { 
     name: "South East", 
     paths: { 
      west: "southWest", 
      north: "northEast" 
     } 
    }, 
}; 

// Set Current Room 

var currentRoom = rooms["northWest"]; 

$(document).ready(function() { 

    $("form").submit(function() { 
     var input = $("#commandLine").val(); 

     // this is a plugin to insert the repetitive lines throughout the code 

     $.fn.properDisplay = function() { 
      return this.hide().insertBefore("#placeholder").fadeIn(1000); 
     }; 

     // this is a function to use the "I don't understand" statement throughout the code 

     function understand() { 
      $("<p>I don't understand " + input + ".</p>").properDisplay(); 
     }; 



     //This is a function to travel from one room to the next 

     var travel = function(direction) { 
      var newRoom = rooms[currentRoom.paths[direction]]; 
      if (!newRoom) { 
       $("<p>You can't go that way.</p>").properDisplay(); 
      } else { 
       currentRoom = newRoom; 
       $("<p>You are now in the " + currentRoom.name + " Room.   </p>").properDisplay(); 
      } 
     }; 

     // This is the take sword function 

     var takeSword = function() { 
      if (currentRoom.hasSword) { 
       $("<p>You picked up a sword.</p>").properDisplay(); 
      } else { 
       $("<p>The sword is not here.</p>").properDisplay(); 
      } 
     }; 


     var receiveInput = function(input) { 
      switch (input) { 
       case "help": 
        $("#messageHelp").properDisplay(); 
        break; 
       case "take sword": 
        takeSword(); 
        break; 
       case "go east": 
        travel("east"); 
        break; 
       case "go west": 
        travel("west"); 
        break; 
       case "go north": 
        travel("north"); 
        break; 
       case "go south": 
        travel("south"); 
        break; 
      } 
     } 


     $("#commandLine").val(""); 
    }); 
}); 
+1

乍一看:你'receiveInput()'函數不會被調用。要修正:在$。(「#form」)...'函數末尾添加'receiveInput(input);' – wintvelt

+0

謝謝@wintvelt。我花了一天的時間找出在哪裏添加。我把它放在'$(「form」)。submit(function(){'部分。只是想出了它的位置。再次感謝你的幫助。現在一切正常。 – megler

+0

也作爲回答發表..很高興聽到它的幫助。 – wintvelt

回答

1

乍一看:您的receiveInput()函數從未被調用過。

要解決:在你$.("#form")...函數結束時,加

receiveInput(input); 
+0

爲了澄清,它工作時,我把它放在 '$(「#commandLine」)。val(「」);' – megler

相關問題