#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    43
    Rep Power
    3

    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;
    	}
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    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
      Code:
      mode=setMode();
      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.

IMN logo majestic logo threadwatch logo seochat tools logo