So I am making a side scroller and as an image gets too far to the left (off the screen) I dont want to draw it anymore. So I am using this code which works fine but it can not work with many images, only one.
private static boolean drawTree = true;
public static void tree(SpriteBatch batch, int treeTallX) {
if (drawTree)
batch.draw(Assets.treeTall, grassX + treeTallX, 90);
if (grassX + treeTallX < -200)
drawTree = false;
}
However since I want to make this work with multiple trees I attempt to do it like this:
public static void tree(SpriteBatch batch, int treeTallX, boolean drawTree) {
if (drawTree)
batch.draw(Assets.treeTall, grassX + treeTallX, 90);
if (grassX + treeTallX < -200){
drawTree = false;
System.out.println("It should be false");
}
}
I call the method with this code
public class Levels {
public static boolean drawTree1 = true;
public static void levelOne(SpriteBatch batch){
//other level drawing stuff here....
Objects.tree(batch, 402, drawTree1);
}
}
Although “It should be false” prints out the boolean isnt being set to false so the image is still drawn Is this the wrong way to do this? is there something in libgdx that lets me undraw something? or is this the correct way to do it and am i just doing it wrong…