37

--Solved by Elliot B. Thanks! May also take into 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;
}
4
  • 1
    So what is not working properly. Are you getting errors in console log? Commented Aug 21, 2012 at 16:48
  • 2
    change images to IDs and it will work… Commented Aug 21, 2012 at 16:49
  • 4
    Instead of "new Array()" or "new Object()", I would use "[ ]" or "{ }" respectively. Commented Aug 21, 2012 at 16:54
  • 1
    I agree @ianpgall -hence my post :) Commented Aug 21, 2012 at 16:58

4 Answers 4

50

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"
    };
}
Sign up to request clarification or add additional context in comments.

4 Comments

One should use an object here, not an array.
Thank you! It works now. I'll accept the answer when the 15-minute cooldown is over.
@pimvdb is right. @AidanEdwards If you're not using integers for indices, Array() doesn't make sense.
@Aidan Edwards: Arrays are basically lists with indices (access them with [0] etc). Objects are generic key/value pairs, which is what you're using.
15

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

Comments

8

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

1 Comment

I want to keep the IDs in a separate file, I'll probable use the first one.
0

Taking in consideration that in JavaScript Array is object too this can be written as:

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

This code will display content of array in browser's window

window.onload=function(){
var s="";
var ar = BlockID(); //function return array
for(el in ar){
    s+=ar[el]+"</br>";
}
document.body.innerHTML=s; 
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.