The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
While loop problem
Discuss While loop problem in the Java Help forum on Dev Shed. While loop problem Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 14th, 2012, 10:20 PM
|
|
Contributing User
|
|
Join Date: Oct 2011
Posts: 43
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
|
|
|
While loop problem
Object is supposed to change mode (movement algorithm) depending on the time elapsed (switch btw chase or scatter). I created an while loop but the object moves only in one mode.
Code:
//////
private static int seconds=0;
private static boolean ghostalive;
protected static final int chaseMode = 0;
protected static final int scatterMode = 1;
protected static final int frightenedMode = 2;
static int mode; //initially ghost start in scatterMode
public Ghost(int x, int y, Maze maze){
super(x, y, maze);
futureDirection = 0;
timer = 0;
mode = getMode();
}
public static int getMode(){
mode=setMode();
return mode;
}
//LEVEL 1
//scatter for 7s
//chase for 20s
//scatter for 7s
//chase for 20s
//scatter for 5s
//chase for 20s
//scatter for 5s
//chase indefinite
public static int setMode(){
while(ghostalive){
mode = scatterMode;
if(seconds>7)
mode = chaseMode;//chaseMode=true;
if(seconds>27)
mode = scatterMode;
if(seconds>34)
mode = chaseMode;
if(seconds>54)
mode = scatterMode;
if(seconds>59)
mode = chaseMode;
if(seconds>79)
mode = scatterMode;
if(seconds>84)
mode = chaseMode;
seconds++;
} return mode;
}
|

November 14th, 2012, 11:04 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
when you say that some objects move I guess there's code in a separate thread? Otherwise this wouldn't make sense at all.
Your mistake is that you assume the "seconds" counter actually determines the number of seconds. But Java will process a loop iteration in an instance, so the whole thing is over before you even notice it. You'll need something like Thread.sleep() to actually wait a second.
And you should follow naming conventions. A "set" or "get" methods are supposed to do nothing but "set" and "get" a variable (like the name suggests). Any other logic should be named differently.
|

November 15th, 2012, 04:11 AM
|
|
|
Jacques1 is right here. Even though you named a variable 'seconds', does not imply that it actually *is* a second.
I ran your code, and the Ghost perfectly outputs the behaviour you expected in the command-line.
The issue you have here is that the code is executed in a split-second, also because it all is calculated in the constructor, resulting in that you only perceive the last mode of the Ghost. To resolve this: make the Ghost a Thread
There are some other issues, besides Jacques1 mentioned: - A get/set method should do only just that. put the Ghost logic in another method, for example: updateMode.
- Your current setMethod references and updates the variable 'mode' and returns it. It does not need to return it, since it is a variable accessible by the entire instance of Ghost. Therefore the code-part can be changed to
Code:
setMode() //or: updateMode()
- Change the large if-statement to an else-if construction, because only 1 clause can be true.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|