0

I'm working on a video game that redraws every 1/60 of a second using Kotlin and TornadoFX/JavaFX. Currently, my program redraws by setting the background to white and drawing over it. However, I would prefer something that is a bit cleaner. This is how I'm drawing it currently:

private fun drawShapes(gc: GraphicsContext) {
    gc.fill = c(255, 255, 255)
    gc.fillRect(0.0, 0.0, 700.0, 700.0) //Game is 700x700
    gc.fill = c(94, 132, 233)
    walls.forEach { gc.fillRect(it.x.toDouble(), it.y.toDouble(), it.w.toDouble(), it.h.toDouble()) }
    gc.fill = c(255, 239, 20)
    coins.filter { !it.collected }.forEach { gc.fillRect(it.x.toDouble(), it.y.toDouble(), it.w.toDouble(), it.h.toDouble()) }
    //Cut off here because following is more of same
}
fixedRateTimer(period = 16, initialDelay = 500) { drawShapes(graphicsContext2D) }

Is there any way to simply wipe it clean instead of layering a lot on top of each other? I've tried searching the internet for it, but I couldn't find anything.

fin444
  • 725
  • 2
  • 14
  • 27
  • 1
    You should [profile](http://stackoverflow.com/q/2064427/230513) to see what's "causing lag issues." – trashgod Dec 08 '17 at 01:52
  • @trashgod I'm not really sure how to use the programs you linked. Also, it's mainly just preferable to me. I'll remove the misleading portion. – fin444 Dec 08 '17 at 02:53
  • Two approaches are contrasted [here](https://stackoverflow.com/a/44141878/230513). – trashgod Dec 08 '17 at 12:30

2 Answers2

0

Turns out there isn't really a way to do this. However, blanking out the canvas and drawing it again didn't cause any issues. The issues were being caused another part of the program.

fin444
  • 725
  • 2
  • 14
  • 27
0

If the background of your game is consistently white then I would consider inserting a pane behind the canvas that has a white background color. You can still draw your foreground images just as you currently are and then to clear the canvas between frames use clearRect.

gc.clearRect(0.0, 0.0, 700.0, 700.0);
Shadskii
  • 321
  • 2
  • 4