首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >甚至在使用Libgdx打开之前停止工作

甚至在使用Libgdx打开之前停止工作
EN

Stack Overflow用户
提问于 2017-11-02 10:36:06
回答 1查看 143关注 0票数 2

我在Android上构建了一个简单的应用程序,使用Libgdx来显示一个由6幅.png图像组成的动画,我制作了一个TextureAtlas。该项目构建良好,没有任何错误,但是当我尝试在模拟器中运行应用程序时,它会在打开时崩溃。我知道这不是仿真器的错误,因为我已经构建了一个应用程序,使用相同的配置和库来显示文本,并且这个应用程序也崩溃在我的Galaxy 5上。

下面是核心.java文件的代码:

代码语言:javascript
复制
package com.mygdx.mytestgame;

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

public class MyTestGame extends ApplicationAdapter {
    private SpriteBatch batch;
    private TextureAtlas shooterAtlas;
    private Animation<TextureRegion> animation;
    private float timePassed = 0;

@Override
public void create () {
    batch = new SpriteBatch();

    shooterAtlas = new TextureAtlas(Gdx.files.internal("shooter.atlas"));
    animation = new Animation<TextureRegion>(1/30f, shooterAtlas.getRegions());
}

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

@Override
public void render () {
    Gdx.gl.glClearColor(0,1,0,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    timePassed += Gdx.graphics.getDeltaTime();
    batch.draw(animation.getKeyFrame(timePassed, true), 300, 500);
    batch.end();
    }

}

下面是文件:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>

代码语言:javascript
复制
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="26" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/GdxTheme" >
    <activity
        android:name="com.mygdx.mytestgame.AndroidLauncher"
        android:label="@string/app_name" 
        android:screenOrientation="landscape"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

下面是逻辑猫上显示的崩溃:

代码语言:javascript
复制
11-02 12:52:27.514 3634-3666/com.mygdx.mytestgame E/AndroidRuntime: FATAL EXCEPTION: GLThread 193
                                                                Process: com.mygdx.mytestgame, PID: 3634
                                                                com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: shooter.atlas (Internal)
                                                                    at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:103)
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:231)
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:226)
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:216)
                                                                    at com.mygdx.mytestgame.MyTestGame.create(MyTestGame.java:21)
                                                                    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:275)
                                                                    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1555)
                                                                    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270)
                                                                 Caused by: java.io.FileNotFoundException: shooter.atlas
                                                                    at android.content.res.AssetManager.openAsset(Native Method)
                                                                    at android.content.res.AssetManager.open(AssetManager.java:374)
                                                                    at android.content.res.AssetManager.open(AssetManager.java:348)
                                                                    at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:103) 
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:231) 
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:226) 
                                                                    at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:216) 
                                                                    at com.mygdx.mytestgame.MyTestGame.create(MyTestGame.java:21) 
                                                                    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:275) 
                                                                    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1555) 
                                                                    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1270) 

它说读取文件"shooter.atlas“时出错,这是我在动画中使用的TextureAtlas。此文件名正确,位于C:\Users\user\Documents\libGDX Projects\android\assets中。

解决这个问题的办法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-02 11:45:10

我解决了这个问题,事实证明,这个问题非常琐碎。我下载了地图集文件从一个在线来源,该文件不是在适当的阿特拉斯格式。相反,它是一个包含.png文件的文件夹。因此,我提取了这些.png文件,并使用gdx-texturepacker.jar工具从.png文件中手动打包了一个Atlas文件,该工具可以从这里下载。我将Atlas文件导出到相同的工作目录:C:\Users\user\Documents\libGDX Projects\android\assets,并简单地将"shooter.atlas“重新命名为新的Atlas文件的名称:"shooter2.atlas”et voilà!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47073169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档