1

I'm making a simple game in LibGDX. I want to use the Sprite classes methods for my main player, while using the Animation class in order to animate the player as well. I've gotten Animation to work, I just haven't been able to integrate it with the Sprite class.

Here is my code:

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
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.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;

public class Game extends ApplicationAdapter {
    SpriteBatch batch;

    private TextureAtlas runAtlas;
    private Animation runAnimation;

    private TextureAtlas idleAtlas;
    private Animation idleAnimation;

    private TextureRegion currentIdleFrame;

    private Sprite player;

    private float elapsedTime = 0;
    private float playerX = 10;
    private float playerY = 10;


    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void create () {
        batch = new SpriteBatch();

        runAtlas = new TextureAtlas(Gdx.files.internal("runSpritesheet.atlas"));
        runAnimation = new Animation(1/12f, runAtlas.getRegions());

        idleAtlas = new TextureAtlas(Gdx.files.internal("idleSpritesheet.atlas"));
        idleAnimation = new Animation(1/3f, idleAtlas.getRegions());

        player = new Sprite();

    }

    @Override
    public void render () {
        currentIdleFrame = (TextureRegion) idleAnimation.getKeyFrame(elapsedTime, true);
        elapsedTime += Gdx.graphics.getDeltaTime();

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();

        if(Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) {
            batch.draw((TextureRegion) runAnimation.getKeyFrame(elapsedTime, true), playerX, playerY);
        } else if (Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) {
            batch.draw((TextureRegion) runAnimation.getKeyFrame(elapsedTime, true), playerX, playerY);
        } else {
            player.setRegion(currentIdleFrame);
            player.draw(batch);
        }

        batch.end();
    }

    @Override
    public void dispose () {
        batch.dispose();
        runAtlas.dispose();
        idleAtlas.dispose();
    }
}

The focus is on the idle animation of the player in the "else" statement. I try setting the player sprite region to the current key frame of the idle animation sprite sheet. However, when I launch the game, no sprite with the idle animation comes up. I don't get any errors either. I am hoping someone knows how to fix this problem. Thanks for reading.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    In most cases, the Sprite class should not be used (because it conflates game state, assets, and rendering all into a single class), and this is especially true with Animations. Better to make your own Player class that tracks game state and pass it a TextureRegion to draw. – Tenfour04 Jan 20 '18 at 05:04
  • 1
    For example, using the Sprite class has already produced a bug. You are tracking player position in two different places in your code and not keeping them synced. Then you are drawing the run animation with one player position and the idle animation with the other. – Tenfour04 Jan 20 '18 at 05:12
  • Yes, you will need to override the `Sprite` class. – Micheal O'Dwyer Jan 20 '18 at 11:01
  • I'm recommending not touching the Sprite class at all. Write a base level Player or GameObject class that stores position and maybe size. Give it a draw method that takes a SpriteBatch and TextureRegion as parameters. It uses its own x and y to draw the texture region. Don't store its position in multiple places (inside and outside the Player class). – Tenfour04 Jan 20 '18 at 16:14
  • Thanks, I'll be sure to try this out! – Zein Al-bahrani Jan 20 '18 at 19:53

0 Answers0