【问题标题】:Java game - Submarine killer - Launch the bomb anytime I press my down keyJava 游戏 - 潜艇杀手 - 每当我按下向下键时就发射炸弹
【发布时间】:2013-11-30 17:36:08
【问题描述】:

所以我有这个代码,我正在尝试学习 Java,这基本上是我的第一个游戏,它类似于 SubmarineKiller,你是一艘船,向潜艇发射炸弹。 我下面的课是炸弹。当我按下向下箭头时,炸弹会发射,但我无法发射另一颗,直到它击中潜艇或离开屏幕。我的问题是:如何在第一颗炸弹离开船后立即发射下一颗炸弹?基本上只要我按下向下键就会发射炸弹。

public void keyPressed(KeyEvent evt) {
       int code = evt.getKeyCode();
       if (code == KeyEvent.VK_DOWN) {
        if (bomb.isFalling == false)
            bomb.isFalling = true;
       }
}

-

    private class Bomb {
        int centerX, centerY;
        boolean isFalling;

        Bomb() {
            isFalling = false;
        }

        void updateForNewFrame() {
            if (isFalling) {
                if (centerY > height) {
                    isFalling = false;
                }
                else 
                    if (Math.abs(centerX - sub.centerX) <= 36 && Math.abs(centerY - sub.centerY) <= 21) {
                        sub.isExploding = true;
                        sub.explosionFrameNumber = 1;
                        isFalling = false; // Bomba reapare in barca
                    }
                    else {
                        centerY += 10;
                    }
            }   
        }

        void draw(Graphics g) {
            if ( !isFalling ) {
                centerX = boat.centerX;
                centerY = boat.centerY + 23;
            }
             g.setColor(Color.RED);
             g.fillOval(centerX - 8, centerY - 8, 16, 16); 
        }
}

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java applet awt key-events


【解决方案1】:

您需要根据炸弹列表而不是单个炸弹来重写代码。然后,您的按键事件需要更改以将新炸弹添加到列表中,而不仅仅是设置单个炸弹的属性。然后您的处理代码也需要更改 - 您需要遍历列表并依次处理每个炸弹。

【讨论】:

  • 谢谢你的回答,我还是没弄明白,但我会继续努力的。
猜你喜欢
  • 1970-01-01
  • 2017-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 2015-02-10
相关资源
最近更新 更多