有些人非常慷慨,并帮我整理了以下代码。它不会在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("");
});
});
答案 0 :(得分:1)
乍一看:您的receiveInput()
函数永远不会被调用。
要修复:在$.("#form")...
功能结束时,添加
receiveInput(input);