【问题标题】:How to process actors如何处理演员
【发布时间】:2014-04-02 20:51:56
【问题描述】:

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

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

    }

【问题讨论】:

标签: java arraylist timer jpanel multidimensional-array


【解决方案1】:

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

public int changex(int x1){

    x = x1;
    return x;
}

public int changey(int y1){

    y = y1;
    return y;
}

【讨论】:

  • 非常感谢,现在感觉自己像个白痴,因为我在我的好人课上也有同样的方法,但我做对了哈哈
猜你喜欢
  • 2013-10-21
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 2016-05-15
  • 2010-10-31
相关资源
最近更新 更多