【问题标题】:Libgdx - TextureAtlas has stopped my applicationLibgdx - TextureAtlas 已停止我的应用程序
【发布时间】:2015-04-13 21:11:49
【问题描述】:

我正在从扩展 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));
    }
} 

【问题讨论】:

  • 你能展示你的create方法吗?我建议在您的 create 方法以及它调用的方法中放置断点,并在调试器中逐步检查以找出 ANR 的来源。顺便说一句,对 Textures 或 TextureAtlases 的静态引用会在 Android 上的 libgdx 中引起大问题(尽管不是你描述的那个)。
  • 好的,我已经发布了调用 Assets 类的游戏类的创建方法(已编辑),我将放置一些断点并更改静态变量,完成后我将发布我的命令日志
  • 误读了你的评论,在那种情况下我不会动静态变量
  • 总的来说,这与模拟器有关,而不是应用程序,模拟器与 Open GL ES 2.0 不兼容,所以我下载了 GenyMotion,现在它工作正常。谢谢你的努力。

标签: java android libgdx textures


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    相关资源
    最近更新 更多