Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

javascript - 从函数返回数组(Return array from function)

--Solved by Elliot B. Thanks!

( - 由Elliot B解决。谢谢!)

May also take int account the other modifications.

(也可以考虑其他修改。)

Here is the result.

(这是结果。)

Thanks, everyone, for the speedy answers!

(谢谢大家,快速回答!)

http://dl.dropbox.com/u/18785762/Rust/index.html

(http://dl.dropbox.com/u/18785762/Rust/index.html)

I'm writing a game in javascript, and I want to keep the files for matching block IDs to files in a seperate .js file from the map compiler, so that I can edit things easily.

(我正在用javascript编写一个游戏,并且我希望将用于匹配块ID的文件保存到地图编译器中单独的.js文件中的文件,以便我可以轻松编辑。)

However, the IDs are stored in an array, and I can't seem to get it to use the return function properly.

(但是,ID存储在一个数组中,我似乎无法正确使用返回函数。)

Any help?

(有帮助吗?)

drawmap.js:

(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:

(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;
}
  ask by Aido translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

At a minimum, change this:

(至少,改变这个:)

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;
}

To this:

(对此:)

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;
}

There are a couple fixes to point out.

(有几点需要指出。)

First , images is not defined in your original function, so assigning property values to it will throw an error.

(首先images未在原始函数中定义,因此为其指定属性值将引发错误。)

We correct that by changing images to IDs .

(我们通过将images更改为IDs来纠正此问题。)

Second , you want to return an Object , not an Array .

(其次 ,您想要返回一个Object ,而不是一个Array 。)

An object can be assigned property values akin to an associative array or hash -- an array cannot.

(可以为对象分配类似于关联数组或散列的属性值 - 数组不能。)

So we change the declaration of var IDs = new Array();

(所以我们改变var IDs = new Array();的声明var IDs = new Array();)

to var IDs = new Object();

(to var IDs = new Object();)

.

(。)

After those changes your code will run fine, but it can be simplified further .

(在这些更改之后,您的代码将运行正常,但可以进一步简化 。)

You can use shorthand notation (ie, object literal property value shorthand) to create the object and return it immediately:

(您可以使用简写表示法(即,对象文字属性值简写)来创建对象并立即返回它:)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...