How to make a 2D game animation on Android AIDE application


Hello and welcome back to this article.
In this tutorial I will show you how to implement a 2D game animation on android, the first thing you need to download the app called AIDE on Playstore,
once you did that, open the AIDE app and create a new project...
name of your game should start on Uppercase letter, packages name should be all lower case.
after a game project is created, you can then navigate to the directory where you save your images and sprites that will be used for your game, then copy-paste it to the asset folder of your game project. after you have done that, Create a new class then start importing the Packages that we will be used for our game animation. having the confusion of my Article? see the code for Clarification.
import com.badlogic.gdx import com.badlogic.gdx.graphics.
import com.badlogic.gdx.graphics.g2d.
import com.badlogic.gdx.math.

after importing we go to our classified section and declare the following object, so
OrthographicCamera and well name it "camera" then a
Rectangle so well name it "manPosition" for our starting position;
Vector2 is x,y coordinates and this will be for a movement so well name it "manVelocity"
and then time,  float then "time"
and of course, we need an animation to declare Animation, and will name it "FrameAnimation", but you can name it whatever you want, It's up to you. then we also need to declare a Texture, then name it "texture".and a Sprite batch to batch, in the create method, we will initialize our object so "camera = new OrthographicCamera()", then The method to configure the camera so "configureCamera()" this method is not yet created, we will be creating this method, later on, then "Texture texture = new Texture"  to load the file we need to type "Gdx.files.internal" and the name of the image that we will use as an example "sprite sheets.png" on the next line we will divide our Sprite sheet to a column and to how many rows, to do that we need to declare a two-dimensional array so, in libgdx, there is a class  "TextureRegion" that we can use to split the image and it takes arguments of x,y coordinates also the width and height of object inside of its parameter, so we will declare two dimensional array and name it "Tmp" is equal to "TextureRegion.Split" then inside of parameter is our "spritesheet", so "spritesheet.getWidth()" divided by Frame Column, then a comma, and again, we will also divide our texture to how many frame rows. so "Texture.getHeight()" divided by "frame rows".. and another variable of an integer that we will need to declare and initialize, be sure to initialize it by the value of how many Frame columns, and how many Frame rows your Sprite batch has. so after we split our image, we need again another array that will total all the frame count.
so again, we will use a single array, so TextureRegion array and name it "Framecount", will be equal to a new "TextureRegion" and inside of our Elements will be the Frame column multiplied by Frame rows... To see the correct implementation, You can see the codes below, and test on your  java eclipse libgdx framework,
or even from your android, just download the AIDE app.



package com.aide.trainer.mylibgdxgame;
import com.badlogic.gdx.*;
 import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.*;
public class MyGdxGame implements ApplicationListener {
SpriteBatch batch;
 OrthographicCamera camera;
 Animation walkAnimation;
float time;
@Override public void create() {
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.015f, walkFrames);
 camera = new OrthographicCamera();
 configureCamera();
batch = new SpriteBatch();
}
@Override public void render() {
 Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);  camera.update();
 batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(walkAnimation.getKeyFrame(time, true), 0, 0, 200, 200);
 batch.end();
 time += Gdx.graphics.getDeltaTime();
 }
 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)
 {
 configureCamera();
 }
@Override public void pause() {
 }
@Override public void resume() {
 }
}






    

Post a Comment

0 Comments