0

I'm building a simple 2D Tile-map HTML5... and I have images to use. Is tehre any way to turn

var imageObj = new Image();

Into an array so I don't have to do manual variables?

edit: I tried doing Array but I couldn't find the src. Wait.. I was like.. an Array doesn't have a src.. Hmmm.

1
  • Do you mean you want to turn the an Image object in to an Array object? Why would you want to do that? Of course an Array wouldn't have a src property. Commented Apr 20, 2012 at 21:46

2 Answers 2

2

Why don't you make an array

var images = [];

and push all your Images to that array?

images.push(new Image());
Sign up to request clarification or add additional context in comments.

2 Comments

array()? This is not PHP. Use var images = []; instead,
Wow. I am silly. I looked though my other projects and they ahd this. Ugh.
2

Do a multidimensional array, like this:

var map = [];

for(var x = 0; x<MAP_WIDTH; x++) {
    map[x] = [];
    for(var y = 0; y<MAP_HEIGHT; y++) {
        map[x][y] = new Image();
    }
}

You can then access your images with map[X][Y] where X and Y are the coordinates of the tile you want to access.

Minor note: Javascript doesn't support true multidimensional arrays, but this is close enough. It's actually a nested array, but yeah... probably more than you needed to know.

1 Comment

And oh, you just want references to images, not a map made of images... facepalm Anyhow, this might come in handy at a future point, so I'll leave it up here.

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.