Good Evening,

So have you ever thought that how these life bars in any game are implemented and how these keep on changing? Well I don’t think so because most of us are too busy focusing on the game play, trying to save ourselves if playing a game like Grand Theft Auto Series, Need For Speed etc.

Anyways today I am going to share something interesting with you. The logic behind the life bar thingy. Once you know the logic, I am way to sure that you can use it in any game.

I hope this topic might be useful to someone.

So let’s start shall we, have you ever wondered what those life kind of thingy’s are actually?
Well I’ll reveal the truth, those are actually still images which are resized programmatically.

 

For this you need two images 1 and 2 for outlines and power/life respectively, as shown in the picture. You need to use some basic math to get the effect of decreasing life. Ratio and proportion comes into play here.

The rectangle is rendered takes four parameters namely the (x, y, width, height, textureRegion);

Note: Texture Region is the region which has the texture (image) of a rectangle mapped via two triangles.

Assuming my screen has a resolution of 480×800. (480 is the width, 800 s the height of the screen), (0, o) start from the bottom left of the screen in openglES

 private void presentRunning() {

 int lifeRatio=6, lifeBar=6; //both need to be same as one will decrement and the other will remain still.
  batcherObj.beginBatch(Assets.items); //texture that you want to cut
  batcherObj.drawSprite(480-150, 800-30, 229, 17, Assets.lifeOutlineRegion); //to take the texture of the life outline region.

 //to draw the power bar.

 try
 /**

 *Shifting of co-ordinates is done according to the ratio, otherwise it will decrease from the
 * *sideways. See Image 4

 */

 //batcherObj.drawSprite(x,y,width, height, textureRegion);

 while(lifeBar>0){

 batcherObj.drawSprite(480-40-(lifeBar*113/lifeRatio), 800-30, (lifeBar)*226/lifeRatio, 13, Assets.lifeRegion);

 lifeBar--;

 }

 } catch (Exception e) {
  // TODO Auto-generated catch block
 // gameWorld.checkGameOver();
  }

 batcherObj.endBatch();

  }

The shifting of co-ordinates is done only for x-axis only because we need to translate it either to the left or the right.

 

 

Post By: Siddharth Verma