2014-01-09 65 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我希望它适合你.. –