This Post is an overview to the animation process in libgdx framework, which use a 2 Dimensional array and Texture Region class, in which Frame rows reference as horizontal or the (X) axis coordinate and vertical is column or (Y), and split it using the method in texture region and add it to for loop methods, and by doing that we can think that our character is actually running when the game loop is executed in the render method.
package com.ninal.game;
import com.badlogic.gdx.*;
import com.badlogic.gdx.audio.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.math.*;
import java.util.*;
public class MyGdxGame implements ApplicationListener
{
OrthographicCamera camera;
Animation walkAnimation;
TextureRegion backgroundTexture;
TextureRegion rockTexture;
Sound collisionSound;
BitmapFont font;
SpriteBatch batch;
float time;
Rectangle manPosition;
Vector2 manVelocity;
List<Rectangle> rockPositions;
@Override
public void create()
{
// Load background
Texture texture = new Texture(Gdx.files.internal("skyBackground.jpg"));
backgroundTexture = new TextureRegion(texture, 0, 0, 2048, 563);
// Load and position rocks
Texture texture2 = new Texture(Gdx.files.internal("rock.png"));
rockTexture = new TextureRegion(texture2, 25, 0, 250, 250);
rockPositions = new ArrayList<Rectangle>();
int x = 1800;
for (int i = 0; i < 60; i++)
{
rockPositions.add(new Rectangle(x, 0, 100, 100));
x += 600 + new Random().nextInt(600);
}
// Create run animation
Texture walkSheet = new Texture(Gdx.files.internal("runAnimation.png"));
int FRAME_COLS = 6;
int FRAME_ROWS = 5;
TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS);
TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++)
{
for (int j = 0; j < FRAME_COLS; j++)
{
walkFrames[index++] = tmp[i][j];
}
}
walkAnimation = new Animation(0.025f, walkFrames);
font = new BitmapFont();
collisionSound = Gdx.audio.newSound(Gdx.files.internal("collision.wav"));
batch = new SpriteBatch();
camera = new OrthographicCamera();
resetGame();
}
@Override
public void render()
{
Gdx.gl.glClearColor(0.9f, 0.9f, 0.9f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
// Draw background
for (int i = 0; i < 30; i++)
batch.draw(backgroundTexture, i * 2900, 0, 2900, 800);
// Draw rocks
for (Rectangle r : rockPositions)
batch.draw(rockTexture, r.x, r.y, r.width, r.height);
// Draw man
batch.draw(walkAnimation.getKeyFrame(time, true), manPosition.x, manPosition.y, manPosition.width, manPosition.height);
font.draw(batch, (int) (manPosition.x / 70) + "m", camera.position.x - 10, 30);
batch.end();
// Move man
time += Gdx.graphics.getDeltaTime();
manPosition.x += manVelocity.x * Gdx.graphics.getDeltaTime();
manPosition.y += manVelocity.y * Gdx.graphics.getDeltaTime();
manVelocity.y -= 1000 * Gdx.graphics.getDeltaTime();
if (Gdx.input.isTouched() && manPosition.y == 0)
{
manVelocity.y = 500;
}
if (manPosition.y < 0)
{
manPosition.y = 0;
manVelocity.y = 0;
}
// Move camera
camera.translate((manVelocity.x - camera.viewportWidth / 80) * Gdx.graphics.getDeltaTime(), 0);
// Detect collision
for (Rectangle r : rockPositions)
{
if (r.overlaps(manPosition) && r.getCenter(new Vector2()).dst(manPosition.getCenter(new Vector2())) < 120)
{
collisionSound.play();
resetGame();
break;
}
}
}
private void resetGame()
{
configureCamera();
manPosition = new Rectangle(0, 0, 200, 200);
manVelocity = new Vector2(500, 0);
}
private void configureCamera()
{
if (Gdx.graphics.getHeight() < Gdx.graphics.getWidth())
camera.setToOrtho(false, 800, 800 * Gdx.graphics.getHeight() / Gdx.graphics.getWidth());
else
camera.setToOrtho(false, 800 * Gdx.graphics.getWidth() / Gdx.graphics.getHeight(), 800);
}
@Override
public void dispose()
{
batch.dispose();
}
@Override
public void resize(int width, int height)
{
resetGame();
}
@Override
public void pause()
{
}
@Override
public void resume()
{
}
}
0 Comments