我跟着this tutorial在LibGDX
中構建了一個簡單的遊戲。這個遊戲的基本思想是:有掉下來的墜落物,玩家應該把它拖到水滴落下的地方。在渲染()中隨機更改圖像而不更改所有其他圖像
在所有的液滴從下降的一個形象和,我嘗試做創建的原始版本就是讓不同的下降(下降的不同圖像)的雨,所以我創建了一個List
- someDrops
不同Texture
的而在render()
我換成:
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位置跌落。 另外我想給每滴放不同點,我應該在哪裏保存它以及選擇哪種類型的集合?
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();
}
}
}
據我所知,渲染()繪製所有滴在屏幕上,按照我的代碼,我怎麼能畫出每一次一個新的形象,每滴的的整個生命週期下拉,你可以用代碼顯示它嗎? – 2015-01-10 13:29:57
我並不完全熟悉libgdx中的可用選項,但是僞代碼會在create()中創建一個紋理數組,在render()中檢查雨滴數組和紋理數組的大小是否相同,如果不添加一個額外紋理紋理數組使用一些drops.get(random),然後循環兩個紋理數組和雨滴數組。這個解釋是否更好? – raveesh 2015-01-10 13:40:52
此外,一定要減少你的紋理數組每次你從render() – raveesh 2015-01-10 13:47:33