Search code examples
javaandroidlibgdxtextures

Libgdx - TextureAtlas has stopped my application


I'm loading in Assets from the main game class which extends the libgdx class game. In the create() method of that class i'm calling Assets.loadAtlas(); and this calls:

public static void loadAtlas() {
    String textureFile = "data/rubenSprite.txt";
            myTextures = new TextureAtlas(Gdx.files.internal(textureFile));

            TextureRegion[] walkRightFrames = new TextureRegion[4];
            for (int i = 0; i < 5; i++) 
            {
                walkRightFrames[i] = myTextures.findRegion("wframe" + (i + 1));
            }
            rubenWalkRight = new Animation(0.2f, walkRightFrames[0], walkRightFrames[1], 
                    walkRightFrames[2], walkRightFrames[3], walkRightFrames[4]);

            TextureRegion[] walkLeftFrames = new TextureRegion[4];
            for (int i = 0; i < 5; i++) 
            {
                walkLeftFrames[i] = new TextureRegion(walkRightFrames[i]);
                walkLeftFrames[i].flip(true,false);

            }
            rubenWalkLeft = new Animation(0.2f, walkLeftFrames[0], walkLeftFrames[1],
                    walkLeftFrames[2], walkLeftFrames[3], walkLeftFrames[4]);

            TextureRegion[] idleRightFrames = new TextureRegion[3];
            for (int i = 0; i < 4; i ++) 
            {
                idleRightFrames[i] = myTextures.findRegion("iframe" + (i + 1));

            }
            rubenIdleRight = new Animation(0.2f, idleRightFrames[0], idleRightFrames[1],
                    idleRightFrames[2], idleRightFrames[3]);

            TextureRegion[] idleLeftFrames = new TextureRegion[3];
            for (int i = 0; i < 4; i++) 
            {
                idleLeftFrames[i] = new TextureRegion(idleRightFrames[i]);
                idleLeftFrames[i].flip(true,false);
            }
            rubenIdleLeft = new Animation(0.2f, idleLeftFrames[0], idleLeftFrames[1], 
                    idleLeftFrames[2], idleLeftFrames[3]);

            TextureRegion[] jumpRightFrames = new TextureRegion[4];
            for (int i = 0; i < 5; i++)
            {
                jumpRightFrames[i] = myTextures.findRegion("jframe" + (i + 1));
            }

            rubenJumpRight = new Animation(0.2f, jumpRightFrames[0], jumpRightFrames[1], 
                    jumpRightFrames[2], jumpRightFrames[3], jumpRightFrames[4]);


            TextureRegion[] jumpLeftFrames = new TextureRegion[4];
            for (int i = 0; i < 5; i++)
            {
                jumpLeftFrames[i] = new TextureRegion(jumpRightFrames[i]);
                jumpLeftFrames[i].flip(true,false);
            }
            rubenJumpLeft = new Animation(0.2f, jumpLeftFrames[0], jumpLeftFrames[1], 
                    jumpLeftFrames[2], jumpLeftFrames[3], jumpLeftFrames[4]);   

            TextureRegion fallRight = new TextureRegion();
            fallRight = myTextures.findRegion("fall");
            rubenFallRight = new Animation(0f, fallRight);

            TextureRegion fallLeft = new TextureRegion(fallRight);      ;
            fallLeft.flip(true,false);
            rubenFallLeft = new Animation(0f, fallLeft);
    }

From the tutorials I've looked at, it seems correct to me. I've given the player states and drawn the animations within them inside the mainRenderer depending on the state the player is in. This code seems fine also:

private void renderRuben () {
        TextureRegion keyFrame;
        switch (world.ruben.getState()) {
        case Ruben.RUBEN_STATE_HIT:
            keyFrame = Assets.bobHit;
            break;
        case Ruben.RUBEN_STATE_WALKING: 
            if (Ruben.facingRight == true) 
            {
                keyFrame = Assets.rubenWalkRight.getKeyFrame(world.ruben.getStateTime(), Animation.ANIMATION_NONLOOPING);
                break;
            }
            else 
            {
                keyFrame = Assets.rubenWalkLeft.getKeyFrame(world.ruben.getStateTime(), Animation.ANIMATION_NONLOOPING);
                break;
            }
        case Ruben.RUBEN_STATE_FALL:
            if (Ruben.facingRight == true) {
                keyFrame = Assets.rubenFallRight.getKeyFrame(world.ruben.getStateTime(), Animation.ANIMATION_NONLOOPING);
                break;
                                           }
            else {
                keyFrame = Assets.rubenFallLeft.getKeyFrame(world.ruben.getStateTime(), Animation.ANIMATION_NONLOOPING);
                break;
                 }
        case Ruben.RUBEN_STATE_JUMP:  
            if (Ruben.facingRight == true) {
                keyFrame = Assets.rubenJumpRight.getKeyFrame(world.ruben.getStateTime(), Animation.ANIMATION_NONLOOPING);
                break;
                                           }
            else {
                keyFrame = Assets.rubenJumpRight.getKeyFrame(world.ruben.getStateTime(), Animation.ANIMATION_NONLOOPING);
                break;
                 }
        case Ruben.RUBEN_STATE_IDLE:
        default:
            if (Ruben.facingRight == true) {
            keyFrame = Assets.rubenIdleRight.getKeyFrame(world.ruben.getStateTime(), Animation.ANIMATION_LOOPING);
            }
            else {
            keyFrame = Assets.rubenIdleRight.getKeyFrame(world.ruben.getStateTime(), Animation.ANIMATION_LOOPING);
            }
    }


        float side = world.ruben.getVelocity().x < 0 ? -1 : 1;
        if (side < 0)
            batch.draw(keyFrame, world.ruben.position.x + 0.5f, world.ruben.position.y - 0.5f, side * Ruben.RUBEN_WIDTH, Ruben.RUBEN_HEIGHT);
        else
            batch.draw(keyFrame, world.ruben.position.x - 0.5f, world.ruben.position.y - 0.5f, side * Ruben.RUBEN_WIDTH, Ruben.RUBEN_HEIGHT);
    }

I packed the Atlases using TexturePacker GUI and saved the png and txt file into /assets/data folder.

The overall project has no errors, it just says 'application not responding' when it opens up in the emulator. What am i doing wrong?

Create Method for game class:

public class GameView extends Game {
    // used by all screens
    public SpriteBatch batcher;
    @Override
    public void create () {
        batcher = new SpriteBatch();
        Settings.load();
        Assets.loadAtlas();
        setScreen(new MainMenuScreen(this));
    }
} 

Solution

  • Overall it was something to do with the Emulator not the application, The emulator wasn't compatible with Open GL ES 2.0, So I downloaded GenyMotion and now it works fine.

    http://www.genymobile.com/en/