[SOLVED] Return int from Method (derped)

Hey all, I’m implementing a MouseListener and, I’m trying to retrieve my maps height. But, For whatever reason I’m only getting the first digit when using a return method in my map class from my Mouse Listener class.

These are the three sections of code;

Level aka Map Class


private int levelHeight = 800;

public int get_LevelHeight() {
      return levelHeight;
}

Mouse Listener Class


h = Display.getLevel().get_LevelHeight(); // Returns 8 not 800

If anyone has any idea on what’s happening here I would greatly appreciate a hint :slight_smile:

How do you know it’s returning 8? Is that from a print, or what?

Yea, I put the following line of code in the mouse listener methods and, It returned 8.


System.out.println("Level Height = " + Display.getLevel().get_LevelHeight());

Make the variable final to show where it’s being modified, comment any of that out. Also make sure you don’t have another variable/method of the same name.

In addition, your method naming convention is different from the standard one - take a look at camelCase naming convention, especially if you will want to write library or open source project in the future. :slight_smile:

is there any code inside the “Level” class that deals with LevelHeight? It’s possible you’re accidentally setting it to 8 somewhere.

Is it always the first digit, or is it always 8? If you change [icode]private int levelHeight = 800;[/icode] to 700, does it output 7, or still 8?

This too, good formatting is a good habit. :slight_smile:

Some conventions never outweigh OCD. If I’m ever in a position to release a open source project, The chumps using it will have to suffer as do I.

Well, Truth be told, levelHeight is defined in a xml file. Which is then loaded into the level class. So what I don’t get is why my level loads at the appropriate height, But when I try to access it from my Mouse Listener class all I get is the number 8.

Is it possible that since you’re accessing it via Display.getLevel() (static method call) it’s returning the wrong instance of the object (EDIT: Or the instance of the object before the proper height is set) at that moment the mouse listener is requesting it?

I looked into that, The Mouse Listener is initialized in the init() method of Display. The map is loaded prior to the Display class and, Is passed to it. Either way, It’s only ever null or, 800.

It’s practically implemented the same as my Keyboard Listener class.

While you can do what you want, it might be a good idea to adhere to some nameing conventions like having classnames start uppercase and use propper named getters and setters for accessing properties. Simple for the fact, that IDEs support refactoring and code navigation then. Also there are templating, scripting and component libraries/frameworks that use reflection to access properties and might rely on the fact that getters and setters are named as expected.

Also I don’t know if being ignorant qualifies as OCD :stuck_out_tongue:

I’ll just be nice and, Recommend you work on your spelling.

While we’re correcting people, that would be a lower case R, and your comma would go before the and or not be there at all.

Now enough chitchat, have you figured out your problem yet?

I’ll just be nice, and recommend you get rid of your ignorant attitude. Cylab’s point was perfectly valid, and you should follow his advice.

Negative. Like I previously said, The map is defined by two variables of levelWidth an levelHeight. The map renders properly, Only the Mouse Listener class can’t grab the complete value of levelHeight.

If you havent already, make yourself familar with a debugger and place breakpoints on all locations where the value is written and inspect the value of the variable. The value will not magically change, so you have some buggy code changing the value somewhere. Should be easy to find with a debugger.

If you find spelling mistakes you can keep them this time :wink: Or you can show me your german spelling typing on a phone…

If you would have read my post, You would have noticed I mentioned using “System.out.println()”. I had these nested inside my if statements to debug where my code was failing. I resolved the issue, Marked the topic Solved.

I always recommend everyone here to learn using debuggers - they offer much more and faster means to find problems than System.outs (which have their uses).

Glad you solved the problem. What was it?

another good friend is the search tool , number of times that helps find rogue object changes is uncountable.