[libGDX] Drawing sprites on top of TiledMap

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?

Another thing I noticed might be wrong:

I’m using the format

SpriteBatch.draw(Sprite, xPos, yPos, width, height);

but this one also exists:

Sprite.draw(SpriteBatch);

Which one is correct?

Both are fine to use.

I solved my original problem, it was a silly mistake where I hadn’t told the for loop how many times to loop properly, in order to draw all the tiles.
I also solved a coordinate grid problem between TMX maps and double for loop arrays.

Now I’m running into a tearing problem. Ive read all over threads here and other places about ways to fix this, but none seem to work.

Note: this only happens every once in a while, not consistent, but will stick like this when I stand in certain places.

I tried looking into using MathUtils.round on my player X,Y before using them to update the camera, but my camera is scaled. So if I pass it Integers, the camera is really jumpy.

Texture bleeding.

Here is how I fixed texture bleeding on my game. You might need to change bleed variable, since all my textures are 256x256 haven’t tested it.


	private void set(SpriteSheet sheet, float x0, float y0, float x1, float y1) {
		float ph = 1f / sheet.width; // texture pixel width
		float pv = 1f / sheet.height; // texture pixel height

		int bleed = 8;
		this.x0 = x0 + ph / bleed;
		this.y0 = y0 + pv / bleed;
		this.x1 = x1 - ph / bleed;
		this.y1 = y1 - pv / bleed;
		this.sheet = sheet;
		
	}

PS
this is just a cut out of my code, so…