在 render() 中随机更改图像而不更改所有其他图像

Change randomly image in render() without changing it for all other images

我跟随 this tutorialLibGDX 中构建了一个简单的游戏。游戏的基本思路是:有水滴落下,玩家应该通过将水桶拖到水滴落下的地方来放置它们。

在原始版本中,所有水滴都是从一个水滴图像创建的,我尝试做的是用不同的水滴(水滴的不同图像)下一场雨,所以我创建了一个 List - someDrops 不同的 Texturerender() 我更换了:

     batch.draw(dropImage, raindrop.x, raindrop.y); 

与:

       int random = MathUtils.random(0, 3;
       batch.draw(someDrops.get(random), raindrop.x, raindrop.y); 

我得到的是不同水滴的雨,但它们并没有以不同的图像落下,而是落下并同时改变图像。

我该如何设置,以便每次掉落时都带有来自 someDrops 的随机图像,因为它以随机 x,y 位置掉落。 另外我想给每个滴添加不同的点,我应该把它保存在哪里以及选择什么类型的collection?

public class Drop implements ApplicationListener {
   private Texture dropImage;
   private Texture bucketImage;
   private Sound dropSound;
   private Music rainMusic;
   private SpriteBatch batch;
   private OrthographicCamera camera;
   private Rectangle bucket;
   private Array<Rectangle> raindrops;
   private long lastDropTime;
   private List<Texture> someDrops;

   private Texture drop0;   
   private Texture drop1;
   private Texture drop2;
   private Texture drop3;

   @Override
   public void create() {
      // load the images for the droplet and the bucket, 64x64 pixels each
      dropImage = new Texture(Gdx.files.internal("droplet.png"));

      drop0 = new Texture(Gdx.files.internal("droplet0.png"));
      drop1 = new Texture(Gdx.files.internal("droplet1.png"));
      drop2 = new Texture(Gdx.files.internal("droplet2.png"));
      drop3 = new Texture(Gdx.files.internal("droplet3.png"));
      bucketImage = new Texture(Gdx.files.internal("bucket.png"));

      someDrops = new ArrayList<Texture>();

            someDrops.add(new Texture(drop0));
            someDrops.add(new Texture(drop1));
            someDrops.add(new Texture(drop2));
            someDrops.add(new Texture(drop3));

      camera = new OrthographicCamera();
      camera.setToOrtho(false, 800, 480);
      batch = new SpriteBatch();

      // create a Rectangle to logically represent the bucket
      bucket = new Rectangle();
      bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
      bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
      bucket.width = 64;
      bucket.height = 64;

      //raindrops array and spawn the first raindrop
      raindrops = new Array<Rectangle>();
      spawnRaindrop();

   private void spawnRaindrop() {
      Rectangle raindrop = new Rectangle();
      raindrop.x = MathUtils.random(0, 800-64);
      raindrop.y = 480;
      raindrop.width = 64;
      raindrop.height = 64;
      raindrops.add(raindrop);
      lastDropTime = TimeUtils.nanoTime();
   }

   @Override
   public void render() {
      Gdx.gl.glClearColor(0, 0, 0.2f, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
      camera.update();
      batch.setProjectionMatrix(camera.combined);

      // begin a new batch and draw the bucket and
      // all drops
      batch.begin();
      batch.draw(bucketImage, bucket.x, bucket.y);
      for(Rectangle raindrop: raindrops) {
         //batch.draw(dropImage, raindrop.x, raindrop.y); 
           int random = MathUtils.random(0, 4);
           batch.draw(someDrops.get(random), raindrop.x, raindrop.y); 

      }
      batch.end();

      // check if we need to create a new raindrop
      if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();

      Iterator<Rectangle> iter = raindrops.iterator();
      while(iter.hasNext()) {
         Rectangle raindrop = iter.next();
         raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
         if(raindrop.y + 64 < 0) iter.remove();
         if(raindrop.overlaps(bucket)) {
            dropSound.play();
            iter.remove();
         }
      }
   }

在你的 render() 函数中,我相信你调用每一帧,你绘制所有现有的水滴,必要时创建一个新的水滴,并减少水滴的 y 位置。 出现问题是因为每次你想绘制现有的水滴时,你使用

int random = MathUtils.random(0, 4);

这可能会给你一个不同于之前绘制的图像,即当它处于更高的 y 位置时。因此,当它掉落(递减 y)时,每次渲染发生时图像都会不断变化,直到它落入桶中时最终被移除。

要解决此问题,请仅在第一次渲染新水滴时获取随机图像,稍后保存此图像并在水滴的整个生命周期内使用相同的图像绘制它。这个用于保存相应图像的新数组的大小将与雨滴数组保持相同,除非添加了新的雨滴,在这种情况下你会得到一个新的随机图像。

我将添加一些代码来说明这一点,但我从未在 Java 中编程或使用过 libgdx,因此您必须针对编译器错误修复此问题:)

一开始创建raindropTextures

   private Array<Texture> raindropTextures; // need an ArrayList here?

在 create() 中添加这个 -

   raindropTextures = new Array<Texture>();

然后在 render()

   // if the sizes aren't equal, a new raindrop must have been added!!
   if(raindrops.size() != raindropTextures.size())  {
     int random = MathUtils.random(0, 4);
     raindropTextures.add(someDrops.get(random));
   }        
   for(Rectangle raindrop: raindrops, Texture texture: raindropTextures) {
        // make sure the textures and raindrops correspond here; theoritically they should
        batch.draw(texture, raindrop.x, raindrop.y); 
   }

最后 -

   Iterator<Rectangle> iter = raindrops.iterator();
   Iterator<Texture> iter2 = raindropTextures.iterator();
   //later
   iter.remove()
   iter2.remove()