2011-08-27 39 views
0

我正在寫一個彈跳球類(我剛剛開始使用Java),它的工作原理,但它看起來像每當球碰到一個邊緣的包含框變得更大爪哇Contotal框似乎變得越來越大

代碼反彈觸發

int i = 0; 
    int j = 0; 
    int k = 0; 
    double x = 3; 
    double y = 3; 
    while(i < 200){ 
     if(j == 0){ 
      x += 2; 
     } else { 
      x -= 2; 
     } 
     if(k == 0){ 
      y += 4; 
     } else { 
      y -= 4; 
     } 
     if(thisEye.getX() > 400){ 
      j = 1; 
     } 
     if(thisEye.getX() < 0) { 
      j = 0; 
     } 
     if(thisEye.getY() > 200){ 
      k = 1; 
     } 
     if(thisEye.getY() < 0) { 
      k = 0; 
     } 
     Color someShade = rGen.nextColor(); // var for the color of the eyes 
     Color someOtherShade = rGen.nextColor();// var for the color of the pupules 
     moveThis(thisEye,x,y,someShade);  // Go over all the shapes with the same X,Y offset - var x,y 
     moveThis(thisPupul,x,y,someOtherShade); 
     moveThis(thisEye2,x,y,someShade); 
     moveThis(thisPupul2,x,y,someOtherShade); 
     moveThis(face,x,y,rGen.nextColor()); 
     moveThis(nose,x,y,rGen.nextColor()); 
     pause(150.0); // wait for next cycle to slow down the images 
     //i++; this was to see if it just got farther off with each loop, it dose. 
    } 
} 

private void moveThis(GOval subject, double x, double y, Color newShade){ 
    subject.setFillColor(newShade); 
    subject.move(x, y); 
} 

的「面子」每次反彈它越走越遠離開屏幕報時會彈開正確的點一次,然後是迄今爲止它僅是在屏幕上然後從對方那一小段時間告訴它在屏幕上回來了一下。

我將它標記爲家庭作業,但我是當天的html/php編碼器,晚上只是使用iTunesU Stanford視頻。 但我想學習,所以指針會很棒。

+0

對不起,我無法對你的帖子做任何事情......只要你不說明你的代碼片段與你的感知問題有什麼關係,那麼你的代碼片段就毫無用處。哪些變量是你的邊界框?你如何看待它變大?什麼是j,k,x,y? –

+0

對不起,'thisEye'是彈跳盒,'x'和'y'是彈跳盒'j'的X,Y,'k'只是'0'或'1',這是'而'循環。 – webLacky3rdClass

+0

萬一它很重要我使用那裏進口,導入java.awt.Color; //添加要使用的顏色。 import acm.graphics。*; import acm.program。*; – webLacky3rdClass

回答

0

問題可能是(儘管不太可能)與您的theEye.getX()和Eye.getY()方法。如果你從

if(thisEye.getX() > 400){ 
    j = 1; 
} 
if(thisEye.getX() < 0) { 
    j = 0; 
} 
if(thisEye.getY() > 200){ 
    k = 1; 
} 
if(thisEye.getY() < 0) { 
    k = 0; 
} 

改變你的邊界條件檢查,

if(x > 400){ 
    j = 1; 
} 
if(x < 0) { 
    j = 0; 
} 
if(y > 200){ 
    k = 1; 
} 
if(y < 0) { 
    k = 0; 
} 

否則,請發表完整的源代碼,所以我們可以看到其他人可能這個問題會。