【问题标题】:LibGDX, GDX.files.internal() File not foundLibGDX, GDX.files.internal() 找不到文件
【发布时间】:2019-04-14 00:15:49
【问题描述】:

我在使用带有资产目录的 LibGDX 时遇到问题。我实际上构建了我的项目文件夹this way。我关注this tutorial学习。 (我在 Eclipse 上工作) 我使用的代码是:

package com.mygdx.game;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
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.utils.Timer;
import com.badlogic.gdx.utils.Timer.Task;

public class MyGdxGame implements ApplicationListener {
    private SpriteBatch batch;
    private TextureAtlas textureAtlas;
    private Sprite sprite;
    private int currentFrame = 1;
    private String currentAtlasKey = new String("0001");

    @Override
    public void create() {        
        batch = new SpriteBatch();
        // THE PROBLEM IS UNDER THIS LINE
        textureAtlas = new TextureAtlas(Gdx.files.internal("spritesheet.atlas"));
        AtlasRegion region = textureAtlas.findRegion("0001");
        sprite = new Sprite(region);
        sprite.setPosition(120, 100);
        sprite.scale(2.5f);
        Timer.schedule(new Task(){
                @Override
                public void run() {
                    currentFrame++;
                    if(currentFrame > 20)
                        currentFrame = 1;

                    // ATTENTION! String.format() doesnt work under GWT for god knows why...
                    currentAtlasKey = String.format("%04d", currentFrame);
                    sprite.setRegion(textureAtlas.findRegion(currentAtlasKey));
                }
            }
            ,0,1/30.0f);
    }

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

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

        batch.begin();
        sprite.draw(batch);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

(如果代码难以阅读,我使用与下面链接的教程中完全相同的代码)

我的包浏览器如下所示: imgur link

它还给我:

线程“LWJGL 应用程序”com.badlogic.gdx.utils.GdxRuntimeException 中的异常:找不到文件:spritesheet.atlas(内部) 在 com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136) 在 com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.(TextureAtlas.java:103) 在 com.badlogic.gdx.graphics.g2d.TextureAtlas.(TextureAtlas.java:231) 在 com.badlogic.gdx.graphics.g2d.TextureAtlas.(TextureAtlas.java:226) 在 com.badlogic.gdx.graphics.g2d.TextureAtlas.(TextureAtlas.java:216) 在 com.mygdx.game.MyGdxGame.create(MyGdxGame.java:23) 在 com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149) 在 com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)

我尝试了一些技巧,比如 Project>Clean、refresh、关闭和重新打开 Eclipse,甚至重新创建了一个项目。有什么想法吗?

【问题讨论】:

    标签: java eclipse libgdx


    【解决方案1】:

    好的,我找到了窍门。对于那些遇到相同问题的人(在 Eclipse 上工作,但不管 IDE 是什么),stack overflow already existing 上有一个线程可以提供额外的解决方案。 对我来说,我必须设置 “工作目录”(例如桌面主目录)。为此,请继续Run>Run configuration>Arguments,在工作目录的部分有Default 和Other。勾选 Other's box 我不得不写 ${workspace_loc:my-gdx-game-core/assets} 但我认为它像 ${workspace_loc: [name-of-your-core-directory]/assets}。希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      可能你还没有链接资产文件夹,如果是的话,

      尝试右键单击 my-gdx-game-desktop,在属性对话框中选择 Java Build Path,单击链接源按钮,然后导航到 assets 文件夹。

      如果你已经设置好了,试试这个步骤,如果需要重启eclipse

      【讨论】:

      • 感谢您的回答,我的情况不是这样。但我知道这可能是它确实不起作用的一个原因。
      猜你喜欢
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多