美好的一天,首先我想告诉大家,我不自豪地发布整个源代码。我无法在较小的块中找出问题。这是如何“通过”,但实际上它不应该?
这里是文件的快照:
getRoomById : function(areaId, roomId) {
if (this.hAreas.get(areaId) !== null && this.hAreas.get(areaId)[ roomId ] !== undefined)
{
//I can get here...
//alert(uneval(this.hAreas.get(areaId)));
// returns: ({'000001':{areaId:"000001", roomId:"000001", Informations:{Name:"Room 1"}}, '000002':{areaId:"000001", roomId:"000002", Informations:{Name:"Room 2"}}, '000003':{areaId:"000001", roomId:"000003", Informations:{Name:"Room 3"}}})
//alert(roomId);
// returns: 000003
return this.hAreas.get(areaId)[ roomId ];
// returns: undefined
}
},
和整个文件是存在的:http://jsfiddle.net/JSWCu/2/
问题:我有哪些测试针对JS.Hash
的参数包含对象的方法。该if
希望返回true,但一旦进入,我不能得到(要么提醒或返回)该对象JS.Hash
(未定义返回)。
谢谢!非常感谢您的帮助。我也希望提示,以避免这种错误。
编辑:这是整个代码。对不起它真的很大。这只是开始变得复杂,我不能(现在)隔离的问题,一个较小的一块代码:
<html>
<head>
<style type="text/css">
</style>
<script type="text/javascript">
JSCLASS_PATH = 'http://www.poc.ca/cybrix/src';
function appendConsole(input) {
document.getElementById("console").innerHTML += input + "<br />";
}
</script>
<script type="text/javascript" src="http://www.poc.ca/cybrix/src/loader-browser.js"></script>
<script type="text/javascript">
JS.require('JS.Hash', 'JS.Observable', function() {
var AreaLists = {
"000001" : { "Name" : "Test Area", "Loaded" : false },
};
var World = new JS.Class({
hAreas : new JS.Hash([]),
getAreas : function(areaId) {
if (! this.hAreas.get(areaId) && AreaLists[ areaId ] !== undefined)
{
//TODO: Load from external sources
this.hAreas.put(areaId, {
"000001" : new Room(areaId, "000001", { "Name" : "Room 1" }),
"000002" : new Room(areaId, "000002", { "Name" : "Room 2" }),
"000003" : new Room(areaId, "000003", { "Name" : "Room 3" }),
});
AreaLists[ areaId ].Loaded = true;
appendConsole("Areas #" + areaId + " : " + AreaLists[ areaId ].Name + " Created");
}
return this.hAreas.get(areaId);
},
getRoomById : function(areaId, roomId) {
if (this.hAreas.get(areaId) !== null && this.hAreas.get(areaId)[ roomId ] !== undefined)
{
//I can get here...
//alert(uneval(this.hAreas.get(areaId)));
// returns: ({'000001':{areaId:"000001", roomId:"000001", Informations:{Name:"Room 1"}}, '000002':{areaId:"000001", roomId:"000002", Informations:{Name:"Room 2"}}, '000003':{areaId:"000001", roomId:"000003", Informations:{Name:"Room 3"}}})
//alert(roomId);
// returns: 000003
return this.hAreas.get(areaId)[ roomId ];
// returns: undefined
}
},
reloadAreas : function(areaId) {
//Triggered by Tick only if there is no players
},
addCharacter : function(areaId, roomId, character) {
if (this.hAreas.get(areaId) && this.hAreas.get(areaId)[ roomId ])
{
this.hAreas.get(areaId)[ roomId ].addCharacter(character);
}
},
removeCharacter : function(areaId, roomId, character) {
return this.hAreas.get(areaId)[ roomId ].removeCharacter(character);
}
});
var Room = new JS.Class({
hDoors : new JS.Hash([]),
hExits : new JS.Hash([]),
hBodies : new JS.Hash([]),
hObjects : new JS.Hash([]),
initialize : function(areaId, roomId, Informations) {
this.areaId = areaId;
this.roomId = roomId;
this.Informations = Informations;
//TODO: Load from external sources
if (areaId == "000001" && roomId == "000003")
{
this.hObjects.put("000001", new Objects("000001", { "Name" : "A table", "Type" : 0 }));
this.hObjects.put("000002", new Objects("000002", { "Name" : "A water fountain", "Type" : 1 }));
}
appendConsole("Room: #" + this.areaId + "-" + this.roomId + " : " + this.Informations.Name + " Created");
},
addCharacter : function(character) {
this.hBodies.put(character.characterId , character);
character.onArriveRoom(this);
if (! character.Informations.Stealth)
{
//TODO: Broadcast Informations to others
appendConsole(character.Informations.Name + " has arrived to " + this.Informations.Name);
}
},
removeCharacter : function(character) {
var characterId = (typeof character == "object") ? character.characterId : character,
currentCharacter = this.hBodies.remove(characterId);
character.onLeaveRoom(this);
if (currentCharacter !== null)
{
//TODO: Broadcast Informations to others
appendConsole(character.Informations.Name + " has left " + this.Informations.Name);
return currentCharacter;
}
return undefined;
},
onArrive : function() {
},
onLeave : function() {
},
getObjects : function(objectId, hash) {
if (this.hObjects.get(objectId))
{
var currentObjects = this.hObjects.get(objectId);
if (hash)
{
return new JS.Hash([
currentObjects.objectId, currentObjects
]);
}
return currentObjects;
}
return this.hObjects;
},
toString : function(characterId) {
}
});
var Objects = new JS.Class({
objectsTypes : {
0 : "lies",
1 : "stands"
},
initialize : function(objectId, Informations) {
this.objectId = objectId;
this.Informations = Informations;
appendConsole("Object: #" + this.objectId + " : " + this.Informations.Name + " Created");
},
toString : function() {
return this.Informations.Name + " " + this.objectsTypes[ this.Informations.Type ] + " here.";
}
});
var Character = new JS.Class({
Pet : undefined,
initialize : function(characterId, Informations) {
this.characterId = characterId;
this.Informations = Informations;
this.areaId = this.Informations.Zone.split("-")[ 0 ];
this.roomId = this.Informations.Zone.split("-")[ 1 ];
if (this.Informations.Pet !== undefined)
{
//TODO: Load from external sources
if (this.Informations.Pet === "000001")
{
this.Pet = new Pet("000001", { "Name" : "Molten Panther", "Zone" : this.areaId + "-" + this.roomId, "Stealth" : false });
World.addCharacter(this.Pet.getArea() , this.Pet.getRoom() , this.Pet);
var petRoom = World.getRoomById(this.Pet.getArea() , this.Pet.getRoom());
alert(petRoom); // = undefined ????
}
}
appendConsole("Character: #" + this.characterId + " : " + this.Informations.Name + " Created");
},
onArriveRoom : function (currentRoom) {
},
onLeaveRoom : function(currentRoom) {
},
onArrive : function() {
},
onLeave : function() {
},
getRoom : function() {
return this.roomId + "";
},
getArea : function() {
return this.areaId + "";
},
getInformations : function() {
return this.Informations;
},
hasPet : function() {
return (typeof this.Pet == "object");
},
getPet : function() {
return this.Pet;
},
equals : function(character) {
return (character instanceof this.klass) && character.Informations.Name === this.Informations.Name;
}
});
var Pet = new JS.Class(Character, {
initialize : function(characterId, Informations) {
this.callSuper();
appendConsole("Pet: " + this.Informations.Name + " Created");
}
});
//Tests
var World = new World();
var AreaOne = World.getAreas("000001");
var Cybrix = new Character("000001", { "Name" : "Cybrix", "Zone" : "000001-000003", "Stealth" : false, "Pet" : "000001" });
if (World.getAreas(Cybrix.getArea()))
{
World.addCharacter(Cybrix.getArea() , Cybrix.getRoom() , Cybrix);
//Cybrix = World.removeCharacter(Cybrix.getArea() , Cybrix.getRoom() , Cybrix);
}
});
</script>
</head>
<body style="margin: 0; padding: 0;">
<div id="console" style="display: block; background-color: #000; height: 100%; color: #FFF; font-family: Lucida Console;"></div>
</body>
</html>
然而,尽管它很大,请在StackOverflow上发布* entire *问题。实时链接对于一个问题来说是一个很好的*附件*,但总是在问题*中发布相关的代码*。两个原因。 1.人们不应该遵循链接来帮助你。 2. StackOverflow不仅适用于您,而且适用于将来也有类似问题的其他人。外部链接可以被移动,修改,删除等。通过确保相关代码在问题中,我们确保问题(及其答案)在合理的时间段内保持有用。 –
哦:并且不断尝试创建一个(更小),自包含的示例。 :-) –
@ T.J .:对第二条评论无法表示赞同;) –