从函数返回数组 [英] Return array from function

查看:148
本文介绍了从函数返回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- 埃利奥特B.由于解决了!
也可以采取INT占其他修改。

下面是结果。谢谢大家,为迅速解答! <一href=\"http://dl.dropbox.com/u/18785762/Rust/index.html\">http://dl.dropbox.com/u/18785762/Rust/index.html

我正在写在JavaScript中的游戏,我想保持这些文件在从地图编译一个单独的.js文件块ID匹配的文件,这样我就可以轻松地编辑的东西。然而,ID存储在一个数组中,我似乎无法得到它的正确使用返回功能。任何帮助吗?

drawmap.js:

 函数drawmap(){    变种图像= BLOCKID();    VAR水平= [
    ssssssssssssssssssssss
    sgggggggggCCCCCdddddss
    ssssssssss sssssss
    ];    VAR顶部= 100;
    变种左= 100;
    VAR mytop =顶部;
    VAR myleft =左;
    对于(Y = 0; Y&LT; level.length ++ Y){
        无功行=水平[Y];
        为(X = 0; X&下; row.length ++ x)的{
            变种C = row.charAt(X);
            如果(C!=''){
                img_create(图片[C],mytop,myleft);
            }
            mytop + = 13;
            myleft + = 27;
        }
        mytop =顶部+(Y + 1)* 13;
        myleft =左 - (Y + 1)* 27;
    }
}

MA pread.js:

 函数BLOCKID(){
    VAR的ID =新的Array();
        图像['S'] =图像/ Block_01.png
        图像['G'] =图像/ Block_02.png
        图像['C'] =图像/ Block_03.png
        图像['D'] =图像/ Block_04.png
    返回的ID;
}


解决方案

在最低限度,改变这种:

 函数BLOCKID(){
    VAR的ID =新的Array();
        图像['S'] =图像/ Block_01.png
        图像['G'] =图像/ Block_02.png
        图像['C'] =图像/ Block_03.png
        图像['D'] =图像/ Block_04.png
    返回的ID;
}

要这样:

 函数BLOCKID(){
    VAR的ID =新的对象();
        ID为['S'] =图像/ Block_01.png
        ID为['G'] =图像/ Block_02.png
        ID为['C'] =图像/ Block_03.png
        ID为['D'] =图像/ Block_04.png
    返回的ID;
}

有一对夫妇修复指出。 第一图片未在原有的功能定义,因此分配属性值就会抛出一个错误。我们纠正通过更改图片标识后,要返回对象,而不是阵列。可以将一个物体分配类似于关联数组或哈希的属性值 - 数组不能。所以我们改变 VAR的ID =新的对象()的声明; VAR的ID =新的Array();

在这些改变你的code将会运行得很好,但它可以进一步简化。您可以使用速记符号(即对象文本属性值的简写)来创建对象并立即返回它:

 函数BLOCKID(){
    返回{
            S:图像/ Block_01.png
            ,G:图像/ Block_02.png
            ,C:图像/ Block_03.png
            ,D:图像/ Block_04.png
    };
}

--Solved by Elliot B. Thanks! 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

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. However, the IDs are stored in an array, and I can't seem to get it to use the return function properly. Any help?

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

解决方案

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. We correct that by changing images to IDs. Second, you want to return an Object, not an 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 Object(); to var IDs = new Array();

After those changes your code will run fine, but it can be simplified further. You can use shorthand notation (i.e., 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"
    };
}

这篇关于从函数返回数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