我正在與一羣在屏幕上運行的怪物一起玩這個視頻遊戲。問題在於怪物經過某個區域(超過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變量變成10億太快。另一種方法是通過創建線程並使用Thread.sleep(randCo)或其他方法,但我試圖不使用多線程。有沒有其他辦法可以做到這一點?
不要以爲他可以睡覺。如果我理解他是正確的,那麼單線程渲染所有他的怪物,所以如果你線程睡眠,所有的怪物都會停下來(而不是僅僅是那些穿越邊界的怪物) – Michael
@Michael在我讀了第二個問題後,我想你對了。他應該選擇安德魯的解決方案。 – tibtof