//////
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;
}
While loop problem
Page 1 of 15 Replies - 203 Views - Last Post: 15 November 2012 - 04:11 PM
#1
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.
Replies To: While loop problem
#2
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!
I mean change the condition of the while loop, but you can keep the while loop. Cheers!
#3
Re: While loop problem
Posted 15 November 2012 - 09:22 AM
This is ridiculous
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;
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;
#4
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
correction on my above post, i didnt mean to say for loop, ment your if statement within your while loop.
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.
#5
Re: While loop problem
Posted 15 November 2012 - 09:52 AM
no need to burn CPU cycle evaluating &&
make it in reverse with else
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 >
#6
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
private static int seconds=0;
private static boolean ghostalive
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote



|