5 Replies - 335 Views - Last Post: 15 November 2012 - 04:11 PM Rate Topic: -----

#1 hey92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-November 11

While loop problem

Posted 14 November 2012 - 09:19 PM

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.

	//////
	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;
	}



Is This A Good Question/Topic? 0
  • +

Replies To: While loop problem

#2 WolverineX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 11-November 12

Re: While loop problem

Posted 15 November 2012 - 01:54 AM

You defined "ghostalive" as a boolean, and you gave it no initialization therefore "ghostalive" will be false (By Default), that is why your while loop is not doing anything, because it's false from the beginning so it won't execute. Change your while loop and it should work!

I mean change the condition of the while loop, but you can keep the while loop. Cheers!
Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: While loop problem

Posted 15 November 2012 - 09:22 AM

This is ridiculous

	
	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;


You are telling us that if seconds is 85 or higher you will assign 7 times a value for nothing to the variable mode
so if seconds is 85 you will actually do

mode = scatterMode;
mode = chaseMode;//chaseMode=true;
mode = scatterMode;
mode = chaseMode;
mode = scatterMode;
mode = chaseMode;
mode = scatterMode;
mode = chaseMode;
Was This Post Helpful? 0
  • +
  • -

#4 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Re: While loop problem

Posted 15 November 2012 - 09:49 AM

your assigning mode to bounce between chase and scatter because you only have one condition for your for loop. try something like
if (seconds > 7 && seconds <= 26){
    mode = chaseMode;
}
etc, etc.

correction on my above post, i didnt mean to say for loop, ment your if statement within your while loop.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: While loop problem

Posted 15 November 2012 - 09:52 AM

no need to burn CPU cycle evaluating &&
make it in reverse with else

if(seconds > 84)
  mode = ...
else if(seconds | 79)
  mode = ...
[code]
or because you are in a loop
[code]
   if(seconds > 7) {
       mode = ...
       continue;
   }
   if(seconds >


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: While loop problem

Posted 15 November 2012 - 04:11 PM

actually now that I am back here dues of a complaint from WolverineX why those static variables

private static int seconds=0;
private static boolean ghostalive
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1