0
\$\begingroup\$

I am trying to clear out the drawn texture from the screen.

Lets take a classic game example - in many games when the character goes through the coins the coins clears out from the screen and displays a sweet animation like fade in and fade out the points collected from that coin(+10) and show the coin exploding animation on that position.

I want to do something similar to that.
I am using SpriteBatch to draw the texture region like below.

Setting texture Region:

(...)
 public Texture texture;
    public TextureRegion coin;
    texture = new Texture(Gdx.files.internal("data/texture.png"));
    texture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest); 
    coin = new TextureRegion(texture, 0, 336, 65, 73);
 (...)  


Drawing the texture into screen :

 public SpriteBatch batcher;
 batcher = new SpriteBatch();
 batcher.setProjectionMatrix(cam.combined);
  batcher.begin();
    batcher.enableBlending();
    batcher.draw(coin, x, y, 65,73);
    (...)
    (...)
  batcher.end();  

The game character is also drawn using the same logic shown above.
Now when the game character will collide with the coin that I have drawn,the coin must be cleared(vanished) and some animation or big font like +50 must fadein and fadeout in that position.I tried but didn't find anything that would do this.
So how to do that ?

\$\endgroup\$
  • \$\begingroup\$ I would recommend reading up on MVC. It looks like you've got only a View and no Model. \$\endgroup\$ – Anko Mar 29 '14 at 16:44
  • \$\begingroup\$ @Anko No !! I know whats MVC but it would be a very long code if I paste it in here because there are various classes that i am using for performing various tasks. I wrote the above code just to make it explain better and just to show how i am using libGDX to draw the textures thats it . \$\endgroup\$ – Vishal Nair Mar 29 '14 at 16:49
  • \$\begingroup\$ OK, so why not destroy the Coin instance and create a ScoreDisplay instance where it was? Shouldn't that cause your view to stop drawing the coin sprite and start drawing a +10 score text? \$\endgroup\$ – Anko Mar 29 '14 at 16:54
  • \$\begingroup\$ If i destroy the coin object then how will the batcher.draw(coin, x, y, 65,73); work it will start throwing error on that line . \$\endgroup\$ – Vishal Nair Mar 29 '14 at 17:22
  • 1
    \$\begingroup\$ I think you should make an ArrayList<GameObject> gameObjects and between your batcher's begin and end, do for (GameObject o : gameObjects) { batcher.draw(gameObject.sprite, gameObject.x, gameObject.y, ...); }. That way, you can just add or remove Coins from gameObjects to control whether they're drawn or not. Then gameObjects is your model and your batcher the view. Do you see what I mean? \$\endgroup\$ – Anko Mar 29 '14 at 17:32
1
\$\begingroup\$

Why don't you create a class for your coin object, and then collect all the coins in an array (Sprite[] coin), so whenever a collision occurs (player-coin), you call coins[i].destroy, where you can properly dispose all the referenced resources, or reset position for the next coin ?

For drawing :

 for (int i = 0; i <= quantity of coins; i++) {
          coin[i].draw(spriteBatch);
    }

For detecting collision :

 for (int i = 0; i <= quantity of coins; i++) {
         if (player.collide(coin[i])) {
             coin[i].applyFadeOut();
             score += 10;
             coin[i].destroy();
    }

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.