Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (290)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problems with booleans in methods  (Read 325 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345
« Posted 2013-01-19 16:41:56 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
   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:
1  
2  
3  
4  
5  
6  
7  
8  
   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
1  
2  
3  
4  
5  
6  
7  
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 Sad 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.... Sad
Offline Longarmx

Senior Member


Medals: 4
Projects: 1



« Reply #1 - Posted 2013-01-19 16:56:59 »

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.

Thread.sleep(∞);

Color Game
Offline wreed12345
« Reply #2 - Posted 2013-01-19 16:59:15 »

How do I save a
variable...?
I thought it would save since the Boolean isn't inside the tree method
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline Longarmx

Senior Member


Medals: 4
Projects: 1



« Reply #3 - Posted 2013-01-19 17:01:23 »

How do I save a
variable...?

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).

Thread.sleep(∞);

Color Game
Offline Danny02

JGO Knight


Medals: 36



« Reply #4 - Posted 2013-01-19 17:02:11 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
public void drawImage(Image img, int x)
{
    if(image.width + x > 0 && x < screenWidth)//so it is visible
   {
        image.draw(x);
    }

}
Offline wreed12345
« Reply #5 - Posted 2013-01-19 17:15:46 »

But it is called externally? The second and third code block go together
Offline Jimmt
« Reply #6 - Posted 2013-01-19 17:26:01 »

Even simpler if you're using libgdx
1  
2  
3  
4  
5  
6  
7  
...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.
so much static so much fail.
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.
Offline Longarmx

Senior Member


Medals: 4
Projects: 1



« Reply #7 - Posted 2013-01-19 17:26:18 »

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

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

Thread.sleep(∞);

Color Game
Offline Orangy Tang

JGO Kernel


Medals: 48
Projects: 11


Monkey for a head


« Reply #8 - Posted 2013-01-19 17:42:36 »

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

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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (62 views)
2013-05-17 21:29:12

alaslipknot (71 views)
2013-05-16 21:24:48

gouessej (102 views)
2013-05-16 00:53:38

gouessej (99 views)
2013-05-16 00:17:58

theagentd (108 views)
2013-05-15 15:01:13

theagentd (98 views)
2013-05-15 15:00:54

StreetDoggy (144 views)
2013-05-14 15:56:26

kutucuk (167 views)
2013-05-12 17:10:36

kutucuk (166 views)
2013-05-12 15:36:09

UnluckyDevil (175 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.167 seconds with 21 queries.