Alla fine ho trovato questa soluzione che sembra funzionare :
codice:
private class Game extends Thread
{
@Override
public void run()
{
Energia.setValue(100);
boolean control=true;
boolean z;
while (control)
{
for (int c = 0; c < h; c++)
{
z=Colpitoame();
if((nemico[c].isVisible())&&(z==false))
{
fuocon[c].setVisible(true);
}
if (nemico[c].getLocation().x <= 0)
{
nemico[c].setLocation(800, nemico[c].getLocation().y);
fuocon[c].setLocation(nemico[c].getLocation().x, nemico[c].getLocation().y+40);
if((nemico[c].isVisible())&&(z==false))
{
fuocon[c].setVisible(true);
}
}
if (fuocon[c].getLocation().x <= 0)
{
fuocon[c].setLocation(nemico[c].getLocation().x, nemico[c].getLocation().y+40);
}
nemico[c].setLocation(nemico[c].getLocation().x - 1 , nemico[c].getLocation().y);
if (z==false)
{
fuocon[c].setLocation(fuocon[c].getLocation().x - 4 , fuocon[c].getLocation().y);
}
}
try
{
sleep(10);//velocità aerei nemici
}
catch (InterruptedException ex) { }
}
}
}
codice:
private JLabel[] nemico;
private JLabel[] fuocon;
public int h;
private void Avversari()
{
int x,y;
h = (int) ((6-2)*Math.random()+1);
this.nemico = new JLabel[h];
this.fuocon = new JLabel[h];
for (int i = 0; i < h; i++)
{
this.nemico[i] = new JLabel(new ImageIcon(getClass().getResource("/game/graphics/images/nemico.png")));
this.nemico[i].setVisible(true);
this.fuocon[i] = new JLabel(new ImageIcon(getClass().getResource("/game/graphics/images/fuoco nemico.png")));
this.fuocon[i].setVisible(false);
this.nemico[i].setSize(60, 75);
this.fuocon[i].setSize(40, 10);
y = (int) ((430-0+1)*Math.random());
this.nemico[i].setLocation(700 , y);
this.fuocon[i].setLocation(nemico[i].getLocation().x , nemico[i].getLocation().y +40);
this.jPanel1.add(this.nemico[i]);
this.jPanel1.add(this.fuocon[i]);
}
}
Voi che ne pensate ?
(ho modificato la procedura avversari aggiungendo al suo interno anche il fuoco nemico che è un label come i nemici, poi in Game lo faccio avanzare insieme ai nemici)