Search code examples
androidanimationlibgdx

Android Studio Libgdx Animation not working


I am trying to make an animation for a character who moves to the right. The character is displayed, but no animation is taking place. Everything in the code works well, except the animation. Here is the code of the class:

package com.NeverMind.DontFall.android;



import android.util.Log;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;

/**
 * Created by user on 19-Aug-15.
 */
public class PlayerClass {
    private float posX, posY, frameSpeed, leftX,  leftY, leftSizeX, leftSizeY, speed, timePassed;
    private int rightX, rightY, rightSizeX, rightSizeY;
    private Animation rightMovementAnim;
    private TextureAtlas rightMovementAt;
    private Texture stillPos, leftTexture, rightTexture;
    private SpriteBatch spriteBatch;
    public PlayerClass(int scaleX, int scaleY) {
        rawData(scaleX, scaleY);
    }

    public void update()
    {
        if (getDirection() == 0 && Gdx.input.isTouched())
            posX -= speed;
        if (getDirection() == 1 && Gdx.input.isTouched())
            posX += speed;
    }
    public void draw()
    {
        spriteBatch.begin();
        timePassed = Gdx.graphics.getDeltaTime();
        spriteBatch.draw(rightTexture, rightX, rightY, rightSizeX, rightSizeY);
        spriteBatch.draw(leftTexture, leftX, leftY, leftSizeX, leftSizeY);
        if ((getDirection() == 0 || getDirection() == 1) && Gdx.input.isTouched()) {
            timePassed += Gdx.graphics.getDeltaTime();
            spriteBatch.draw(rightMovementAnim.getKeyFrame(timePassed, true), posX, posY);
        }
        else
            spriteBatch.draw(stillPos, posX, posY);
        spriteBatch.end();

    }
    // Obtin directia in care merge Playerul
    private int getDirection()
    {
        if (leftButtonTouched(Gdx.input.getX(), Gdx.input.getY()))
            return 0;
        if (rightButtonTouched(Gdx.input.getX(), Gdx.input.getY()))
            return 1;
        return 2;
    }
    // Verific daca a fost apasat butonul pentru deplasare in stanga
    private boolean leftButtonTouched(int x, int y)
    {
        if (x > leftX && y < Gdx.graphics.getHeight() - leftY && x < leftSizeX + leftX && y > Gdx.graphics.getHeight() - leftSizeY - leftY)
            return true;
        return false;
    }
    // Verific daca a fost apasat butonul pentru deplasa in dreapta
    private boolean rightButtonTouched(int x, int y)
    {
        if (x > rightX && y < Gdx.graphics.getHeight() - rightY && x < rightSizeX + rightX && y > Gdx.graphics.getHeight() - rightSizeY - rightY)
            return true;
        return false;
    }
    // Date brute puse la sfarsit pentru a nu umple codul
    public void rawData (int scaleX, int scaleY){
        posX = 300 * scaleX;
        posY = 100 * scaleY;
        speed = 5 * scaleX;
        frameSpeed = 1 / 6f;
        rightMovementAt = new TextureAtlas(Gdx.files.internal("charMovement.atlas"));
        rightMovementAnim = new Animation(frameSpeed, rightMovementAt.getRegions());
        leftX = 50; leftY = 0; leftSizeX = 150; leftSizeY = 150;
        rightX = 250; rightY = 0; rightSizeX = 150; rightSizeY = 150;
        stillPos = new Texture(Gdx.files.internal("char2.png"));
        spriteBatch = new SpriteBatch();
        leftTexture = new Texture (Gdx.files.internal("leftButton.png"));
        rightTexture = new Texture (Gdx.files.internal("rightButton.png"));
    }
}

Solution

  • This line seems to be an error. It's causing time to never really pass.

    timePassed = Gdx.graphics.getDeltaTime();
    

    It probably should be (although I don't know exactly what you're trying to do):

    timePassed += Gdx.graphics.getDeltaTime();