Game Development in Android using Libgdx Part X

In this tutorial we will be updating the different game states depending on time/enemy collision/star collections. We will edit the LevelManager class to handle changes.

LevelManager.java

public class LevelManager{
      //...........
      public Boolean IsPaused;
      Boolean levelWon;
      Boolean gameOver;
      public void Update(float dt){
               if(!isPaused && !gameOver){
                        GameTimer.Update(dt);
                        if(GameTimer.CrossTimeLimit){
                                  gameOver=true;
                                  levelWon=false;
                        }
                        if(CurrentStarCount==StarLimit){
                                   levelWon=true;
                                   gameOver=true;
                        }
               }
      }
      public void Reset(){
               GameTimer.Reset();
               //Init all the game variables like mobs/stars etc
      }
      public void SetTime(float timeLimit){
                GameTimer.TimeLimit=timeLimit;
                GameTimer.Reset();
      }
      public void Resume(){
              isPaused=false;
      }
      public void Pause(){
              isPaused=true;
      }
      public Boolean IsGameOver(){
           return gameOver;
      }
      public Boolean IsLevelWon(){
           return levelWon;
      }
}

So when we parse levels we can set the stars values and time limit for the level in this class’s object and call its update in game’s update function. In every frame we check for gameOver in main update funciton. If it is true then we check if level is Won or not and depending on that we generate a pause/restart/next level popup/screen.

Temporary Invincibility:

This was a feature we added to the protagonist, so that when it hits the laser or other enemies it doesn’t get hurt in a short span twice. This feature is usually present in many games and it is a really simple feature to add. Here is a snippet of how we applied it in our game.

Hero.java

public Timer InvincibleTimer;
public void AddDamage(){
      if(InvincibleTimer.CrossTimeLimit){
            //Add damage to hero
            InvincibleTimer.Reset();
      }
}

So if hero collides in a span shorter then the invincibleTimer time limit then it won’t add damage to the hero.

Next Tutorial:

Next tutorial would be short one on how to handle the back key event in android.

Let us know any of your doubts/suggestions; please put them in the comments section and we would try to answer them.

Thank you for the patience.

Tagged with: , , , ,
Posted in Tutorials
2 comments on “Game Development in Android using Libgdx Part X
  1. Kevin says:

    Method Resume should not set isPaused to false? (it’s set to true in both Resume() and Pause() )

    • Rahul Srivastava says:

      Hi,
      Thanks for pointing out the typo. Yes in Resume isPaused should be set to false. Updated it in the post.

Try our games



Error: Twitter did not respond. Please wait a few minutes and refresh this page.