1
votes

I am developing a game with libgdx and i got stuck at a point. So My SpriteBatch draws for all the Rectangles that are in an array with the same texture but I want that every single one has its own texture. MY Code looks like this

    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();
     }

So what actually happening is that everytime a new Rectangle spawns in the screen the other ones who were already displayed change the Texture so every Rectangle got the same Texture . Any solutions or examples?

EDIT: http://imgur.com/46ywYyy This is my problem for people who understood my question false :) Like you can see the texture is changing for all the other rectangles but i want everyone to have their static texture

2
You need to use the Sprite class instead of Rectangle, or some other class that you create yourself. - Tenfour04

2 Answers

2
votes

They told you, one way to do it, but I see you do not clear, I'll put more or less as you might do.

I'll try to make the form more similar to what you already have in your code.

note this code may have syntax error, among others, becouse I'm doing from the editor StackOverflow.

1- create a class that is derived from sprite, for example something like:

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();
 }
}

This your code with with any changes

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);
     }

this is just an idea, but I think it can work, I hope you help

0
votes

The variable random is global for class, once a new sprite spawns it sets the value to new random value and all of them are draw by:

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

You need to track this together with location per instance.