Zeyad Elsayed
Zeyad Elsayed

Reputation: 1

How to draw in graphics context in game loop?

I'm making fruit ninja game and I want to pause the animation timer for a while and set the image to the sliced fruit image "current.getBufferedImages()[1]" when sliced but I get the same image of the non-sliced fruit "current.getBufferedImages()[0]" even though I change the image to be drawn in the graphicsContext.setImage()

        public void startGame(Stage primaryStage) throws Exception {
        ga = GameActions.get();
        canvas = new Canvas(1024,683);

        gc = canvas.getGraphicsContext2D();

        timeline = new Timeline(new KeyFrame(Duration.millis(falling), event -> {
            time += falling / 3000;
            IGameObject c = ga.createGameObject();
            drop.add(c);
        }));
        timeline.setCycleCount(4000);
        timeline.play();

        timer = new AnimationTimer() {

            @Override
            public void handle(long arg0) {
                gameUpdate(primaryStage);
            }

        };
        timer.start();
        scene = new Scene(root, 1024, 683);


        root.getChildren().addAll(ivBackground,lblMissed,canvas,back,reset);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

         public void gameUpdate(Stage stage){
        Image k;
        gc.drawImage(background, 300, 300);
        gc.clearRect(0, 0, 1024, 683);
        Iterator<IGameObject> itr = drop.iterator();
        while(itr.hasNext()) {
            IGameObject current = itr.next();
            EventHandler<MouseEvent> e = new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                    if(current.insideBounds(event.getX(), event.getY()))
                        current.slice();
                }

            };
            gc.getCanvas().setOnMousePressed(e);
            if(!current.isSliced()) {
                k = SwingFXUtils.toFXImage(current.getBufferedImages()[0], null);
            }else {
                k = SwingFXUtils.toFXImage(current.getBufferedImages()[1], null);
                if(current.getObjectType().toString().equalsIgnoreCase("dangerousbomb")) {
                    for(int i=0;i<150000;i++) {
                        gc.drawImage(k, current.getXlocation(), current.getYlocation());
                        timer.stop();
                    }
                    timer.start();
                    itr.remove();
                    lives--;
                    lblLives.setText("Lives: " + String.valueOf(lives));
                }else if(current.getObjectType().toString().equalsIgnoreCase("fatalbomb")) {
                    gameOver();
                    break;
                }else if(current.getObjectType().toString().equalsIgnoreCase("specialfruit")) {
                    for(int i=0;i<150000;i++) {
                        gc.drawImage(k, current.getXlocation(), current.getYlocation());
                        timer.stop();
                    }
                    timer.start();
                    itr.remove();
                    score+=5;
                    lblScore.setText("Score: " + String.valueOf(score));
                    if(score >= highestScore) {
                        highestScore = score;
                        ga.saveHighScore(highestScore);
                        lblHighestScore.setText("Highest Score: "+highestScore);
                    }
                }else if(current.getObjectType().toString().equalsIgnoreCase("fruit")) {
                    for(int i=0;i<150000;i++) {
                        gc.drawImage(k, current.getXlocation(), current.getYlocation());
                        timer.stop();
                    }
                    timer.start();
                    itr.remove();
                    score++;
                    lblScore.setText("Score: " + String.valueOf(score));
                    if(score >= highestScore) {
                        highestScore = score;
                        ga.saveHighScore(highestScore);
                        lblHighestScore.setText("Highest Score: "+highestScore);
                    }
                }
            }

            gc.drawImage(k, current.getXlocation(), current.getYlocation());
            ga.updateObjectsLocations(time,current);


            }
        }
    }

Upvotes: 0

Views: 286

Answers (1)

fabian
fabian

Reputation: 82491

gameUpdate is executed on the JavaFX application thread. This means any delay of this method returning simply freezes the GUI until the method returns. For this reason those

for(int i=0;i<150000;i++) {
    gc.drawImage(k, current.getXlocation(), current.getYlocation());
    timer.stop();
}

loops do not work.

The easiest way of "pausing" here would be to save the time the AnimationTimer is allowed to continue.

timer = new AnimationTimer() {

    @Override
    public void handle(long arg0) {
        gameUpdate(arg0);
    }

};
private long pauseEnd = 0L;

/**
 * Continue updates after a certain number of milliseconds after the current time
 */
private void pauseFor(long currentTime, long durationMillis) {
    pauseEnd = currentTime + 1_000_000L * durationMillis;
}

public void gameUpdate(long timestamp) {
    if (timestamp >= pauseEnd) { // only redraw, if not paused
        ...

        //for(int i=0;i<150000;i++) {
        //    gc.drawImage(k, current.getXlocation(), current.getYlocation());
        //    timer.stop();
        //}
        //timer.start();
        //itr.remove();
        //score++;
        gc.drawImage(k, current.getXlocation(), current.getYlocation());
        itr.remove();
        score++;
        pauseFor(timestamp, 3000); // start updates again in 3000 ms

        ...
    }
}

Furthermore you replace the EventHandler for every game object in every iteration of the game loop. This way only a single game object is checked for an intersection with the mouse. You need to check all of them with a single event handler. In addition to removing the event handler registration from the gameLoop method, add the following code to the start method to check all objects for intersections:

canvas.setOnMousePressed(event -> {
    for (IGameObject current : drop) {
        if(current.insideBounds(event.getX(), event.getY())) {
            current.slice();
        }
    }
});

Furthermore I strongly recommend not converting the BufferedImages in every single iteration of the game loop over and over again. Preferrably replace the BufferedImages with JavaFX Images directly or keep a Map<BufferedImage, Image> containing every conversion result...

Upvotes: 2

Related Questions