1
\$\begingroup\$

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.

Instead of this:

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – ashes999
    Jul 13, 2014 at 9:08
  • \$\begingroup\$ 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; \$\endgroup\$ Jul 13, 2014 at 9:09
  • \$\begingroup\$ got it. had to add tiles = new Texture[10]; inside public void create(){ } \$\endgroup\$ Jul 13, 2014 at 9:48

2 Answers 2

1
\$\begingroup\$

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};
\$\endgroup\$
5
  • \$\begingroup\$ 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" \$\endgroup\$ Jul 13, 2014 at 5:45
  • \$\begingroup\$ Can you post the code you used to initialize tile array? \$\endgroup\$ Jul 13, 2014 at 5:50
  • \$\begingroup\$ When you are creating an array, you will type "Texture[] tile" and not "Texture tile[]" i think thats the mistake you are doing \$\endgroup\$ Jul 13, 2014 at 6:05
  • \$\begingroup\$ both ways fail... \$\endgroup\$ Jul 13, 2014 at 8:39
  • \$\begingroup\$ It is not the ways that are failing...if its not working, then YOU are doing something wrong. \$\endgroup\$ Jul 14, 2014 at 10:32
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ That's a bit beyond my comprehension to be honest, I added a bit more of what I'm trying to do above. \$\endgroup\$ Jul 13, 2014 at 8:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .