Problems with booleans in methods

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 :frowning: 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… :frowning:

The reason is because you keep calling the method with a true variable. The method doesn’t save the variable internally. It prints out because that part of the code is run, just none of the variables are saved.

Otherwise known as scope.

How do I save a
variable…?
I thought it would save since the Boolean isn’t inside the tree method

You need to save it externally, outside of your method. For example, drawTree1 (I don’t actually mean save it in a file, just in memory).

so much static so much fail.

sry but you have to look up how java handles variables.

why can’t you just do stuff like this:


public void drawImage(Image img, int x)
{
    if(image.width + x > 0 && x < screenWidth)//so it is visible
    {
        image.draw(x);
    }

}

But it is called externally? The second and third code block go together

Even simpler if you’re using libgdx

...render(float delta){

batch.begin();
if(image.width + x > 0 && x < screenWidth){
image.draw(batch);
}
batch.end();

You need to learn scope (http://www.java-made-easy.com/variable-scope.html) because otherwise you’ll keep on having these problems.

Indeed. I don’t understand why your tree() method and your boolean variable is static in the first place. We use statics (methods, variables) to be able to call them without creating an instance. Checking if the image gets off the screen certainly should not be static, as it is dependent on the x/y.

But you never change the external variable. Add a return statement to your method.

I think he’s expecting pass-by-reference behaviour with the drawTree1 bool. But I’m too tired to properly explain it with all the proper cavets and terminology, so I’ll just say go google how Java handles primitives vs. objects as method parameters.