Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Values of a float not updating in classes besides the one it is defined in :(  (Read 1345 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2013-03-24 19: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 ? Sad

Offline matheus23

JGO Kernel


Medals: 98
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2013-03-24 19:40:42 »

How are you incrementing/decreasing those values?

This is definitely a language-problem.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2013-03-24 19: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
1  
playerSpeed = 2;

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #3 - Posted 2013-03-24 19:45:42 »

Are you also updating the values in the other class?

Offline Gamerulf

Junior Member


Medals: 2



« Reply #4 - Posted 2013-03-24 19: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
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #5 - Posted 2013-03-24 19:51:43 »

@ra4king I never knew I had to do that in past experiences it automatically did that... i think persecutioncomplex

@gamerulf
both get statements are being called repeatedly when trying that

Offline cubus

Junior Member


Medals: 2



« Reply #6 - Posted 2013-03-24 19: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...
Offline Gamerulf

Junior Member


Medals: 2



« Reply #7 - Posted 2013-03-24 20:04:37 »

This would be a whole lot easier if you'd show us the classes.

- Gamerulf
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #8 - Posted 2013-03-24 20:08:23 »

I am in the process of cleaning all of this code up so here is the main class:
http://pastebin.java-gaming.org/7ab3539984a
and the class im calling it from:
http://pastebin.java-gaming.org/ab359489a48
The latter needs to be re organized so please ignore that for now although it should be readable

Before everything in my game was in one class for some stupid reason, so i am in this spring cleaning mode lol

Offline Sammidysam
« Reply #9 - Posted 2013-03-24 20: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!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #10 - Posted 2013-03-24 20:17:45 »

I tried doing that earlier and it worked, although i was really curious of why this was not working out

Offline Gamerulf

Junior Member


Medals: 2



« Reply #11 - Posted 2013-03-24 20:21:36 »

Passing it like an argument? I think im unsure of what an argument is.

Like "main.levelX" ? Tongue

- Gamerulf
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #12 - Posted 2013-03-24 20: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.

Offline Sparky83

Senior Member


Medals: 6
Projects: 1



« Reply #13 - Posted 2013-03-24 20: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(); // HERE

So you have two independent instances of GameMain.
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #14 - Posted 2013-03-24 20: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  Emo

Offline Sammidysam
« Reply #15 - Posted 2013-03-24 20:27:00 »

I would base all game logic and rendering in separate classes than Main, and just use Main for the game loop.
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #16 - Posted 2013-03-24 20: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

Offline Sammidysam
« Reply #17 - Posted 2013-03-24 20:33:09 »

Arguments, I guess.  I don't have a good enough grasp of your code base to suggest other alternatives.
Offline Grunnt

JGO Wizard


Medals: 55
Projects: 9
Exp: 5 years


Complex != complicated


« Reply #18 - Posted 2013-03-24 20: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/

Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #19 - Posted 2013-03-24 21: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...

Offline vbrain
« Reply #20 - Posted 2013-03-24 21: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.
Offline Gamerulf

Junior Member


Medals: 2



« Reply #21 - Posted 2013-03-24 22: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 Tongue

- Gamerulf
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #22 - Posted 2013-03-24 22:09:33 »

Would you recommend diverging else over static?

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 118
Projects: 4
Exp: 3 years



« Reply #23 - Posted 2013-03-24 22:25:33 »

Would you recommend diverging else over static?
What?
What does else have to do with static?

Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #24 - Posted 2013-03-24 22:27:33 »

Would you recommend anything* else over static, sorry auto correct changed it

Offline Sammidysam
« Reply #25 - Posted 2013-03-24 22:33:20 »

Well you could make lots of classes and work around that.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 118
Projects: 4
Exp: 3 years



« Reply #26 - Posted 2013-03-24 22: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?
Offline Gamerulf

Junior Member


Medals: 2



« Reply #27 - Posted 2013-03-24 23: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
Offline vbrain
« Reply #28 - Posted 2013-03-25 02:42:20 »

Dear God, it sounds to me like you need to completely redesign and restructure your entire code.
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #29 - Posted 2013-03-25 17:52:01 »

@vbrain is my coding really that bad? Sad

Pages: [1] 2
  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.

xsi3rr4x (64 views)
2014-04-15 18:08:23

BurntPizza (62 views)
2014-04-15 03:46:01

UprightPath (75 views)
2014-04-14 17:39:50

UprightPath (58 views)
2014-04-14 17:35:47

Porlus (76 views)
2014-04-14 15:48:38

tom_mai78101 (101 views)
2014-04-10 04:04:31

BurntPizza (161 views)
2014-04-08 23:06:04

tom_mai78101 (256 views)
2014-04-05 13:34:39

trollwarrior1 (209 views)
2014-04-04 12:06:45

CJLetsGame (216 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!