2015-01-09 36 views
1

我跟着this tutorialLibGDX中構建了一個簡單的遊戲。這個遊戲的基本思想是:有掉下來的墜落物,玩家應該把它拖到水滴落下的地方。在渲染()中隨機更改圖像而不更改所有其他圖像

在所有的液滴從下降的一個形象和,我嘗試做創建的原始版本就是讓不同的下降(下降的不同圖像)的雨,所以我創建了一個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(); 
     } 
     } 
    } 

回答

0

在你render()功能,我相信你撥打每一幀,你畫的所有現有滴,創建一個新的下降,如果必要和遞減下降的y位置。 問題就出現了,因爲你要畫,甚至在現有滴每次你使用

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

它可以給你從以前的平局即當它是在更高的y位置不同的圖像。因此,當它下降(遞減y)時,每次渲染髮生時,圖像都會不斷變化,直到它落入桶中才最終被移除。

要解決這個問題,只有在第一次渲染一個新墨滴時纔會獲得一張隨機圖像,稍後保存該圖像並在整個使用壽命期間繪製相同的圖像。保存相應圖像的這個新陣列的大小將與雨滴陣列保持相同,除非添加了新的雨滴,在這種情況下,您將獲得一個新的隨機圖像。

我會添加一些代碼來說明這一點,但我從來沒有在Java的編程或使用libgdx所以你必須解決這個問題的編譯器錯誤:)

在開始創建raindropTextures

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

內建立()添加此 -

raindropTextures = new Array<Texture>(); 

然後內作出()

// 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() 
+0

據我所知,渲染()繪製所有滴在屏幕上,按照我的代碼,我怎麼能畫出每一次一個新的形象,每滴的的整個生命週期下拉,你可以用代碼顯示它嗎? – 2015-01-10 13:29:57

+0

我並不完全熟悉libgdx中的可用選項,但是僞代碼會在create()中創建一個紋理數組,在render()中檢查雨滴數組和紋理數組的大小是否相同,如果不添加一個額外紋理紋理數組使用一些drops.get(random),然後循環兩個紋理數組和雨滴數組。這個解釋是否更好? – raveesh 2015-01-10 13:40:52

+0

此外,一定要減少你的紋理數組每次你從render() – raveesh 2015-01-10 13:47:33