LibGDX 不使用 Android 资产文件夹

2022-01-12 00:00:00 android java libgdx

我在编译 Android 或 IOS 版本时遇到了读取 assets 文件夹的 libGDX 项目的问题.在尝试访问 android 模块下 assets 文件夹中的文件时,我似乎总是在 IOS 或 Android 中得到 FileNotFoundException,但在桌面上运行时却没有.我的桌面配置指向 android 资产目录,就像它应该的那样.

I am having a problem with a libGDX project reading the assets folder when compiling the Android or IOS build. While trying to access a file in the assets folder under the android module, I always seem to get a FileNotFoundException in IOS or Android, but not when running it on the Desktop. My desktop config points the android asset directory like it should.

执行以下代码时:

 public void readGameMap() throws IOException
    {
        readGameMap("gameMap.txt");
    }

    public void readGameMap(String path) throws IOException
    {

        Scanner s = new Scanner(Gdx.files.internal(path).file());


        int i = 0;


        while ((s.hasNextLine()))
        {

            String str = s.nextLine();

            if (str.length() < maze.length)
                throw new IOException("Incorrect game_map.txt structure");

            for (int j = 0; j < str.length(); j++)
            {

                Object temp;

                switch (str.charAt(j))
                {
                    case '1':
                        temp = new Wall();
                        break;

                    case '3':
                        temp = new Dot();
                        break;

                    default:
                        temp = new Object();
                        break;
                }

                maze[j][i] = temp;

            }

            i++;
        }

        s.close();

    }

抛出未找到文件异常.我的文件夹结构如下:

The file not found exception is thrown. My folder structure is as follows:

-Android
  -assets
    -gameMap.txt
-Desktop
-IOS
-HTML
-core

推荐答案

如果您使用的是Android Studio,请选择edit config...

In case if you are using Android Studio, select edit config...

将桌面应用程序"->工作目录"更改为您的 android 资产目录

Change 'Desktop Application' -> 'Working Directory' to your android asset forlder

相关文章