1

this is my first post. I looked through similar questions but couldn't find anything that helped. So I'm trying to programm a little RPG for practicing javafx and I managed to implement a fighting system that works. Now I want to write a resting method, that regenerates health over time. It wrote is so that every second it regenerates 10% of the max health which works, but it doesn't show the progress. The bar only fills once the method is finished, but I would like to see every tick.

package application;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class RootBorderPane extends BorderPane
{
    Rectangle healthCurrent;
    Label healthBorder;
    GridPane grid;
    Button heal;
    Button reset;
    private double health;

    
    public RootBorderPane()
    {
        innitComponents();
        addComponents();
        addHandlers();
    }
    
    
    
    private void addHandlers()
    {
        heal.setOnAction( event -> heal());
        reset.setOnAction( event -> reset());
    }
    
    
    private void innitComponents()
    {
        grid = new GridPane();
        
        healthCurrent = new Rectangle(100, 20, Color.RED);
        healthBorder = new Label("",  healthCurrent);
            healthBorder.setMinHeight(20);
            healthBorder.setMinWidth(100);
            healthBorder.setAlignment(Pos.CENTER_LEFT);
            healthBorder.setStyle("-fx-border-color: red;");
            healthBorder.setPadding(new Insets(2, 2, 2, 2));
            setHealth(1);
            healthCurrent.setWidth(getHealth());
        heal = new Button("heal");
        reset = new Button("reset");
            
    }
    
    private void addComponents()
    {
        grid.add(healthBorder, 0, 0, 2, 1);
        grid.add(heal, 0, 1, 1, 1);
        grid.add(reset, 1, 1, 1, 1);
        
        setCenter(grid);
    }
    
    private void heal()
    {
        if(health <= 100)
        {
            fillBar();
        }
    }
    
    private void fillBar()
    {
        if(getHealth() < 100)
        {
            setHealth(getHealth() +10);
            healthCurrent.setWidth(getHealth());
            try
            {
                Thread.sleep(1000);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        else
        {
            setHealth(100);
            healthCurrent.setWidth(getHealth());
            try
            {
                Thread.sleep(1000);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
    
    private void reset()
    {
        setHealth(1);
        healthCurrent.setWidth(getHealth());
        
    }
    
    public void setHealth(double health)
    {
        this.health = health;
    }
    
    public double getHealth()
    {
        return health;
    }
}

I wrote the heal() and fillBar() as two methods because i hoped that would make a difference, but it didn't.

Thank you in advance and I hope I'm not violating to many coding conventions.

Edeoo
  • 11
  • 1
  • 2
    you __must not__ sleep the fx application thread! What you want to learn is concurrency .. – kleopatra Jul 12 '20 at 10:14
  • 2
    The Animation API is probably a good fit for this: see, e.g., https://stackoverflow.com/questions/9966136/javafx-periodic-background-task – James_D Jul 12 '20 at 13:47

0 Answers0