2015-10-31 60 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