按下 space 栏后如何生成新对象?

How do I generate a new object after pressing the space bar?

我创建了一个 "bullet",它在按下 space 栏后沿正 x 方向移动。一旦离开屏幕,它会在某个位置重置。但是,我怎样才能做到 每次 按下 space 栏时,都会发射一颗子弹?是否需要创建一个新对象,或者我可以保留同一个对象但在第一个对象仍在动画时第二次实例化(我觉得那行不通)。我是新手,所以感谢任何帮助。

编辑:到目前为止我的代码。第一个用于 PlayState,第二个用于球 class

public class GameMain {
private static final String GAME_TITLE = "LoneBall (Chapter 5)";
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 450;
public static Game sGame;

public static void main(String[] args) {
    JFrame frame = new JFrame(GAME_TITLE);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false); // Prevents manual resizing of window
    sGame = new Game(GAME_WIDTH, GAME_HEIGHT);
    frame.add(sGame);
    frame.pack();
    frame.setVisible(true);
    frame.setIconImage(Resources.iconimage); // This is the new line!
}

}

public class PlayState extends State {

private static final int PADDLE_WIDTH = 15;
private static final int PADDLE_HEIGHT = 60;

private Ball ball;
private static final int BALL_DIAMETER = 10;

private int playerScore = 0;
private Font scoreFont;

@Override
public void init() {

    scoreFont = new Font("SansSerif", Font.BOLD, 25);
    ball = new Ball(300, 200, BALL_DIAMETER, BALL_DIAMETER);
}

@Override
public void update() {

    ball.update();
    if (ball.isDead()) {
        playerScore++;
        ball.reset();
    }

    //adjust player score here
}

@Override
public void render(Graphics g) {
    // Draw Background

    g.setColor(Resources.darkRed);
    g.fillRect(0, 0, GameMain.GAME_WIDTH,
            GameMain.GAME_HEIGHT);
    // Draw Separator Line

    // Draw Paddles
    g.setColor(Color.white);
    g.drawImage(Resources.gun, 0, 150, 300, 200, null);


    // Draw Ball
    g.setColor(Color.yellow);
    g.fillOval(ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight());


    // Draw UI
    g.setFont(scoreFont); // Sets scoreFont as current font
    g.drawString("" + playerScore, 350, 40); // Draws String using current
                                                // font
}

@Override
public void onClick(MouseEvent e) {
    // TODO Auto-generated method stub
}

@Override
public void onKeyPress(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_SPACE) {
        ball.velX = 10;
        }
}

@Override
public void onKeyRelease(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_UP
            || e.getKeyCode() == KeyEvent.VK_DOWN) {

    }
 }
}

然后球class:

public class Ball {

private int x, y, width, height;
public int velX;
private Rectangle oval;

public Ball(int x, int y, int width, int height) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    final int velX = 0;
    oval = new Rectangle(x, y, width, height);
}

public void update() {
    x += velX;
}

private void updateRect() {
    oval.setBounds(x, y, width, height);
}


public boolean isDead() {
    return (x < 0 || x + width > GameMain.GAME_WIDTH || y < 0 || y > GameMain.GAME_HEIGHT);
}

public void reset() {
    x = 300;
    y = 200;
    velX = 0;

}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public int getWidth() {
    return width;
}

public int getHeight() {
    return height;
}

public Rectangle getRect() {
    return oval;
}
}

你没有提到你的 GUI 库,所以我假设它是一个 Swing 应用程序,但希望你能通过给我们 lot 更多信息来解决这个问题,并且您当前的相关代码。

一种解决方案是使用 Key Bindings 将子弹创建操作绑定到空格键。这解决了 KeyListeners 的问题并关注困扰其使用的问题。

例如,

// assuming displayed a main JPanel called mainJPanel
ActionMap actionMap = mainJPanel.getActionMap();
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = mainJPanel.getInputMap(condition);

// assuming a class, CreateBulletAction, that extends AbstractAction
// and that creates your bullets
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "space");
actionMap.put("space", new CreateBulletAction());