Question

Hi I'm currently trying to make a picross game with libdgx and I'm having trouble drawing images to the screen from within a class. Just wondering what parameters I have to pass a draw/render function within the class, the code I have so far is below. I'm fairly new to programming with libdgx so any help would be greatly appreciated. Thanks

package PicrossGameObjects;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Tile { 

    Texture BlankTile;
    Texture RedTile;
    int xCoord;
    int yCoord;

public Tile(int x, int y){
    BlankTile = new Texture("BlankTile.png");
    RedTile = new Texture("RedTile.png");
    xCoord = x;
    yCoord = y;
}

public void Render(SpriteBatch batch){      

}
    }
Was it helpful?

Solution

You would do something like this:

boolean isBlank = true;

public void render(SpriteBatch batch){

    if(isBlank)
    batch.draw(BlankTile,xCoord,yCoord);
    else
    batch.draw(RedTile,xCoord,yCoord);

}

Notice that you need a boolean variable to decide what kind of tile you want to draw. Also loading a new texture for every tile should have a really bad effect for performance. You should read about using TextureAtlas, AssetManager. Or just pass your Texture as a parameter to your Tile instead of creating new one every time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top