6 – ANIMATION IN LIBGDX

 

 

INTRO

 

Animation can very easily be done by using LibGDX.  We simply draw an object based on its coordinates x and y and then regularly update the values of x and y.  This will be very clear in examples.

 

EXAMPLE 1 – MAKING A CIRCLE MOVE RIGHT

 

The code below continuously redraws a circle.  However, that circle’s x coordinate (stored in the variable circleX) is continuously increasing which makes the circle move.

 

Note that the coordinates of the circle (circleX and circleY)  are data fields.  This is necessary for them to store their values from one call to another.

 

public class PongGame extends ApplicationAdapter

{

 

   private ShapeRenderer renderer;

   private int circleX;

   private int circleY;

 

   @Override

   public void create ()

   {

       renderer = new ShapeRenderer();

       circleX = 0;

       circleY = 200;

   }

 

   @Override

   public void render ()

   {

      //Update circle's location (move right)

      circleX++;

      

      //Clear the screen

      Gdx.gl.glClearColor(0, 0, 0, 1);

      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

 

      //Render circle in new location.

      renderer.begin(ShapeType.Filled);

      renderer.setColor(Color.GREEN);

      renderer.circle(circleX, circleY, 50);

      renderer.end();

   }

}

The arrow shows the direction of the circle in the animation.  The circle will go offscreen and continue forever.