Skip to content Skip to sidebar Skip to footer

Libgdx Render For A Rectangle Array Different Textures

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 singl

Solution 1:

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:

publicclassSpriteRaindropextendsSprite{

     Rectangleraindrop=newRectangle();

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

 publicvoidupdateR(){
     raindrod.x = getX();
     raindrod.y = getY();
 }
}

This your code with with any changes

publicclassGameScreenimplementsScreen{

    final MrJetpack game;

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

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

    privateint random;


    publicGameScreen(final MrJetpack game){

this.game = game;

camera = newOrthographicCamera();
camera.setToOrtho(false, 800, 480);

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

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

raindrops = newArray<SpriteRaindrop>();

rend = newShapeRenderer();
batch = newSpriteBatch();

   }


   @Overridepublicvoidrender(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()) {

     SpriteRaindropraindrop= iter.next();

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

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


 }


  privatevoidspawnRaindrop() {

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

      lastDropTime = TimeUtils.nanoTime();

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

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

Solution 2:

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.

Post a Comment for "Libgdx Render For A Rectangle Array Different Textures"