34

——由 Elliot B 解决。谢谢!也可以考虑其他修改。

这是结果。谢谢大家,快速解答!http://dl.dropbox.com/u/18785762/Rust/index.html

我正在用 javascript 编写游戏,我想将用于匹配块 ID 的文件保存在地图编译器的单独 .js 文件中,以便我可以轻松地编辑内容。但是,ID 存储在一个数组中,我似乎无法让它正确使用返回函数。有什么帮助吗?

drawmap.js:

function drawmap() {

    var images = BlockID();

    var level = [
    "ssssssssssssssssssssss",
    "sgggggggggCCCCCdddddss",
    "ssssssssss     sssssss"
    ];

    var top = 100;
    var left = 100;
    var mytop = top;
    var myleft = left;
    for (y=0; y<level.length; ++y) {
        var row = level[y];
        for (x=0; x < row.length; ++x) {
            var c = row.charAt(x);
            if(c != ' ') {
                img_create(images[c], mytop, myleft);
            }
            mytop += 13;
            myleft += 27;
        }
        mytop = top + (y+1)*13;
        myleft = left - (y+1)*27;
    }
}

mapread.js:

function BlockID() {
    var IDs = new Array();
        images['s'] = "Images/Block_01.png";
        images['g'] = "Images/Block_02.png";
        images['C'] = "Images/Block_03.png";
        images['d'] = "Images/Block_04.png";
    return IDs;
}
4

4 回答 4

48

至少,改变这个:

function BlockID() {
    var IDs = new Array();
        images['s'] = "Images/Block_01.png";
        images['g'] = "Images/Block_02.png";
        images['C'] = "Images/Block_03.png";
        images['d'] = "Images/Block_04.png";
    return IDs;
}

对此:

function BlockID() {
    var IDs = new Object();
        IDs['s'] = "Images/Block_01.png";
        IDs['g'] = "Images/Block_02.png";
        IDs['C'] = "Images/Block_03.png";
        IDs['d'] = "Images/Block_04.png";
    return IDs;
}

有几个修复需要指出。首先images未在您的原始函数中定义,因此为其分配属性值将引发错误。我们通过更改images为 来纠正这一点IDs其次,你想返回一个Object,而不是一个Array。可以为对象分配类似于关联数组或哈希的属性值——数组不能。所以我们改变var IDs = new Array();to的声明var IDs = new Object();

在这些更改之后,您的代码将运行良好,但可以进一步简化。您可以使用速记表示法(即对象文字属性值速记)来创建对象并立即返回它:

function BlockID() {
    return {
            "s":"Images/Block_01.png"
            ,"g":"Images/Block_02.png"
            ,"C":"Images/Block_03.png"
            ,"d":"Images/Block_04.png"
    };
}
于 2012-08-21T16:48:25.720 回答
14

您的BlockID函数使用未定义的变量images,这将导致错误。此外,您不应该在Array此处使用 - JavaScript 键值映射是普通对象:

function BlockID() {
    return {
        "s": "Images/Block_01.png",
        "g": "Images/Block_02.png",
        "C": "Images/Block_03.png",
        "d": "Images/Block_04.png"
    };
}
于 2012-08-21T16:52:22.833 回答
8

更整洁:

function BlockID() {
  return {
    "s":"Images/Block_01.png",
    "g":"Images/Block_02.png",
    "C":"Images/Block_03.png",
    "d":"Images/Block_04.png"
   }
}

要不就

var images = {
  "s":"Images/Block_01.png",
  "g":"Images/Block_02.png",
  "C":"Images/Block_03.png",
  "d":"Images/Block_04.png"
}
于 2012-08-21T16:51:29.270 回答
0

考虑到在 JavaScript 中数组也是对象,这可以写成:

function BlockID() {
    return new Array(
        "Images/Block_01.png", 
        "Images/Block_02.png", 
        "Images/Block_03.png", 
        "Images/Block_04.png" 
       );
    }

此代码将在浏览器窗口中显示数组的内容

window.onload=function(){
var s="";
var ar = BlockID(); //function return array
for(el in ar){
    s+=ar[el]+"</br>";
}
document.body.innerHTML=s; 
};
于 2021-11-18T09:30:05.777 回答