test
test

Reputation: 18200

JavaScript.. Image() object as Array?

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.

Upvotes: 0

Views: 935

Answers (2)

Elliot Bonneville
Elliot Bonneville

Reputation: 53291

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.

Upvotes: 2

keune
keune

Reputation: 5795

Why don't you make an array

var images = [];

and push all your Images to that array?

images.push(new Image());

Upvotes: 2

Related Questions