按空格键后如何生成新对象?

时间:2015-02-02 17:40:36

标签: java swing object

我创建了一个“子弹”,在按下空格键后向正x方向移动。一旦它离开屏幕,它就会在某个位置重置。但是,我怎样才能使每隔时间按空格键,子弹被击中?是否需要创建一个新对象,或者我可以保留相同的对象但是第一次实例化,而第一次仍然是动画(我觉得这样做不起作用)。我是新手,所以感谢任何帮助。

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

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

    }
 }
}

然后是球类:

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

1 个答案:

答案 0 :(得分:1)

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

一种解决方案是使用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());