如何处理演员

时间:2014-04-02 20:51:56

标签: java arraylist timer jpanel multidimensional-array

好吧,所以我有一个badguy类,在那个类中我创建了一个随机数生成器和方法来向上,向下,向左或向右移动图标,然后我将它包装在Swing计时器中以便它不断重复,但是我的badguy不动,当我向他开枪时他立即重生了任何想法?这是我的代码谢谢:)

public class BadGuy extends WizardBattleGrid {
    ArrayList <BadGuy> b = new ArrayList<BadGuy>();
    private int x;
    private int y;
    private ImageIcon bad;
    Timer timer;


    public BadGuy(int x1, int y1, ImageIcon b){

        x = x1;
        y = y1;
        bad = b;
    }

    public int getx(){

        return x;
    }

    public int gety(){

        return y;
    }

    public ImageIcon getIcon(){

        return bad;
    }


    public int changex(int x1){

        x1 = x;

        return x;
    }

    public int changey(int y1){

        y1 = y;

        return y;
    }



    public void spawnEnemy(int x, int y){

        if(y < 10){
            return;
        }
        WizardCells[x][y].setIcon(BadGuyWizard());
    }

    public void removeEnemy(int x, int y){
        if(y < 10){
            return;
        }

        WizardCells[x][y].setIcon(null);
    }



    public void moveUp(){
        if(this.getx() != 0){

            WizardCells[this.getx()][this.gety()].setIcon(null);
            WizardCells[this.changex(this.getx()-1)][this.gety()].setIcon(this.getIcon());

        }

    }

    public void moveDown(){

        if(this.getx() != 19){

            WizardCells[this.getx()][this.gety()].setIcon(null);
            WizardCells[this.changex(this.getx()+1)][this.gety()].setIcon(this.getIcon());
        }

    }

    public void moveRight(){

        if(this.gety() != 19){

            WizardCells[this.getx()][this.gety()].setIcon(null);
            WizardCells[this.getx()][this.changey(this.gety()+1)].setIcon(this.getIcon());

        }

    }

    public void moveLeft(){

        if(this.gety() != 10){

            WizardCells[this.getx()][this.gety()].setIcon(null);
            WizardCells[this.getx()][this.changey(this.gety()-1)].setIcon(this.getIcon());
        }

    }

    public void move(){
    int delay = 100;    
    b.add(bgw);

    timer = new Timer(delay, new ActionListener(){

        public void actionPerformed(ActionEvent e){
            int movechoice = (int) (Math.random() *4);

            for(BadGuy a : b){
                if(movechoice == 0){
                    a.moveUp();
                }

                else if(movechoice == 1){
                    a.moveDown();
                }

                else if(movechoice == 2){
                    a.moveLeft();
                }

                else if(movechoice == 3){
                    a.moveRight();
                }
                System.out.println(movechoice);
            }

    }

});
    timer.start();


    }



}

我的网格类:

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);

    }

1 个答案:

答案 0 :(得分:0)

您的问题出在changex()和changey()方法中。你所拥有的只是返回当前的x或y值。它应该是:

public int changex(int x1){

    x = x1;
    return x;
}

public int changey(int y1){

    y = y1;
    return y;
}