随着时间的推移填补健康栏Javafx

时间:2020-07-12 10:03:56

标签: javafx time

这是我的第一篇文章。我浏览了类似的问题,但找不到任何有帮助的东西。 因此,我尝试编写一些RPG练习Javafx,并且设法实现了一个有效的战斗系统。现在,我想编写一个休息的方法,该方法可以随着时间的推移恢复健康。 它这样写是为了使它每秒恢复可正常运行的最大生命值的10%,但未显示进度。该栏仅在方法完成后才填充,但我希望看到每个刻度。

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;
    }
}

我将heal()和fillBar()编写为两个方法,是因为我希望这会有所作为,但事实并非如此。

预先感谢您,希望我不会违反许多编码约定。

0 个答案:

没有答案