我已經做了相當多的搜索這個答案,和一些編輯,我不能得到它的工作。我認爲我的布爾方法getColor()有問題。我試圖從一個不同類的循環中使用它。我的布爾方法java
HERSE我的getColor方法,從AvatarPanel類
public boolean setColor(boolean good)
{
if (good == true)
what--;
avatars.get(0).setBackground(colors.get(what));
repaint();
if (good == false)
what++;
avatars.get(0).setBackground(colors.get(what));
repaint();
return good;
}
下面是從背景類,我試着在使用的getColor的attackPerformed方法。林不知道,如果我寫的方法錯了,或如果attackPerformed需要偵聽器。遊戲仍然正常運行,工作正常。我只是希望得到一些幫助,確保這些文件至少能夠正確書寫。
我決定展示背景構造函數可能是個好主意,因爲我認爲它是我需要調用attackPerformed的地方。我無法弄清楚我需要調用attackPerformed,但我很確定它會在後臺構造函數中。 attackPerformed方法如下所示,構造函數位於頂部。
public class Background extends JPanel implements Constants, ActionListener
{
private static final long serialVersionUID = 1L;
private final Color BACK_COLOR = Color.GRAY;
private ArrayList<Sprite> sprites;
private Avatar avatar;
/** Constructor */
public Background()
{
sprites = new ArrayList<Sprite>();
reset(0);
setBackground(BACK_COLOR);
KeyboardFocusManager manager =KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventDispatcher(new KeyEventDispatcher()
{
public boolean dispatchKeyEvent(KeyEvent event)
{
int x = 0, y = 0;
double angle = 0;
Point p = avatar.getPosition();
switch (event.getKeyCode())
{
case KeyEvent.VK_DOWN:
y=6;
angle=270;
break;
case KeyEvent.VK_UP:
y = -6;
angle = 90;
break;
case KeyEvent.VK_LEFT:
x = -6;
break;
case KeyEvent.VK_RIGHT:
x = 6;
angle = 180;
break;
}
avatar.setParameters();
p.x += x;
p.y += y;
avatar.setPosition(p);
avatar.setAngle(angle);
if (!getBounds().contains(avatar.getBounds()))
{
avatar.restore();
Toolkit.getDefaultToolkit().beep();
return true;
}
repaint();
return true;
}
});
Timer timer = new Timer(15, this);
timer.start();
} //end of constructor
public void attackPerformed()
{
for(Sprite s : sprites)
{
AvatarPanel panel = new AvatarPanel();
Rectangle r = new Rectangle(sprites.get(0).getBounds());
Rectangle p = new Rectangle(sprites.get(1).getBounds());
if(r.intersects(p))
{
panel.setColor(false);
}
}
}
public void actionPerformed(ActionEvent e)
{
for (Sprite s : sprites)
{
if (s instanceof Move)
{
s.setParameters();
for (int i=0; i<3; i++)
{
((Move)s).nextPosition();
if (!getBounds().contains(s.getBounds()))
{
((Move) s).moveFailed();
}
else break;
}
}
}
repaint();
}
public @Override void paintComponent(Graphics g)
{
super.paintComponent(g); /* Let the parent class do its painting */
for (Sprite sprite: sprites)
{
sprite.draw(g);
}
}
public void reset(int level)
{
Sprite sprite = null;
int x, y, angle;
double scale, min, max;
sprites.clear();
int[] figures = FIGURES[level];
for (int f=0; f<figures.length; f++)
{
for (int i=0; i<figures[f]; i++)
{
x = (int)(Math.random()*600 + 100);
y = (int)(Math.random()*400 + 100);
switch (f)
{
case 0:
sprite = avatar = new Avatar(x,y);
break;
case 1:
sprite = new Predator(x,y);
break;
case 2:
sprite = new Mine(x,y);
break;
case 3:
sprite = new Shield(x,y);
break;
}
angle = (int)(Math.random()*360);
sprite.setAngle(angle);
min = sprite.getMinScaleFactor();
max = sprite.getMaxScaleFactor();
scale = Math.random() * (max - min) + min;
sprite.setScale(scale);
sprites.add(sprite);
repaint();
}
}
}
}
您是否缺少setColor中的一些{}?如果您沒有使用{}中包含的語句塊來執行if語句,則只會有條件地運行第一條語句。所以現在,唯一有條件的是你的什麼 - 什麼是++行。每次調用setColor時,都要執行兩個setBackground和重繪步驟。 – 2013-03-16 19:14:45
在setColor方法中,使用正確的縮進級別或花括號。 – 2013-03-16 19:14:58
退貨聲明在哪裏?它接縫的一部分setColor方法缺失。 – MrSmith42 2013-03-16 19:17:22