无法加载文件:资产

Couldn't load file: assets

我正在尝试在 Eclipse 上渲染 Tiled Map libGDX。这是我渲染地图的代码。这是我学校的作业。我真的需要帮助。一直提示找不到文件!

package screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

public class Play implements Screen {

    private TiledMap map;
    private OrthogonalTiledMapRenderer otmr;

    @Override
    public void dispose() {
        map.dispose();
        otmr.dispose();

    }

    @Override
    public void hide() {
        dispose();

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

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

        otmr.render();

    }

    @Override
    public void resize(int arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    @Override
    public void show() {

        map = new TmxMapLoader().load("assets/level1.1.tmx");

        otmr = new OrthogonalTiledMapRenderer(map);

    }

}

这是 tmx 级别文件的文本。右键单击 level1.1.tmx 并单击打开 with/text 编辑器

时出现的文本
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.3.1" orientation="orthogonal" renderorder="right-down" compressionlevel="0" width="62" height="37" tilewidth="16" tileheight="16" infinite="0" nextlayerid="4" nextobjectid="1">
 <editorsettings>
  <export target="." format="csv"/>
 </editorsettings>
 <tileset firstgid="1" source="prTiles.tsx"/>
 <layer id="2" name="Tile Layer 2" width="62" height="37">
  <data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,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0,0,0,0,
6,6,6,7,7,7,7,7,7,7,6,6,7,7,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,6,6,2,2,2,2,2,2,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
6,6,6,6,6,7,7,7,7,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,4,4,6,6,6,6,6,6,6,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
 </layer>
</map>

这是 TileSet 的文本

<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.2" tiledversion="1.3.1" name="prTiles" tilewidth="16" tileheight="16" tilecount="10" columns="10">
 <image source="prTiles.tsx" width="160" height="16"/>
</tileset>

这里是错误

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.lwjgl.LWJGLUtil (file:/D:/Downloads/libGDX/libGDX/gdx-backend-lwjgl.jar) to method java.lang.ClassLoader.findLibrary(java.lang.String)
WARNING: Please consider reporting this to the maintainers of org.lwjgl.LWJGLUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: assets
    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
    at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:96)
    at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:84)
    at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:65)
    at screens.Play.show(Play.java:57)
    at com.badlogic.gdx.Game.setScreen(Game.java:61)
    at PrisonRevelations.create(PrisonRevelations.java:10)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.run(LwjglApplication.java:120)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Cannot open a stream to a directory: assets (Internal)
    at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:143)
    at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
    ... 10 more
Caused by: java.io.FileNotFoundException: assets (Access is denied)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:155)
    at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:140)
    ... 12 more

请帮忙!!!!

您的设置有几个问题:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.lwjgl.LWJGLUtil (file:/D:/Downloads/libGDX/libGDX/gdx-backend-lwjgl.jar) to method java.lang.ClassLoader.findLibrary(java.lang.String)

现在可能不会影响你,但这意味着你需要使用 JDK 8 https://github.com/libgdx/libgdx/issues/4927

Caused by: java.io.FileNotFoundException: assets (Access is denied)

在这里它抛出这个异常,因为它找不到资产文件夹。 只要 assets 文件夹在你的项目中,它应该默认工作,但首先尝试使用绝对路径,看看它是否工作。

例如:

map = new TmxMapLoader().load("D:/assets/level1.1.tmx");

另外请注意,如果它是一个 android 项目,请看这里: