简体   繁体   English

对象不会出现在Java swing的按键上

[英]Object won't appear on keypress in java swing

So I am trying to make a very simple game in Java using swing. 因此,我试图使用swing创建一个非常简单的Java游戏。 It's a space invaders type game and I have successfully coded in the spaceship that moves left and right with key presses. 这是一款太空侵略者类型的游戏,我已经成功地在按键中左右移动的飞船中进行了编码。 However I am having a lot of trouble getting the "shot" to show up. 但是,要显示“镜头”非常麻烦。

I am not looking to position it just yet, I really just want to create an object on keypress to make it work. 我暂时不打算定位它,我真的只是想在按键上创建一个对象来使其工作。 I am assuming since it is not initially in the "paint" method, that is why it is not appearing. 我假设因为它最初不在“ paint”方法中,所以它没有出现。 But I don't know how to make it show up! 但是我不知道如何显示它! Any ideas? 有任何想法吗?

For reference here is current code: 供参考的是当前代码:

public void keyPressed(KeyEvent e) {
    if (KeyEvent.VK_RIGHT == e.getKeyCode()) {
        moveRight();
    }
    if (KeyEvent.VK_LEFT == e.getKeyCode()) {

        moveLeft();
    }
    if (KeyEvent.VK_SPACE == e.getKeyCode()) {
        shoot();

    }
}

public void shoot() {
    Laser laser = new Laser(new Point(200, 200));
    this.repaint();
}

public void moveLeft() {
    if (player.getCentre().getX() <= 20) {
        player.setX(20);
    } else {
        double movement = player.getCentre().getX();
        movement -= 10;
        player.setX(movement);
    }
    this.repaint();
}

@Override
public void paint(Graphics g) {
    setBackground(Color.black);
    super.paint(g);
    player.draw(g);

}

I just want to make shoot() successfully create an object and have it show up in my panel! 我只想使shoot()成功创建一个对象,并将其显示在面板中!

Problems/Suggestions: 问题/建议:

  • Your shoot method creates an object just fine, but then completely ignores it. 您的Shoot方法会很好地创建一个对象,但是会完全忽略它。 You don't add your newly created Laser object to anything, not to a field of the class, not to any ArrayList, and not to any other collection, so the class will have no knowledge that the object exists, and once the method where it was created ends, the object is primed for garbage collection since it only exists locally within the method. 您无需将新创建的Laser对象添加到任何对象,类的字段,任何ArrayList或任何其他集合中,因此该类将不知道该对象存在,并且一旦该方法在它创建完后,由于该对象仅在方法内部本地存在,因此已准备好进行垃圾回收。
  • We have no idea if your KeyListener is working or not. 我们不知道您的KeyListener是否正常工作。 Have you debugged this part with println statements? 您是否已使用println语句调试了这一部分? If not I'd do this. 如果没有,我会这样做。
  • I'd avoid KeyListeners in general in favor of Key Bindings as the latter is much less fidgety regarding focus requirements 我通常会避免使用KeyListeners来支持Key Bindings,因为后者在关注焦点方面不那么烦躁
  • As a general rule, don't override JPanel's paint method but rather its paintComponent method, and be sure to call the super's method within your override. 通常,不要覆盖JPanel的paint方法,而要覆盖其paintComponent方法,并确保在您的覆盖范围内调用super的方法。
  • Your GUI will only paint what you tell it to paint, and currently your painting method is only asking to have the Player's state painted but nothing else. 您的GUI仅会绘制您告诉其进行绘制的内容,当前您的绘制方法仅要求绘制玩家的状态,而没有其他内容。 If you want a laser blast painted, then the painting method, which again should be a protected void paintComponent(Graphics g) override and not a public void paint(Graphics g) override, should contain code to do this. 如果要进行激光喷漆,则该绘制方法(应再次使用protected void paintComponent(Graphics g)替代而不是public void paint(Graphics g)替代)应包含执行此操作的代码。

We're a bit limited in any advice that we can give since you've posted a code snippet, one that we can't compile or run. 自从您发布了一个代码片段以来,我们无法提供任何建议,但是我们无法编译或运行它。 If these suggestions don't lead you to an answer, then please create and post a small but simple complete program that illustrates your problem, an MCVE . 如果这些建议没有为您带来答案,请创建并发布一个小而简单的完整程序来说明您的问题,即MCVE Please check the link for the details of this very useful tool. 请检查链接以获取此非常有用的工具的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM