Skip to main content
se eliminaron 73 caracteres en el cuerpo; título editado
Origen Enlace

Make a Estoy haciendo un delay in a KeyEvent in Javaen un Key Event en java

I am making a recreation of Space Invaders inEstoy recreando el SpaceInvaders en Java and I want to create ay quiero hacer un delay between the missiles of my spaceshipentre misil y misil.

    @Override
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    // TODO Auto-generated method stub
    if (key == KeyEvent.VK_LEFT&&n.boom==0) {
        left=true;
    }

    if (key == KeyEvent.VK_RIGHT&&n.boom==0) {
        right=true;
    }
    if (key == KeyEvent.VK_SPACE&&n.boom==0) {
        space=true;
    }
}
@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    int key = e.getKeyCode();
    // TODO Auto-generated method stub
    if (key == KeyEvent.VK_LEFT) {
        left=false;
    }

    if (key == KeyEvent.VK_RIGHT) {
        right=false;
    }
    if (key == KeyEvent.VK_SPACE) {
        space=false;
    }

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

In my code,En mi codigo la variable space is a booleanes una variable that when it'sbooleana que cuando es true then it creates a new "missile" in another functioncrea un nuevo objeto misil en otra función. All the code works in aEl código funciona en un while(true) loop that makes everything move. The problem is that if I use some kind ofpor lo que si hago un thread.sleep it will stop all the game() se va a parar todo el juego.

Make a delay in a KeyEvent in Java

I am making a recreation of Space Invaders in Java and I want to create a delay between the missiles of my spaceship.

    @Override
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    // TODO Auto-generated method stub
    if (key == KeyEvent.VK_LEFT&&n.boom==0) {
        left=true;
    }

    if (key == KeyEvent.VK_RIGHT&&n.boom==0) {
        right=true;
    }
    if (key == KeyEvent.VK_SPACE&&n.boom==0) {
        space=true;
    }
}
@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    int key = e.getKeyCode();
    // TODO Auto-generated method stub
    if (key == KeyEvent.VK_LEFT) {
        left=false;
    }

    if (key == KeyEvent.VK_RIGHT) {
        right=false;
    }
    if (key == KeyEvent.VK_SPACE) {
        space=false;
    }

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

In my code, space is a boolean variable that when it's true then it creates a new "missile" in another function. All the code works in a while(true) loop that makes everything move. The problem is that if I use some kind of thread.sleep it will stop all the game.

Estoy haciendo un delay en un Key Event en java

Estoy recreando el SpaceInvaders en Java y quiero hacer un delay entre misil y misil.

    @Override
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    // TODO Auto-generated method stub
    if (key == KeyEvent.VK_LEFT&&n.boom==0) {
        left=true;
    }

    if (key == KeyEvent.VK_RIGHT&&n.boom==0) {
        right=true;
    }
    if (key == KeyEvent.VK_SPACE&&n.boom==0) {
        space=true;
    }
}
@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    int key = e.getKeyCode();
    // TODO Auto-generated method stub
    if (key == KeyEvent.VK_LEFT) {
        left=false;
    }

    if (key == KeyEvent.VK_RIGHT) {
        right=false;
    }
    if (key == KeyEvent.VK_SPACE) {
        space=false;
    }

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

En mi codigo la variable space es una variable booleana que cuando es true crea un nuevo objeto misil en otra función. El código funciona en un while(true) por lo que si hago un thread.sleep() se va a parar todo el juego.

Origen Enlace

Make a delay in a KeyEvent in Java

I am making a recreation of Space Invaders in Java and I want to create a delay between the missiles of my spaceship.

    @Override
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    // TODO Auto-generated method stub
    if (key == KeyEvent.VK_LEFT&&n.boom==0) {
        left=true;
    }

    if (key == KeyEvent.VK_RIGHT&&n.boom==0) {
        right=true;
    }
    if (key == KeyEvent.VK_SPACE&&n.boom==0) {
        space=true;
    }
}
@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    int key = e.getKeyCode();
    // TODO Auto-generated method stub
    if (key == KeyEvent.VK_LEFT) {
        left=false;
    }

    if (key == KeyEvent.VK_RIGHT) {
        right=false;
    }
    if (key == KeyEvent.VK_SPACE) {
        space=false;
    }

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

In my code, space is a boolean variable that when it's true then it creates a new "missile" in another function. All the code works in a while(true) loop that makes everything move. The problem is that if I use some kind of thread.sleep it will stop all the game.