JavaFX重置图形上下文

我正在使用Kotlin和TornadoFX / JavaFX重绘每1/60秒的video游戏。 目前,我的程序通过将背景设置为白色并绘制而重绘。 不过,我宁愿一些更清洁的东西。 这是我目前正在绘制它的方式:

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

有没有什么办法简单地把它擦干净,而不是在彼此之上层层叠叠? 我试过在网上搜索,但是找不到任何东西。

结果是没有办法做到这一点。 但是,清空canvas并再次绘制并不会造成任何问题。 这些问题正在成为该计划的另一部分。

如果你的游戏背景一直是白色的,那么我会考虑在canvas后面插入一个具有白色背景色的窗格。 您仍然可以像现在一样绘制前景图像,然后使用clearRect清除帧之间的canvas。

 gc.clearRect(0.0, 0.0, 700.0, 700.0);