1

我目前正在制作游戏,但由于某种原因,某些 Graphics2D 组件没有移动并且程序最终崩溃。KeyListener 和移动之间是否有一些干扰?或者可能是因为我重新粉刷了所有移动中的面板?

我已经评论了代码的每个部分的作用。

这是代码:`

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;

public class Interface extends JPanel implements ActionListener, KeyListener{

Timer timer;

Player p;
Alien[] a; 

ImageIcon img, msl, al;
Image i, ms, ali;
Graphics2D g2d;

boolean allowUp = true, allowDown = true, allowAlienMove = false, firstDraw = true;
boolean[] allowAlien;
int mx, my = 0;
static int[] ay, ax, olday, oldax;

public Interface(){

    setFocusable(true);
    setBackground(Color.BLACK);
    setDoubleBuffered(true);

    addKeyListener(this);

    img = new ImageIcon(this.getClass().getResource("player.jpg"));
    al = new ImageIcon(this.getClass().getResource("alien.jpg"));

    p = new Player(this.getWidth()/6, this.getHeight()/6,10,this.getHeight()/2,img);
    timer = new Timer(1000, this);

    allowAlien = new boolean[5];
    a = new Alien[5];
    ay = new int[5];
    ax = new int[5];
    olday = new int[5];
    oldax = new int[5];

    for(int i = 0; i < 5; i++){

        ay[i] = -1;
        olday[i] = -1;

        ax[i] = -1;
        oldax[i] = -1;

    }

    for(int i = 0; i < allowAlien.length; i++){

        allowAlien[i] = false;

    }

    spawnAliens();          


}

//SPAWN ALIENS

public void spawnAliens(){       

    int[] area = new int[5];
    area[0] = 70;
    area[1] = 170;
    area[2] = 270;
    area[3] = 370;
    area[4] = 445;

    for(int j = 0; j < ax.length; j++){

        ax[j] = 700;
        oldax[j] = ax[j];

    }

    for(int i =0; i < a.length; i++){

        if(i > 0){
            ay[i] = ((int)(Math.random()*area[i] + area[i-1]));
        }else{
            ay[i] = ((int)(Math.random()*area[i] + 5));
        }

        olday[i] = ay[i];


        //THIS COMPONENT NOT MOVING
        a[i] = new Alien(this.getWidth()/6, this.getHeight()/6,ax[i],ay[i],img);
        allowAlien[i] = true;

       repaint();

    }

    allowAlienMove = true;



}

//THIS IS THE DRAWING PART 

public void paint(Graphics g){
    super.paint(g);

    i = img.getImage();  
    ms = msl.getImage();
    ali = al.getImage();

    g2d = (Graphics2D)g;
    g2d.drawImage(i, p.getX(), p.getY(), null);
    if(allowMissle){    
        g2d.drawImage(ms, m.getX(), m.getY(), null);
    }

    for(int i = 0; i < a.length; i++){
        if(allowAlien[i]){        
            g2d.drawImage(ali, ax[i], ay[i], null);


        }
    }


    if(firstDraw){
        timer.start();
    }

    firstDraw = false;
}

//THIS IS THE MOVEMENT PART

public void actionPerformed(ActionEvent e){


    while(ax[0] > 10){

        ax[0] += 10;
        repaint();
    }


}

//KEY LISTENER INTERFACE METHODS

public void keyReleased(KeyEvent e){



}
public void keyPressed(KeyEvent e){

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_W) {

        if(p.getY() <= 5){

            allowUp = false;

        }else{

            allowUp = true;

        }

        if(allowUp){        
            p.move(0, -5);
            my = p.getY();

            repaint();
        }
    }else if (key == KeyEvent.VK_S) {

        if(p.getY() >= 410 - 5){

            allowDown = false;

        }else{

            allowDown = true;

        }
        if(allowDown){
            p.move(0, 5);
            my = p.getY();
            repaint();
        }
    }

}
public void keyTyped(KeyEvent e){


}
}

提前致谢 :)

`

4

1 回答 1

1

因为你在那里没有停顿,这意味着一切都会立即发生,这将导致它崩溃。您还启动了一个计时器,但从未使用过它,您想通过此实现什么目标?

于 2013-04-13T15:58:12.440 回答