0

我一直避免在这里发布一些东西,因为我决心自己解决,但我碰壁了。

我真正使用 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"

}
};

例如,

您从郊区开始,并获得信息说您可以向北移动,然后您键入或单击一个显示“北”的项目,然后它切换到下一个区域,即“路径”。

然后根据路径填充所有信息(名称和信息等)

我想如果我向对象添加“出口”,例如:

“出口”:{“北”:“路径”,“南”:“悬崖”},

这可能是我猜测您可以前往哪些方向的关键,或者可能是基于每个区域的方向的正确或错误陈述。

所以我真正要问的是:

如何在每个区域之间移动?(从 outskirts.name 更改为 path.name)我是否走在正确的道路上?(我的问题可能来自经验不足)

我感谢任何可以提前提出解决方案的人,非常感谢任何建议。

感谢您的时间。

4

3 回答 3

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];
于 2013-04-12T22:26:51.187 回答
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"]);
于 2013-04-12T22:24:20.783 回答
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)

whereareas是这样定义的:

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];
}
于 2013-04-12T22:29:58.463 回答