2014-01-09 64 views
0

我已經動畫了我的一些程序,它是一個移動的人,它的工作原理和一切。我有很多重複的代碼,所以我想試着讓它更有效率,並循環重複。我的問題是,即使支架適量它給我的錯誤下面編譯器錯誤使用循環,開關和案例

 public void DrawAstronaut(Graphics2D g2d) { 
    if (nViewDX == -1) { 
     DrawAstronautLeft(g2d); 
    } else if (nViewDX == 1) { 
     DrawAstronautRight(g2d); 
    } else { 
     DrawAstronautStand(g2d); 
    } 
} 

public void DrawAstronautLeft(Graphics2D g2d) { 
    switch (nAstroAnimPos) { 
    for(int i = 1; i <= 6; i++){ 
     case i: 
      g2d.drawImage(arimgAstroWalkLeft[i], nAstronautX + 1, nAstronautY + 1, this); 
      break; 
     default: 
      g2d.drawImage(imgAstroStandLeft, nAstronautX + 1, nAstronautY + 1, this); 
      break; 
     } 
} 
} 
    public void DrawAstronautRight(Graphics2D g2d) { 
    switch (nAstroAnimPos) { 
     for(int i = 1; i <= 6; i++){ 
     case i: 
      g2d.drawImage(arimgAstroWalkRight[i], nAstronautX + 1, nAstronautY + 1,            this); 
      break; 
     default: 
      g2d.drawImage(imgAstroStandRight, nAstronautX + 1, nAstronautY + 1, this); 
      break; 
     } 
    } 
} 

public void DrawAstronautStand(Graphics2D g2d) { 
    switch (nAstroAnimPos) { 
     default: 
      g2d.drawImage(imgAstroStandLeft, nAstronautX, nAstronautY, this); 
      break; 
} 
} 

幾乎所有的東西當我加入for循環的DrawAstronautLeft下面的一切了錯誤,它甚至不喜歡在公共無效DrawAstronautRight即使他們不應該有任何問題。我知道我有適量的括號,但有人可以幫助把事情放在正確的地方?

的錯誤包括: 不能夠找到符號 「的情況下,默認情況下,或‘}’預期」 「類,接口,或枚舉預期」

+3

始終複製/粘貼錯誤和異常輸出。我認爲你需要圍繞整個switch語句的循環。 –

+0

你的'switch-case'是多餘的..好吧..它總是'我'.... – Maroun

+0

因此,將開關放在for循環中,除了「case i:」之外的所有錯誤,謝謝@ AndrewThompson – BlueBarren

回答

1

你不需要開關。你可以修改你的循環與 -

for(int i = 0; i <= nAstroAnimPos; i++){ 
    if(i == 0) // Start with stand position 
     g2d.drawImage(imgAstroStandLeft, nAstronautX + 1, nAstronautY + 1, this); 
    else // Run the sequence from 1 to 6 
     g2d.drawImage(arimgAstroWalkLeft[i], nAstronautX + 1, nAstronautY + 1, this);  
} 

如果你想結束也站位置 -

for(int i = 0; i <= nAstroAnimPos + 1; i++){ 
    if(i == 0 || i == nAstroAnimPos + 1) // Start and end with stand position 
     g2d.drawImage(imgAstroStandLeft, nAstronautX + 1, nAstronautY + 1, this); 
    else // Run the sequence from 1 to 6 
     g2d.drawImage(arimgAstroWalkLeft[i], nAstronautX + 1, nAstronautY + 1, this);  
} 
+0

謝謝,這真的很有幫助。我不知道它是否工作正常,因爲我已經將我的圖像加載到數組中的方式無法正常工作,但我確信當我修復它時! ^。^ – BlueBarren

+0

@BlueBarren我希望它適合你.. –