This site requires JavaScript, please enable it in your browser!
Greenfoot back
alinasomcutean
alinasomcutean wrote ...

2017/5/8

Wrong value

alinasomcutean alinasomcutean

2017/5/8

#
I have a variable in Die class which memorate the number where the die stops and i return it, so I can use it in another class, Personaj class. In the Die class the num variable keeps the correct number but when I access it from Personaj class and put it in nr variable, the nr doesn't memorate the right value. Why? This is from Die class: public static int num = 0; public int getNum() { return num; } And this from Personaj class: Die die = new Die(); int nr; nr = die.getNum(); What is wrong at my code?
danpost danpost

2017/5/8

#
You are creating a new Die object and getting a number from it instead of getting a number from the Die object that is already in your world.
alinasomcutean alinasomcutean

2017/5/8

#
And how I can take the number from the Die object that is in world?
alinasomcutean alinasomcutean

2017/5/8

#
I wrote like this: nr = MyWorld.die.getNum() but it still doesn't work
alinasomcutean alinasomcutean

2017/5/8

#
Can you please write me a part of the code? I have to finish it and I really don't know how to fix this problem... I think that the nr variable memorate more values and it does for each one all the instructions. Can I somehow to make that the intructions to be made just for the last value? Of course, after I resolve the problem with the right value (to memorate the right value in nr)
danpost danpost

2017/5/8

#
alinasomcutean wrote...
I wrote like this: nr = MyWorld.die.getNum() but it still doesn't work
This would be a better try:
nr = ((MyWorld)getWorld()).die.getNum();
alinasomcutean alinasomcutean

2017/5/8

#
It still doesn't work... int nr; if(Greenfoot.isKeyDown("space")) { nr = ((MyWorld)getWorld()).die.getNum(); System.out.println(nr); if( nr % 2 == 0) { setLocation(480, 580); } else { setLocation(50, 580); } } I wanted to see what number is displayed, so I wrote this instruction: System.out.println(nr); And after I press space once, more numbers are displayed. Why?
danpost danpost

2017/5/8

#
alinasomcutean wrote...
I wanted to see what number is displayed, so I wrote this instruction: System.out.println(nr); And after I press space once, more numbers are displayed. Why?
It is hard to say with the limited code you have supplied here. I can only presume that pressing the "space" key is not stopping the die from changing it value. I can be quite sure, however, that the numbers are coming from the die in the world (or its value would not change at all).
alinasomcutean alinasomcutean

2017/5/8

#
But I can't understand, if the getNum() return the right value, why nr don't receive the same value?
danpost danpost

2017/5/8

#
alinasomcutean wrote...
But I can't understand, if the getNum() return the right value, why nr don't receive the same value?
You can be quite sure that 'nr' is receiving the value returned from 'getNum()' if you are not getting any run-time errors. That being said, again, I can only presume that the value in the Die object is continuously changing and pressing of "space" key is not freezing its value.
alinasomcutean alinasomcutean

2017/5/8

#
You are right, because I observed that more than one value is displayed after the instruction "System.out.println(nr);" is execute once. What can I do to freez its value, so when "space" is pressed to keep only one value which will return?
You need to login to post a reply.