我正在制作这个视频游戏,里面有一堆在屏幕上跑来跑去的怪物。问题是怪物有时会经过一个区域(超过 y 坐标)并且必须一直回到顶部。我收到了一项建议,让这种从底部到顶部的传输延迟随机时间(0、1、2 或 3 秒)。现在,它只是上升到顶部,没有任何中断。我的问题是实施这个休息。我尝试了各种方法,例如用循环停止,如下所示
// 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;
}
}
但是计算机计算得太快了,所以 w 变量变得 1000000000 太快了。另一种方法是创建线程并使用 Thread.sleep(randCo) 或其他方法,但我试图不使用多线程。我还有其他方法可以做到这一点吗?