我一直在避免在这里发布一些东西,因为我决心自己解决这个问题,但是我已经碰壁了。
我只是在短时间内使用jQuery / Javascript,以及小容量(基本动画等),我为自己设置挑战(并帮助自己学习)转换我相当无聊的投资组合网站基于“点击”或基于文本(ish)的游戏,以更具互动性/创造性的方式显示我的插图。
无论如何 - 到了这一点。
我构建了一个包含不同区域信息的对象数组,我希望做的是根据区域信息将方向(北,南)与每个区域相关联。
数组:
var areas = {
"outskirts" : {
"name" : "Dead city Outskirts",
"info" : "What you see around you",
"image" : "images/outskirts.jpg",
},
"path" : {
"name" : "A lonely Path",
"info" : "What you see around you",
"image" : "images/path.jpg"
},
"cliff" : {
"name" : "An Ultimatum",
"info" : "What you see around you",
"image": "images/cliff.jpg"
},
"city" : {
"name" : "Carcass of a City",
"info" : "What you see around you",
"image" : "images/city.jpg"
},
"building" : {
"name" : "Once a home",
"info" : "What you see around you",
"image" : "images/city.jpg"
}
};
所以,例如,
你从郊区开始并获得有关你可以向北移动的信息 然后你输入或点击一个显示“north”的项目,然后切换到下一个“path”区域。
然后根据Path,(名称和信息等)填充所有信息
我想如果我将“退出”添加到对象中,例如:
“退出”:{ “北”:“路径”, “南”:“悬崖” },
这可能是我猜你可以旅行的方向的关键,或者可能是基于每个区域的方向的真或假声明。
所以我真的要问的是:
如何在每个区域之间移动? (从outskirts.name更改为path.name) 我走的是正确的道路吗? (我的问题可能来自经验不足)
我感谢任何可以提前提出解决方案的人,非常感谢任何建议。
感谢您的时间。
答案 0 :(得分:0)
您的设计方式非常简单,适用于有限的情况。 例如。您只能为每个节点指定一个北方城市。如果两个城市位于A市以北,会发生什么。
除此之外,它是可行的。 因此,当您点击Building-North时,您可以访问
areas[areas[["building"]]["north"]]["name"]
必须进行检查以确保值未定义。
var areas = {
"outskirts" : {
"name" : "Dead city Outskirts",
"info" : "What you see around you",
"image" : "images/outskirts.jpg",
},
"path" : {
"name" : "A lonely Path",
"info" : "What you see around you",
"image" : "images/path.jpg"
},
"cliff" : {
"name" : "An Ultimatum",
"info" : "What you see around you",
"image": "images/cliff.jpg"
},
"city" : {
"name" : "Carcass of a City",
"info" : "What you see around you",
"image" : "images/city.jpg"
},
"building" : {
"name" : "Once a home",
"info" : "What you see around you",
"image" : "images/city.jpg",
"north" : "city"
}
};
alert("JSON.newPrice:"+areas[areas[["building"]]["north"]]["name"]);
答案 1 :(得分:0)
说你在悬崖上,然后点击“往北”按钮。执行的代码可能如下所示:
var curLocationName = "cliff";
var direction = "north";
// Get our new location
var newLocationName = areas[curLocationName].exits[direction];
if(newLocationName === undefined) {
console.log("You can't go that way!");
} else {
console.log("You just went to the " + newLocationName);
}
var newLocation = areas[newLocationName];
答案 2 :(得分:0)
据我所知你想做这样的事情:
var area = areas['building']; // starting point
do {
showCurrentArea(area)
var direction = getInput(); // returns "north", "south", "west", "east", etc.
area = moveFromArea(area, direction);
} while(true)
其中areas
的定义如下:
var areas = {
areaID1: {name: '...', info:'...', image:'...', exits: { north: 'areaID1', south: 'areaID2', ...} }
areaID2: {name: '...', info:'...', image:'...', exits: { north: 'areaID2', south: 'areaID3', ...} }
areaID3: {name: '...', info:'...', image:'...', exits: { north: 'areaID2', south: 'areaID1', ...} },
}
将定义moveFromArea()
:
function moveFromArea(fromArea, direction) {
var areaName = fromArea.exits[direction];
if ( !areaName){
throw "Cannot go there";
}
return areas[areaName];
}