I'm working on this video game with a bunch of monsters that run across the screen. The problem is at one point the monster goes past an area (goes above a y-coordinate) and has to go all the way back up to the top. I've received a suggestion to make this transfer from bottom to top delayed by a random amount of time (0, 1, 2, or 3 seconds). Right now, it just goes up to the top without any break whatsoever. My problem is implementing this break. I tried various methods such as stalling with a loop, as shown below
// if( monster is below 400 on y-axis
if( sbgBackFootY[ i ] >= 400 ) {
// random number that determines how long it will take to go to top
int randCo = ( int ) ( Math.random() * 3 );
if( randCo == 0 ) {
//moves monster to top
sbgHeadX[ i ] = 200;
sbgHeadY[ i ] = 80;
sbgMouthX[ i ] = 206;
sbgMouthY[ i ] = 110;
sbgBackX[ i ] = 190;
sbgBackY[ i ] = 95;
sbgBackFootX[ i ] = 190;
sbgBackFootY[ i ] = 115;
sbgFrontFootX[ i ] = 197;
sbgFrontFootY[ i ] = 115;
sbgLeftEyeX[ i ] = 205;
sbgLeftEyeY[ i ] = 90;
sbgRightEyeX[ i ] = 215;
sbgRightEyeY[ i ] = 90;
sbgLeftEyebrowStartX[ i ] = 203;
sbgLeftEyebrowStartY[ i ] = 83;
sbgLeftEyebrowEndX[ i ] = 210;
sbgLeftEyebrowEndY[ i ] = 90;
sbgRightEyebrowStartX[ i ] = 220;
sbgRightEyebrowStartY[ i ] = 83;
sbgRightEyebrowEndX[ i ] = 215;
sbgRightEyebrowEndY[ i ] = 90;
}
if( randCo == 1 ) {
//loop is supposed to stall program
for( int w = 0; w <= 1000000000; w++ ){
SBGco[ i ]++;
}
if( SBGco[ i ] == 1000000000 ) {
//moves monster to top
sbgHeadX[ i ] = 200;
sbgHeadY[ i ] = 80;
sbgMouthX[ i ] = 206;
sbgMouthY[ i ] = 110;
sbgBackX[ i ] = 190;
sbgBackY[ i ] = 95;
sbgBackFootX[ i ] = 190;
sbgBackFootY[ i ] = 115;
sbgFrontFootX[ i ] = 197;
sbgFrontFootY[ i ] = 115;
sbgLeftEyeX[ i ] = 205;
sbgLeftEyeY[ i ] = 90;
sbgRightEyeX[ i ] = 215;
sbgRightEyeY[ i ] = 90;
sbgLeftEyebrowStartX[ i ] = 203;
sbgLeftEyebrowStartY[ i ] = 83;
sbgLeftEyebrowEndX[ i ] = 210;
sbgLeftEyebrowEndY[ i ] = 90;
sbgRightEyebrowStartX[ i ] = 220;
sbgRightEyebrowStartY[ i ] = 83;
sbgRightEyebrowEndX[ i ] = 215;
sbgRightEyebrowEndY[ i ] = 90;
}
}
But the computer computes too fast, so the w variable becomes 1000000000 way too fast. The other way to do this is by making threads and going Thread.sleep(randCo) or something, but I am trying to not multi-thread. Is there any other way I can do this?
You are probably better off precomputing a path for your sprite and then assign it a velocity. Then, during your "draw" step you compute the location based on the sprite's path, velocity, and time.