无法使用图形上下文绘制同一图像的多个实例

Unable to draw multiple instace of same Image with GraphicsContext

大家好,这是我的第一个问题,欢迎大家提出建设性的批评意见。 我正在使用 ScalaFX 制作 space 射击游戏,我遇到了一个问题,当我在 AnimationTimer 循环中绘制子弹 laser 时,每当玩家发射另一颗子弹时,程序只渲染最新的子弹,旧的子弹就消失了。

为了简洁起见,我将post主AnimationTimer循环仅与项目符号输入和渲染相关:

      //List of stuffs (Bullets, enemies etc)
      var laserListB : ListBuffer[Laser] = ListBuffer()

      val timer = AnimationTimer( currentNanoTime => {
      //Calculating time since last frame for frame independant rendering
      var elapsedTime : Double = (currentNanoTime - lastNanoTime) / 1000000000.0;
      lastNanoTime = currentNanoTime;

      //Input check
      //SKIPPED PLAYER MOVEMENT CODE
      if(shootPress){
        var now = System.nanoTime()
        //Checking for atkSpeed cooldwon
        if((lastShootNano <= 0L) || ((now - lastShootNano) >= player.atkSpeed)){
          laserListB += player.shoot()
        }
        lastShootNano = now
      }
      
      //Updating position
      for(laser <- laserListB){
        laser.sprite.velocityX = 0
        laser.sprite.velocityY = -400
        laser.sprite.update(elapsedTime)
      }

      //Rendering
      //Bullets
      for(laser <- laserListB){
        laser.sprite.render(gc)
      }
      
    })

下面是我的 Sprite class:

import scalafx.scene.image.Image
import scalafx.scene.canvas.GraphicsContext
import scalafx.geometry.Rectangle2D

class Sprite(
    private var _image : Image,
    private var _positionX : Double,
    private var _positionY : Double,
    private var _velocityX : Double,
    private var _velocityY : Double,
    private var _width : Double,
    private var _height : Double){

    //Functions
    def render(gc : GraphicsContext){
        gc.drawImage(_image, _positionX, _positionY);
    }
}

如果你需要,这是我播放器的射击功能:

    def shoot() = {
        //Setting up laser sprite
        val laser = new Laser(_atkSprite, damage, true)
        laser.sprite.velocityX = 0 
        laser.sprite.velocityY = -400 
        laser.sprite.positionX = _sprite.positionX + (_sprite.width / 2)       //Center laser horizontally on player sprite
        laser.sprite.positionY = _sprite.positionY - 10                   //Slight offset to be a bit higher than the player sprite
        laser
    }

感谢您阅读到这里 :D

编辑 1: 这是演示问题的视频:https://youtu.be/DliNyBoa1DI

好的,我解决了,这不是 GraphicsContext 的问题,而是由于 Laser 在 player.shoot() 中使用相同的 Sprite 实例化,因此所有 Laser 将具有相同的 X 和 Y,并且重叠,因此看起来好像它“消失”了。

新代码每次实例化Laser时都会实例化一个新的Sprite如下:

    def shoot() = {
        //Laser sprites
        val atkImg = new Image(getClass.getResourceAsStream("/Images/small_laser_blue.png"))
        val atkSprite = new Sprite(atkImg, 0, 0, 0, 0, atkImg.getWidth(), atkImg.getHeight())
        val laser_ = new Laser(atkSprite, _damage, true)
        laser_.sprite.velocityX = 0 
        laser_.sprite.velocityY = -400 
        laser_.sprite.positionX = _sprite.positionX + (_sprite.width / 2)  //Center laser horizontally on player sprite
        laser_.sprite.positionY = _sprite.positionY - 10                   //Slight offset to be a bit higher than the player sprite
    }