gpt4 book ai didi

java - Sprites数组合并渲染 Sprite 时的

转载 作者:行者123 更新时间:2023-12-01 09:42:38 26 4
gpt4 key购买 nike

Sprite 桶图像、背景、r1、r2、r5、 r10、r20、r50、r100、r200、r500、k1、k2、k5、k10、k20、k50;

我在名为spawnRaindrop()的方法中创建对象。我有一个 Sprite 数组,我想在循环中改变 Sprite ,就像现在一样,它可以工作,但图像会相互合并。

  sprites = new Sprite[15];
r1 = new Sprite(atlas.findRegion("r1"));
r1.flip(false, true);
r2 = new Sprite(atlas.findRegion("r2"));
r2.flip(false, true);
r5 = new Sprite(atlas.findRegion("r5"));
r5.flip(false, true);
r10 = new Sprite(atlas.findRegion("r10"));
r10.flip(false, true);
r20 = new Sprite(atlas.findRegion("r20"));
r20.flip(false, true);
r50 = new Sprite(atlas.findRegion("r50"));
r50.flip(false, true);
r100 = new Sprite(atlas.findRegion("r100"));
r100.flip(false, true);
r200 = new Sprite(atlas.findRegion("r200"));
r200.flip(false, true);
r500 = new Sprite(atlas.findRegion("r500"));
r500.flip(false, true);
k1 = new Sprite(atlas.findRegion("k1"));
k1.flip(false, true);
k2 = new Sprite(atlas.findRegion("k2"));
k2.flip(false, true);
k5 = new Sprite(atlas.findRegion("k5"));
k5.flip(false, true);
k10 = new Sprite(atlas.findRegion("k10"));
k10.flip(false, true);
k20 = new Sprite(atlas.findRegion("k20"));
k20.flip(false, true);
k50 = new Sprite(atlas.findRegion("k50"));
k50.flip(false, true);
sprites[0] = r1;
sprites[1] = r2;
sprites[2] = r5;
sprites[3] = r10;
sprites[4] = r20;
sprites[5] = r50;
sprites[6] = r100;
sprites[7] = r200;
sprites[8] = r500;
sprites[9] = k1;
sprites[10] = k2;
sprites[11] = k5;
sprites[12] = k10;
sprites[13] = k20;
sprites[14] = k50;

创建游戏对象

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

创建并绘制数组 Sprite

 game.batch.draw(bucketImage, bucket.x, bucket.y);

for (Rectangle raindrop : raindrops) {
for (int i = 0; i < sprites.length - 1; i++) {

game.batch.draw(sprites[i], raindrop.x, raindrop.y);
}
}
game.batch.end();

结果:我附上了图片,可以看到图像相互堆积

enter image description here

最佳答案

看来您的问题是您对所有 Sprite 使用相同的 raindrop.xraindrop.y 坐标!

for (Rectangle raindrop : raindrops)
{
for (int i = 0; i < sprites.length - 1; i++)
{
// The following will draw ALL sprites at the same location!
game.batch.draw(sprites[i], raindrop.x, raindrop.y);
}
}

您可以尝试创建一个名为(例如)的新类:Raindrops,然后在该类中为每个单独的图像维护一个 x,y 坐标:

class Raindrop
{
Vector2 coordinates;
Sprite sprite;
}

然后在您的 spawnRaindrop 方法中,创建这些雨滴的数组并为每个雨滴创建一个单独的(随机?)图像。

编辑:我直接在这里编写了以下代码,没有测试任何东西,所以它很可能会出现一些错误,但没有什么是你不能自己修复的......

// This goes into your initialisation method
String regions[] = {"r1", "r2", "r5", "r10", "etc etc etc"}
Raindrop raindrops[] = new Raindrop[15];
for ( int i = 0; i < raindrops.length; i++ )
{
raindrop[i] = new Raindrop();
raindrop[i].coordinate.x = MathUtils.random(screenWidth);
raindrop[i].coordinate.y = MathUtils.random(screenHeight);
raindrop[i].sprite = atlas.findRegion(regions[(int)MathUtils.random(regions.length)]);
}

那么你的主循环应该看起来像这样:

game.batch.draw(bucketImage, bucket.x, bucket.y);
for ( Raindrop raindrop : raindrops )
{
game.batch.draw(raindrop.sprite, raindrop.coordinate.x, raindrop.coordinate.y);
}
game.batch.end();

关于java - Sprites数组合并渲染 Sprite 时的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38342642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com