0

Not sure why. My method runs multiple times and prints string "Fire !!!" It should only run, once after event occurred.

Player class

@Override
public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()){
    case KeyEvent.VK_SPACE:{
        Missilies m = new Missilies(x,y);
        //m.fire();
        if(!m.isFired()) {
            m.fire();
            m.setFired(true); 
        }

   public void keyReleased(KeyEvent e) {
    switch (e.getKeyCode()){

    case KeyEvent.VK_SPACE:{
        m.setFired(false);
    }

Missile class

public  void fire(){
    System.out.println("Fire !!!");

 }

now this seems to work:

 @Override
public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()){
    case KeyEvent.VK_SPACE:{
        Missilies m = new Missilies(x,y);
        if (lastKey == null || lastKey != e.getKeyChar()) {
            lastKey = e.getKeyChar();
            m.fire();
            System.out.println("keyPressed: " + lastKey);
        }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
RomZes
  • 23
  • 2
  • 7
  • possible duplicate of [Make a KeyEvent in Java only happen once even when key is held](http://stackoverflow.com/questions/23642854/make-a-keyevent-in-java-only-happen-once-even-when-key-is-held) – BitNinja Jul 04 '14 at 00:01
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Jul 04 '14 at 10:52
  • 1) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson Jul 04 '14 at 10:52
  • @AndrewThompson No specs, no teachers. Just me. Working on small video game and want it to be available online from the webpage. Plus mastering my JAVA skills. – RomZes Jul 04 '14 at 15:08
  • In that case, code a `JFrame` and launch it direct from a link using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). – Andrew Thompson Jul 04 '14 at 23:25

1 Answers1

0

Have a look at this:

keyPressed() will keep firing so long as the key is held down. You could put the code into keyReleased() but then it'd probably feel weird to the player if the missile was firing when they let go of the key. Better would be to put some kind of check in place to make sure it only fires once for each event.

boolean fired;

@Override
public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()) {
    case KeyEvent.VK_SPACE:{
        m = new Missilies(x,y);
        if(!fired) {
            m.fire();
            fired = true;
        }
        break;
}

@Override
public void keyRelased(KeyEvent e) {
    fired = false;
}
Fado
  • 31
  • 2
  • Did you set a boolean 'fired' field and create a setter for it? (Edited to show that. Sorry, I probably should have shown that in the first place.) – Fado Jul 03 '14 at 23:59
  • private boolean fired; public boolean isFired() { return fired; } public void setFired(boolean fired) { this.fired = fired; } – RomZes Jul 04 '14 at 00:00
  • My bad. I misread your original code. How about my new edit? – Fado Jul 04 '14 at 00:12
  • Still does it. Still fires like 100 times. Could be the timer issue? – RomZes Jul 04 '14 at 00:25