Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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 644 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2013-01-19 15: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
« Reply #1 - Posted 2013-01-19 15: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.

Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2013-01-19 15: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!
Legends of Yore - The Casual Retro Roguelike
Offline Longarmx
« Reply #3 - Posted 2013-01-19 16: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).

Offline Danny02
« Reply #4 - Posted 2013-01-19 16: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

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #5 - Posted 2013-01-19 16:15:46 »

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

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Reply #6 - Posted 2013-01-19 16: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
« Reply #7 - Posted 2013-01-19 16: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.

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2013-01-19 16: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  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (56 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (44 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!