好吧所以我创建了一个随机移动敌人图标的方法,我使用Swing计时器使其不断重复并每隔一秒更新一次,但问题是图标没有移动它只是停留在该位置,我添加了一个打印方法来显示我的随机数字,它们正在被正确创建和显示,但是我的敌人没有移动,任何帮助都非常感激:)并且bluespell类的ImageIcon是一个jus返回常规的方法ImageIcon那个方法没什么问题ha
public class BlueSpell extends WizardBattleGrid{
private int BlueSpellx;
private static int BlueSpelly;
private ImageIcon Blue;
WizardBattleRunner wbr = new WizardBattleRunner();
Timer timer;
public BlueSpell(int x, int y, ImageIcon BlueSpell){
BlueSpellx = x;
BlueSpelly = y;
Blue = BlueSpell;
}
public int getx(){
return BlueSpellx;
}
public int gety(){
return BlueSpelly;
}
public ImageIcon getIcon(){
return Blue;
}
public int changex(int x){
BlueSpellx = x;
return x;
}
public int changey(int y){
BlueSpelly = y;
return y;
}
public void shootSpell(){
final BlueSpell b = new BlueSpell(GoodGuy.getx(), GoodGuy.gety() +1, BlueSpellWizard());
int delay = 60;
timer = new Timer(delay,new ActionListener(){
public void actionPerformed(ActionEvent e){
if(b.gety() != 19){
WizardCells[b.getx()][b.gety()].setIcon(null);
WizardCells[b.getx()][b.changey(b.gety()+1)].setIcon(b.getIcon());
}
else{
WizardCells[b.getx()][b.gety()].setIcon(null);
b.changex(GoodGuy.getx());
b.changey(GoodGuy.gety() +1);
timer.stop();
}
}
});
timer.start();
}
}
WizardCells在我的Grid类中,它们是:
public class WizardBattleGrid extends GameWindowWizard {
public static JLabel[][] WizardCells = new JLabel [20][20];
public static JPanel WizardPanel = new JPanel(new GridLayout(20, 20, 0, 0));
static BufferedImage cursorimage = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
static Cursor blankcursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorimage, new Point(0, 0), "blank cursor");
public static void CreateGrid(){
for(int i=0; i<WizardCells.length; i++ ){
for(int j=0; j<WizardCells[i].length; j++){
JLabel label = new JLabel();
WizardCells[i][j] = label;
}
}
for(int i=0; i<WizardCells.length; i++){
for(int j=0; j<WizardCells[i].length;j++){
WizardPanel.add(WizardCells[i][j]);
}
}
WizardCells[10][0].setIcon(GoodGuyWizardIcon());
WizardPanel.setOpaque(true);
WizardPanel.setBackground(Color.DARK_GRAY);
WizardPanel.setCursor(blankcursor);
gameWindow.add(WizardPanel);
}
}