wreed12345
|
 |
«
Posted
2013-03-24 18:38:30 » |
|
So I have been trying to fix this for nearly an hour now, but I am truly clueless.... In my class MainGame i define 2 floats: 1 2
| private float levelX = -500; private float levelY = -500; |
and I also define a getter for both of those. When you press the up arrow key the levelY is decreased (using libgdx so it may be irregular seeing something like this) And I do the same for the rest of my arrow keys. Now the problem is when I am trying to use the value of the levelX or levelY in another class it always returns as -500 even though if I print out the value in the MainGame class it is changed from that original -500 but not in the other class.... Is this some stupid error I am not picking up on here or is it something deeper ? 
|
|
|
|
matheus23
|
 |
«
Reply #1 - Posted
2013-03-24 18:40:42 » |
|
How are you incrementing/decreasing those values?
This is definitely a language-problem.
|
|
|
|
wreed12345
|
 |
«
Reply #2 - Posted
2013-03-24 18:42:00 » |
|
pretty much its just this for each key: 1 2 3 4 5
| if ((Gdx.input.isKeyPressed(Keys.W))) { levelY -= playerSpeed; up = true; } else up = false; |
where
|
|
|
|
Games published by our own members! Check 'em out!
|
|
ra4king
|
 |
«
Reply #3 - Posted
2013-03-24 18:45:42 » |
|
Are you also updating the values in the other class?
|
|
|
|
Gamerulf
|
 |
«
Reply #4 - Posted
2013-03-24 18:45:53 » |
|
1 2 3 4 5 6 7 8 9 10 11
| public float getLevelY() { System.out.println("getY called"); return levelY; }
public float getLevelX() { System.out.println("getX called"); return levelX; } |
Then run game and see if those methods are called. Im new to this so that's all I can come up with.
|
- Gamerulf
|
|
|
wreed12345
|
 |
«
Reply #5 - Posted
2013-03-24 18:51:43 » |
|
@ra4king I never knew I had to do that in past experiences it automatically did that... i think  @gamerulf both get statements are being called repeatedly when trying that
|
|
|
|
cubus
|
 |
«
Reply #6 - Posted
2013-03-24 18:58:15 » |
|
only 2 possible reasons for that:
1) the values get reset between changing and getting
2) you are changing the values in one instance and getting them from another instance print out the instance address in the key handler method and in the getters. i'm pretty sure they are not equal...
|
|
|
|
Gamerulf
|
 |
«
Reply #7 - Posted
2013-03-24 19:04:37 » |
|
This would be a whole lot easier if you'd show us the classes.
|
- Gamerulf
|
|
|
|
Sammidysam
|
 |
«
Reply #9 - Posted
2013-03-24 19:16:29 » |
|
I think you should pass levelX and levelY as an argument instead of creating a main instance. I feel like trouble awaits when you do that.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
wreed12345
|
 |
«
Reply #10 - Posted
2013-03-24 19:17:45 » |
|
I tried doing that earlier and it worked, although i was really curious of why this was not working out
|
|
|
|
Gamerulf
|
 |
«
Reply #11 - Posted
2013-03-24 19:21:36 » |
|
Passing it like an argument? I think im unsure of what an argument is. Like "main.levelX" ? 
|
- Gamerulf
|
|
|
ra4king
|
 |
«
Reply #12 - Posted
2013-03-24 19:22:16 » |
|
Err you seem to not understand OOP. I advise you to learn more about Java before making games.
In your code, you are never calling mainGame.update() inside Resource however you claimed to be calling it, which leads me to believe you are using a different instance in a separate class. This means you don't understand how different instances are separate from each other.
|
|
|
|
Sparky83
|
 |
