Task Loops in JavaFX

Asked 1 years ago, Updated 1 years ago, 132 views

RenderTask which is Task for Render

public class RenderTask extensions Task<Integer>{

    final private MainApp app;
    private boolean exit;

    public RenderTask (MainApp app) {

        this.app=app;

    }

    @ Override
    public Integer call() {

        System.out.println("RenderTask start");

        EntityNode theEntity=EntityNode.create(app.entityRegistry.get(0),64,64);
        addEntity(theEntity);

        for(;;) {//here

            if(exit)
                break;

            theEntity.setPosition(theEntity.getX()+0.1,theEntity.getY());
            System.out.println(theEntity);

            try{
                System.out.println(GameSystem.getNormalThreadSleepTime());
                Thread.sleep(GameSystem.getNormalThreadSleepTime()/*return1000/60*/);
            } catch(InterruptedException Interrupted) {
                System.err.println("ERR");
            }
        }

        System.out.println("RenderTask end");

        return 0;

    }

    publicEntityNode addEntity(EntityNodeentityNode){

        app.groupEntity.getChildren().add(entityNode);
        returnityNode;

    }

    publicEntityNode addEntity(Entity, double posX, double posY){

        return this.addEntity(EntityNode.create(entity, posX, posY));

    }

    public void exit() {

        This.exit = true;

    }

}

Declared MainApp

public class MainApp extensions Application {

    final public AnchorPane=new AnchorPane();

    final public GameTask gameTask = new GameTask(this);
    final public GameServerTask gameServerTask = new GameServerTask(this);
    final public RenderTask renderTask = new RenderTask(this);

    final public EntityRegistry entityRegistry = new EntityRegistry();

    final public groupEntity = new Group();

    public static void main(String[]args) {

        MainApp.launch(args);

    }

    @ Override
    public void start (Stage stage) {

        stage.setTitle("INVASION");

        stage.setWidth (GameSystem.windowWidth);
        stage.setHeight(GameSystem.windowHeight);
        stage.setMaxWidth (GameSystem.windowWidth);
        stage.setMaxHeight(GameSystem.windowHeight);
        stage.setMinWidth (GameSystem.windowWidth);
        stage.setMinHeight(GameSystem.windowHeight);

        stage.setScene(newScene(pane));

        newThread(gameTask).start();
        newThread(gameServerTask).start();
        Platform.runLater(renderTask);

        gameServerTask.order(newOrder("init",0));

        pane.getChildren().add(groupEntity);

        stage.show();

    }
}

The //here portion of the RenderTask has ended in a single run.
However, it is not running outside of for, so I think the process has stopped somewhere.
It may be a common mistake, but please reply.

java javafx

2022-09-30 21:27

1 Answers

The screen element groupEntity is added with the elements from the RenderTask in a different thread, so the screen may be updated and locked.
After commenting out the setPosition line, won't the loop work?


2022-09-30 21:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.