2015-01-09 40 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