2015-05-12 64 views
1

我正在開發一個libgdx的遊戲,我被困在一個點上。所以我的SpriteBatch繪製了所有具有相同紋理的數組中的矩形,但我希望每個人都有自己的紋理。我的代碼看起來像這樣Libgdx渲染一個矩形陣列不同的紋理

public class GameScreen implements Screen{ 

    final MrJetpack game; 

    OrthographicCamera camera; 
    SpriteBatch batch; 
    ShapeRenderer rend; 
    private Array<Rectangle> raindrops; 

    Texture enemy1,enemy2,enemy3,enemy4,endScreen; 
    TextureRegion[] enemys = new TextureRegion[4]; 

    private int random; 


    public GameScreen(final MrJetpack game){ 

this.game = game; 

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

enemy1 = new Texture(Gdx.files.internal("boxk.png")); 
enemy2 = new Texture(Gdx.files.internal("boxg.png")); 
enemy3 = new Texture(Gdx.files.internal("kugel.png")); 
enemy4 = new Texture(Gdx.files.internal("kugelk.png")); 

enemys[0] = new TextureRegion(enemy1);  
enemys[1] = new TextureRegion(enemy2);  
enemys[2] = new TextureRegion(enemy3);  
enemys[3] = new TextureRegion(enemy4); 

raindrops = new Array<Rectangle>(); 

rend = new ShapeRenderer(); 
batch = new SpriteBatch(); 

    } 


    @Override 
    public void render(float delta) { 
    // TODO Auto-generated method stub 
//Gdx.gl.glClearColor(0, (float)148/255,(float) 255/255, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    camera.update(); 




    batch.setProjectionMatrix(camera.combined); 
    rend.begin(ShapeType.Filled); 
    rend.rect(0, 0, 800, 10); 
    rend.rect(0, 160, 800, 10); 
    rend.rect(0, 320, 800, 10); 
    rend.setColor(Color.ORANGE); 
    rend.end(); 

    batch.begin(); 
    for(Rectangle raindrop: raindrops) { 
     batch.draw(enemys[random], raindrop.x - 10, raindrop.y); 
    } 
    batch.end(); 


    if(TimeUtils.nanoTime() - lastDropTime > spawnTime){ 
     spawnRaindrop(); 
     } 
    Iterator<Rectangle> iter = raindrops.iterator(); 
    while(iter.hasNext()) { 
    Rectangle raindrop = iter.next(); 
    raindrop.x -= 20 * Gdx.graphics.getDeltaTime(); 

    if(raindrop.x < 0) { 
     spawnRaindrop(); 
     iter.remove(); 
    } 
    } 


    } 


    private void spawnRaindrop() { 
    Rectangle raindrop = new Rectangle(); 
    raindrop.x = 800; 
    stages = MathUtils.random(1, 3); 
    random = MathUtils.random(0, 3); 
    raindrop.width = 30; 
    raindrop.height = 53; 
    raindrops.add(raindrop); 
    lastDropTime = TimeUtils.nanoTime(); 
    } 

那麼實際情況是,每次在屏幕上一個新的Rectangle產卵誰已經顯示改變紋理其他的人所以每次矩形得到了相同的紋理。任何解決方案或例子?

編輯:http://imgur.com/46ywYyy 這是我的問題對誰明白我的問題:)虛假人們 就像你所看到的紋理正在改變所有其它的矩形,但我想每個人都有自己的靜態紋理

+0

你需要使用Sprite類而不是Rectangle或其他你自己創建的類。 – Tenfour04

回答

2

他們告訴過你,做到這一點的一種方法,但我看到你不清楚,我會盡可能地多放一些。

我會盡量讓表單更接近你已經在你的代碼中。

note 此代碼可能有語法錯誤等因素,因爲我在編輯器StackOverflow中執行操作。

1-創建從精靈的派生類,例如像:

public class SpriteRaindrop extends Sprite{ 

