0

I'm making Tower Defence Game using JavaFX, my current problem is updating view after animation calculations. I've tried making new Thread and starting it, but whenever i touch GraphicContext from Canvas, game crashed. My current game loop class looks like:

import model.Enemy;
import model.Map;
import model.Player;
import model.Model;
import view.View;

public class GameLoop2 {

    private Canvas canvas;
    public Integer enemiesNr;
    public Integer enemiesSpawnTime;
    public Integer spawnedEnemies;
    private long lastSpawn;
    private ArrayList<Enemy> enemies;
    private boolean firstPlay;
    private Model model;
    private Map map;

    public GameLoop2(Canvas mainCanvas, Model m) {
        model = m;
        map = model.getMap();
        canvas = mainCanvas;
        enemiesNr = map.getEnemiesNr();
        enemiesSpawnTime = model.getMap().getEnemiesSpawnTime();
        spawnedEnemies = 0;
        lastSpawn = System.currentTimeMillis();
        enemies = new ArrayList<>(enemiesNr);
        for(int i=0; i < enemiesNr;i++)
            enemies.add(i, new Enemy(map.getStartXPosition(), map.getStartXPosition()));
    }

    private void spawnEnemy() {
        if(spawnedEnemies >= enemiesNr)
            return;
        enemies.get(spawnedEnemies).setAlive(true);
        View.drawEnemy(canvas, enemies.get(spawnedEnemies));
        spawnedEnemies++;
        lastSpawn = System.currentTimeMillis();
    }
    private void enemyPhysics() {
        for(Enemy e: enemies)
            if(e.isAlive())
                e.physics(1);
    }
    private void drawEnemies(){
        for(Enemy e: enemies)
            if(e.isAlive())
                View.drawEnemy(canvas,e);
    }
    private void update() {
        canvas.getGraphicsContext2D().restore();
        View.drawMap(map, canvas);
        drawEnemies();
    }

    public void start() {
      while(true){
        // Calculations
        long now = System.currentTimeMillis();
        if (now - lastSpawn > enemiesSpawnTime) {
            spawnEnemy();
        }
        enemyPhysics();
        // Updating View
        update();
        // View is refreshed after break; statement
        if(now - lastSpawn > 6000)
            break;
    }
}

I've also tried Service class, but it didn't worked for me. I would also want to make claculation as fast as possible to make movement animation effect for aproaching enemies. It will be nice to make it in a way that will allow to add another thread, for example background music, or calculating damage to main tower.

Mateusz Dorobek
  • 702
  • 1
  • 6
  • 22
  • 3
    What's the exception? – Stefan Warminski May 29 '17 at 14:17
  • Also consider a `Worker` or `Worker`, suggested [here](https://stackoverflow.com/a/44141878/230513). – trashgod May 29 '17 at 14:28
  • Do the updates to the ui on the application thread. Everything else could lead to exceptions when the JavaFX application thread does the layout since it relies on the fact that it's the only thread updating the ui... – fabian May 29 '17 at 16:09
  • fabian - I know, but I'm not creating any Thread. And even despite of it after calculations, when I try to update the view compilator says, that the view is updated in wrong thread. What can I do wit content of start() function to make update() function work in main Thread? – Mateusz Dorobek May 29 '17 at 17:07
  • Whenever you want to touch the graphical thread in JavaFx you need to use `Platform.runLater()` - https://stackoverflow.com/questions/13784333/platform-runlater-and-task-in-javafx – Chris May 29 '17 at 17:36
  • I solved this problem using AnimationTimer class. Without Thread, Platform.runLater. Very usefull if you want to animate something small in javaFX. – Mateusz Dorobek Jun 10 '17 at 15:25

0 Answers0