Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return array from function

--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;
}
like image 913
Aido Avatar asked Aug 21 '12 16:08

Aido


People also ask

Which function returns a reference to array?

Correct Option: A 'fetchrow_arrayref()' returns a reference to an array of row values.

Can a function return a value of type array?

Finally a function can never returns an array.


3 Answers

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 Array(); to var IDs = new Object();.

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"
    };
}
like image 124
Elliot B. Avatar answered Oct 19 '22 21:10

Elliot B.


Your BlockID function uses the undefined variable images, which will lead to an error. Also, you should not use an Array here - JavaScripts key-value-maps are plain objects:

function BlockID() {
    return {
        "s": "Images/Block_01.png",
        "g": "Images/Block_02.png",
        "C": "Images/Block_03.png",
        "d": "Images/Block_04.png"
    };
}
like image 14
Bergi Avatar answered Oct 19 '22 19:10

Bergi


neater:

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

or just

var images = {
  "s":"Images/Block_01.png",
  "g":"Images/Block_02.png",
  "C":"Images/Block_03.png",
  "d":"Images/Block_04.png"
}
like image 8
mplungjan Avatar answered Oct 19 '22 21:10

mplungjan