简体   繁体   English

JavaScript .. Image()对象是否为Array?

[英]JavaScript.. Image() object as Array?

I'm building a simple 2D Tile-map HTML5... and I have images to use. 我正在构建一个简单的2D Tile-map HTML5 ...,我可以使用图像。 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 . 编辑:我尝试做数组,但我找不到src Wait.. I was like.. an Array doesn't have a src .. Hmmm. 等等..我就像..数组没有src ..嗯。

Why don't you make an array 你为什么不做一个数组

var images = [];

and push all your Images to that array? 并将所有图像推送到该阵列?

images.push(new Image());

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. 然后,您可以使用map[X][Y]访问图像,其中X和Y是您要访问的图块的坐标。

Minor note: Javascript doesn't support true multidimensional arrays, but this is close enough. 小注释:Javascript不支持真正的多维数组,但这已经足够接近了。 It's actually a nested array, but yeah... probably more than you needed to know. 它实际上是一个嵌套数组,但是,是的……可能比您需要知道的更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM