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

2012/12/15

Confusing code problems

erdelf erdelf

2012/12/15

#
I have the variable speed to control the speed of my actor. The following code is written in the act method.
System.out.println(speed+"");
speed+=10;
the console with the printed lines says 1 every act cycle, but if I am inspecting the object, the speed is 11. So my two questions are: 1. why is the speed variable not changing? 2. why is another speed printed then inspected?
danpost danpost

2012/12/15

#
Please post the whole method that the code is in..
Gevater_Tod4711 Gevater_Tod4711

2012/12/15

#
Probably you are changing the value of speed two times: The first time is before the code you posted where you set the value of speed = 1 then this value is printed on the screen. The next time is in the code you posted but it's after printing it out and so there are different values that are printed out because if you inspect the object the act method isn't running. It still has happend. But thats just a guess. To be sure you should post the whole code.
erdelf erdelf

2012/12/15

#
@Gevater_Tod4711, I made a method before that with setting the speed to 1, I thought it would be executed only one time. thx for your help
You need to login to post a reply.