«
Reply #13 - Posted
2013-03-24 19:22:31 » |
|
It is like ra4king says. Because you have a new instance of ressource in GameMain and in Ressource and you also create an instance of its own at the very beginning here: 1 2 3 4 5 6 7 8
| public class Resource {
public int x, y; int width = 64; int height = 58; boolean drawResource = true; float resourceTimer = 0; GameMain mainGame = new GameMain(); |
So you have two independent instances of GameMain.
|
|
|
|
wreed12345
|
 |
«
Reply #14 - Posted
2013-03-24 19:25:20 » |
|
@sparky83 you are definately right about that but how do I actually avoid creating a new instance of that? Do i have to use static getters / setters or something. I feel very foolish for not realizing this 
|
|
|
|
Sammidysam
|
 |
«
Reply #15 - Posted
2013-03-24 19:27:00 » |
|
I would base all game logic and rendering in separate classes than Main, and just use Main for the game loop.
|
|
|
|
wreed12345
|
 |
«
Reply #16 - Posted
2013-03-24 19:31:44 » |
|
but still, if i create one class containing Level with has levelX and levelY how do I acess those without creating new instances
|
|
|
|
Sammidysam
|
 |
«
Reply #17 - Posted
2013-03-24 19:33:09 » |
|
Arguments, I guess. I don't have a good enough grasp of your code base to suggest other alternatives.
|
|
|
|
Grunnt
|
 |
«
Reply #18 - Posted
2013-03-24 19:37:26 » |
|
I suggest you first do some Java tutorials as your questions are not related to game development but to Java and OO programming in general. A good place to start are the Java tutorials by Oracle: http://docs.oracle.com/javase/tutorial/
|
|
|
|
wreed12345
|
 |
«
Reply #19 - Posted
2013-03-24 20:53:50 » |
|
Does anyone have ideas on solving this? I feel like static Is a bad idea and in some cases arguments won't solve this...
|
|
|
|
vbrain
|
 |
«
Reply #20 - Posted
2013-03-24 20:57:57 » |
|
Does anyone have ideas on solving this? I feel like static Is a bad idea and in some cases arguments won't solve this...
Static is not a bad idea. You should really study more about Java before attempting a game.
|
|
|
|
Gamerulf
|
 |
«
Reply #21 - Posted
2013-03-24 21:03:36 » |
|
I had 150hours of programming in school before we even touched graphical/visual programming. And that doesnt include all the homework we had... And Im still lost 
|
- Gamerulf
|
|
|
wreed12345
|
 |
«
Reply #22 - Posted
2013-03-24 21:09:33 » |
|
Would you recommend diverging else over static?
|
|
|
|
Jimmt
|
 |
«
Reply #23 - Posted
2013-03-24 21:25:33 » |
|
Would you recommend diverging else over static?
What? What does else have to do with static?
|
|
|
|
wreed12345
|
 |
«
Reply #24 - Posted
2013-03-24 21:27:33 » |
|
Would you recommend anything* else over static, sorry auto correct changed it
|
|
|
|
Sammidysam
|
 |
«
Reply #25 - Posted
2013-03-24 21:33:20 » |
|
Well you could make lots of classes and work around that.
|
|
|
|
Jimmt
|
 |
«
Reply #26 - Posted
2013-03-24 21:39:51 » |
|
Shouldn't use static just because "normal" variables aren't working for you. levelX and levelY sound like iffy variables to make static, unless you only are ever going to have one level total (also, shouldn't you have a level class? and a player class?) Referring to the original question, are you somehow calling onCreate() excessively? By the method name I wouldn't think so but I'm only familiar with desktop libgdx. When is the getter called in relation to the setter?
|
|
|
|
Gamerulf
|
 |
«
Reply #27 - Posted
2013-03-24 22:53:32 » |
|
The other class doesnt store it. He uses main.getLevelX() as "values" for the conditions of an if-statement. If I remember correctly..
|
- Gamerulf
|
|
|
vbrain
|
 |
«
Reply #28 - Posted
2013-03-25 01:42:20 » |
|
Dear God, it sounds to me like you need to completely redesign and restructure your entire code.
|
|
|
|
wreed12345
|
 |
«
Reply #29 - Posted
2013-03-25 16:52:01 » |
|
@vbrain is my coding really that bad? 
|
|
|
|
|