Hello! Im new to libgdx, and having some problems with drawing sprites over a tiled map. I got it to work successfully with the player sprite, but other sprites locked on map grid I cannot get to draw.
First: the explanation.
I am drawing a whole map of tiles, but some of the tiles need to be “crop” tiles, where the player can plant seeds, farm, etc and have the plant image update for differing conditions (watered/not watered/growth level).
What I am doing at the moment is setting a specific tile property to “crop” in Tiled, then creating a new CropTile in the same position.
...
cropLayer = (TiledMapTileLayer) map.getLayers().get("Tile Layer 1");
...
private void dealWithCrops() {
for (int row = 0; row < cropLayer.getHeight(); row++) {
for (int col = 0; col < cropLayer.getWidth(); col++) {
if (cropLayer.getCell(row, col)
.getTile()
.getProperties()
.containsKey("crop")) {
cropList.newCrop(row, col, LEVELID);
}
}
}
}
My Crop constructor looks like this
public CropTile(int x, int y, int mapID) {
this.x = x;
this.y = y;
int mapID1 = mapID;
cropSprite = new Sprite();
backGroundSprite = new Sprite();
cropSprite.setSize(1, 1);
cropSprite.setPosition(x, y);
backGroundSprite.setSize(1, 1);
backGroundSprite.setPosition(x, y);
Next comes a bunch of logic to determine which texture the sprite gets with sprite.setTexture(Texture). ( I am sure this bit is properly giving the sprites textures)
Then I later try to draw them with
public void render(SpriteBatch sb, OrthographicCamera cam) {
setSpriteImages();
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(backGroundSprite, x, y, 1, 1);
sb.draw(cropSprite, x, y, 1, 1);
sb.end();
}
I think my problem might have to do with mixing coordinate planes, IE: libGDX uses bottom left (0,0) while my code is implying (0,0) top left? I understand more code is probably needed, please let me know what to share. I got this method to work well with the player Sprite, I just cant seem to figure out many tiles.
What is the best way to have drawn tiles on top of TiledMapRenderer? Should I be editing the tiles within the TMX file, or is my method acceptable? Should I be passing one SpriteBatch and Camera around everywhere?