1

Some days ago i asked this: How to have multiple instances on the screen of the same sprite at the same time with javafx2 and partially solved the question elaborating the suggestion of jewelsea.

I have this obstacle now: when a key is pressed to 'fire' bullets, weapon shoot bullets as fast as a machine gun.. I would like to limit the amount of bullets that the weapon of the hero of my game can shoot..for example to decide to shoot a bullet every 0.5 secs or just when a key is pressed and not to have always a machine gun effect... In my game the part of program that controls the 'fire' effect is like this:

        scene.setOnKeyTyped(new EventHandler<KeyEvent>() {  
            @Override  
            public void handle(KeyEvent event2) {  

            if (event2.getCode()==KeyCode.F); { .........

Before i've tried also using setOnKeyPressed and setOnKeyReleased with the same results.. So what could i try to shoot just a bullet also keeping press the 'F' key or to limit the bullets in number? Thank you in advance and good bye!

Community
  • 1
  • 1
Roberto C.
  • 21
  • 3
  • Ok, I think alternatives are: limit bullets in number (they will shoot faster if you shoot closer to something that eliminate them, like walls), only detect key down from the event, mute the code for a time (using timer) after a bullet is shot... But I'm just guessing since I don't know javafx. – eri0o Oct 04 '14 at 04:44

1 Answers1

0

I've done this by using a Timeline as a timer and starting it and stopping it on key pressed and key released:

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class KeyEventTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Pane root = new Pane();
        Scene scene = new Scene(root, 400, 400);

        Duration firingInterval = Duration.millis(500);
        Timeline firing = new Timeline(
                new KeyFrame(Duration.ZERO, event -> fire()),
                new KeyFrame(firingInterval));
        firing.setCycleCount(Animation.INDEFINITE);

        scene.setOnKeyPressed(event -> {
            if (event.getCode() == KeyCode.F && firing.getStatus() != Animation.Status.RUNNING) {
                firing.playFromStart();
            }
        });

        scene.setOnKeyReleased(event -> {
            if (event.getCode() == KeyCode.F) {
                firing.stop();
            }
        });

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void fire() {
        // dummy implementation:
        System.out.println("Fire!");
    }

    public static void main(String[] args) {
        launch(args);
    }
}

It's fairly easy to adapt this to additionally limit the number of bullets on the screen at any time, etc.

James_D
  • 201,275
  • 16
  • 291
  • 322