    Rectangle raindrop = new Rectangle(); 

public SpriteRaindrop(Texture t, int srcWidth, 
           int srcHeigth, 
           float posX, 
           float posY){ 

     super(t, srcWidth, srcHeigth); 

     raindrop.x  = posX; 
     raindrop.y  = posY; 
     raindrop.width = srcWidth; 
     raindrop.height = srcHeigth; 

     setPosition(posX, posY); 
} 

public void updateR(){ 
    raindrod.x = getX(); 
    raindrod.y = getY(); 
} 
} 

這是你的代碼與任何更改

public class GameScreen implements Screen{ 

    final MrJetpack game; 

    OrthographicCamera camera; 
    SpriteBatch batch; 
    ShapeRenderer rend; 
    private Array<SpriteRaindrop> raindrops; 

    Texture enemy1,enemy2,enemy3,enemy4,endScreen; 
    TextureRegion[] enemys = new TextureRegion[4]; 

    private int random; 


    public GameScreen(final MrJetpack game){ 

this.game = game; 

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

enemy1 = new Texture(Gdx.files.internal("boxk.png")); 
enemy2 = new Texture(Gdx.files.internal("boxg.png")); 
enemy3 = new Texture(Gdx.files.internal("kugel.png")); 
enemy4 = new Texture(Gdx.files.internal("kugelk.png")); 

enemys[0] = new TextureRegion(enemy1);  
enemys[1] = new TextureRegion(enemy2);  
enemys[2] = new TextureRegion(enemy3);  
enemys[3] = new TextureRegion(enemy4); 

raindrops = new Array<SpriteRaindrop>(); 

rend = new ShapeRenderer(); 
batch = new SpriteBatch(); 

    } 


    @Override 
    public void render(float delta) { 
    // TODO Auto-generated method stub 
//Gdx.gl.glClearColor(0, (float)148/255,(float) 255/255, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    camera.update(); 




    batch.setProjectionMatrix(camera.combined); 
    rend.begin(ShapeType.Filled); 
    rend.rect(0, 0, 800, 10); 
    rend.rect(0, 160, 800, 10); 
    rend.rect(0, 320, 800, 10); 
    rend.setColor(Color.ORANGE); 
    rend.end(); 

    batch.begin(); 

    for(SpriteRaindrop raindrop: raindrops) { 
     //raindrop.translatex(-10f); 
     //what is raindrop.y value 
     raindrop.updateR(); 
     raindrop.draw(batch); 
    } 

    batch.end(); 


    if(TimeUtils.nanoTime() - lastDropTime > spawnTime){ 
     spawnRaindrop(); 
    } 

    Iterator<SpriteRaindrop> iter = raindrops.iterator(); 

    while(iter.hasNext()) { 

    SpriteRaindrop raindrop = iter.next(); 

    raindrop.translateX(-20f * Gdx.graphics.getDeltaTime()); 

     if(raindrop.getX() < 0) { 
      spawnRaindrop(); 
      iter.remove(); 
     } 
} 


} 


    private void spawnRaindrop() { 

     stages = MathUtils.random(1, 3); 
     random = MathUtils.random(0, 3); 

     lastDropTime = TimeUtils.nanoTime(); 

     SpriteRaindrop raindrop = new SpriteRaindrop(enemys[random], 
                30, 53, 
                800, 0); 
     raindrops.add(raindrop); 
    } 

這只是一個想法,但我認爲它可以工作,我希望你能幫助

+0

看看我把它放在imgur的鏈接 – user3629585

+1

@ user3629585如果這個答案有助於或解決你的問題,cosidera upvote /接受,因爲我有幾個像你的這樣的答覆仍然有效並不upvote,也許在某些時候我不能接受更低的接受率,看看這個http://stackoverflow.com/tour –

+0

生病嘗試這個,如果它有幫助,我會接受它 – user3629585

0

變量隨機是全球級的,一旦一個新的精靈產生它將該值設置爲新的隨機值,並且所有這些都被繪製:

batch.draw(enemys [random],raindrop.x - 10,raindrop.y) ;

您需要跟蹤每個實例的位置。

+0

我該怎麼做 – user3629585