Libgdx - TextureAtlas已停止我的应用程序

时间:2015-04-13 21:11:50

标签: java android libgdx textures

我正在从主游戏类加载资产,扩展了libgdx类游戏。在该类的create()方法中,我正在调用Assets.loadAtlas();,并调用:

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);
    }

从我看过的教程中,对我来说似乎是正确的。我已经给出了播放器状态,并根据播放器所处的状态在mainRenderer中绘制了动画。这段代码似乎也很好:

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);
    }

我使用TexturePacker GUI打包了Atlases,并将png和txt文件保存到/ assets / data文件夹中。

整个项目没有错误,它只是在模拟器中打开时说“应用程序没有响应”。我做错了什么?

为游戏类创建方法:

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));
    }
} 

1 个答案:

答案 0 :(得分:1)

总的来说,这与模拟器而不是应用程序有关,模拟器与Open GL ES 2.0不兼容,所以我下载了GenyMotion,现在它工作正常。

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