LibGDX、GDX.files.internal() 找不到文件

LibGDX, GDX.files.internal() File not found

我在资产目录中使用 LibGDX 时遇到问题。我实际上建立了我的项目文件夹this way. I follow 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

它 returns 我 :

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: File not found: spritesheet.atlas (Internal) at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136) at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.(TextureAtlas.java:103) at com.badlogic.gdx.graphics.g2d.TextureAtlas.(TextureAtlas.java:231) at com.badlogic.gdx.graphics.g2d.TextureAtlas.(TextureAtlas.java:226) at com.badlogic.gdx.graphics.g2d.TextureAtlas.(TextureAtlas.java:216) at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:23) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:126)

我尝试了一些技巧,例如“项目”>“清理”、刷新、关闭并重新打开 Eclipse,甚至重新创建了一个项目。有什么想法吗?

也许您还没有 link编辑资产文件夹,如果是这样,

尝试右键单击 my-gdx-game-desktop,在属性对话框 select Java 构建路径中,单击 link 源按钮,然后导航至资产文件夹。

如果您已经设置好了,请尝试重新执行此步骤,如果需要请重新启动 eclipse

好的,我找到窍门了。对于那些遇到相同问题的人(在 Eclipse 上工作,但无论 IDE 是什么,它都非常相同),stack overflow already existing 上有一个线程提供了额外的解决方案。 对我来说,我必须设置 "working directory"(例如桌面主程序)。为此,继续 运行>运行 配置>参数,在工作目录部分有默认和其他。勾选 Other's box 我不得不写 ${workspace_loc:my-gdx-game-core/assets} 但我认为它像 ${workspace_loc:[你的核心目录名称]/assets}。希望对你有帮助。