# How do i initialize and use a Texture Array? LibGDX

First of all, I am new to Java and LibGDX..

What I am trying to do is Load my texture images into an array so that I can reference them in my drawing code by number, rather than by name. I am drawing a tile map.

tile1 = new Texture(Gdx.files.internal("grass.png"));
tile2 = new Texture(Gdx.files.internal("dirttile.png"));
tile3 = new Texture(Gdx.files.internal("watertile.png"));


I would like it to be more like:

tile[1] = new Texture(Gdx.files.internal("grass.png"));
tile[2] = new Texture(Gdx.files.internal("dirttile.png"));
tile[3] = new Texture(Gdx.files.internal("watertile.png"));


..so that later I can draw them by which tile I need..such as

tileType = worldMap[i][j]; //will be an int number
batch.draw(tile[tileType], tileX, tileY);


I can't figure out how to initialize array and load textures into it though in order to do this, without getting errors.

Basically I need to draw my map based on numbers I have in a tile map like this:

 int worldMap[][] = {
{ 1,1,1,1,1,1,1,1,1,1 } ,
{ 1,1,1,1,1,1,1,1,1,1 } ,
{ 1,1,1,1,1,1,1,1,1,1 } ,
{ 1,1,1,3,3,3,1,1,1,1 } ,
{ 1,1,1,3,3,3,1,1,1,1 } ,
{ 1,1,1,3,3,3,1,1,1,1 } ,
{ 1,1,1,1,1,1,1,1,1,1 } ,
{ 1,1,1,1,1,1,1,1,1,1 } ,
{ 1,1,1,1,1,1,1,1,1,1 } ,
{ 1,1,1,1,1,1,1,1,1,1 } ,
};


I need to draw the map tile based on the numbers in that map... 1 = grass 2 = dirt 3 = water etc.... Still no luck getting the array working.

• Maybe you should clarify specifically what errors you're getting, and what specifically you mean by "no luck getting the array working." We can't help you if we don't know what problem you're running into. – ashes999 Jul 13 '14 at 9:08
• this is what Eclipse tells me... "Exception in thread "LWJGL Application" java.lang.NullPointerException at com.firstApp.Test.MyGdxGame.create(MyGdxGame.java:82) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136) at com.badlogic.gdx.backends.lwjgl.LwjglApplication\$1.run(LwjglApplication.java:114) " .........line 82 refers to tile[1] = tile1; – Charlie Holden Jul 13 '14 at 9:09
• got it. had to add tiles = new Texture[10]; inside public void create(){ } – Charlie Holden Jul 13 '14 at 9:48

You can initialize tile1, tile2 and tile3 and then you can add them to Array.

tile1 = new Texture(Gdx.files.internal("grass.png"));
tile2 = new Texture(Gdx.files.internal("dirttile.png"));
tile3 = new Texture(Gdx.files.internal("watertile.png"));


Here is how you will initialize Array:

Texture[] tile = {tile1, tile2, tile3};

• how should I initialize the tile[] array..I am doing so as a Texture and I get "Type mismatch: cannot convert from Texture[] to Texture" – Charlie Holden Jul 13 '14 at 5:45
• Can you post the code you used to initialize tile array? – Zohaib Amir Jul 13 '14 at 5:50
• When you are creating an array, you will type "Texture[] tile" and not "Texture tile[]" i think thats the mistake you are doing – Zohaib Amir Jul 13 '14 at 6:05
• both ways fail... – Charlie Holden Jul 13 '14 at 8:39
• It is not the ways that are failing...if its not working, then YOU are doing something wrong. – Zohaib Amir Jul 14 '14 at 10:32

Having a list of textures in an array is not a great idea. What if you decide to put them into a texture atlas later (which is generally more efficient than loading a bunch of files)? What if you change the order of a file in the middle? You'll have to re-order a bunch of your data.

A more flexible approach would be to keep not just a flat file name or index, but a combination of the two; perhaps in a simple class, or a hash:

class Tile {
private String filename;
private int index;
}

// or ...

// filename => index
Map<String, int> textureIndicies = new HashMap<String, int>();


In any case, about your original problem: I suggest you use a dynamic class like ArrayList here instead of a flat array. Why?

• Easier to add and remove images from it
• Easy to get the element at a particular index
• Approximately the same average runtime performance as an array

The choice is yours, though, with what approach you go with.

• That's a bit beyond my comprehension to be honest, I added a bit more of what I'm trying to do above. – Charlie Holden Jul 13 '14 at 8:58