【问题标题】:LibGDX not using Android asset folderLibGDX 不使用 Android 资产文件夹
【发布时间】:2014-10-13 20:35:14
【问题描述】:

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

执行以下代码时:

 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();

    }

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

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

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    如果您使用的是 Android Studio,请选择编辑配置...

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

    【讨论】:

      【解决方案2】:

      你应该使用 Gdx.file.internal();

      https://github.com/libgdx/libgdx/wiki/File-handling#reading-from-a-file

      如果您使用 [设置 UI | Project Setup, Running & Debugging],此文件将包含在您的 Android 项目的资产文件夹中,准确地说是 $ANDROID_PROJECT/assets/data。您的桌面和 html 项目链接到 Eclipse 中的此文件夹,并且在 Eclipse 中执行时会自动获取它。

      【讨论】:

      • 我刚刚编辑了我的帖子以反映 Gdx.files.internal 的用法。它仍然不适用于修改。
      • 我一直设法使用 libgdx 内部读取文件,请查看Obtaining FileHandles 并尝试不同的变体。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 2013-10-14
      相关资源
      最近更新 更多