java - 如何通过索引获取该数组中的元素?

标签 java xml libgdx

我正在从 XML 列表创建一个数组,如下所示:

Array<Element> a = root.getChildrenByName("Element name");

我试图通过索引获取 XML 数据,但这不起作用。

int result = a.get(i).getInt("attribute name"); //Does not change anything to the result.
int result = tiles.items[i].getInt("attribute name"); //Results in an error.

我尝试将其转换为常规数组[],但这也不起作用。当调试我从 xml 文件创建的数组时,将鼠标悬停在数组上时,我可以看到底部写入的 XML,但我无法在其中看到实际的“arraytree”。

[<element att="value"/>, <element att="value"/>, ...]

当我尝试将此数组转换为普通 array[] 或 ArrayList 或类似的内容时,我得到:

Exception in thread "LWJGL Application" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.badlogic.gdx.utils.XmlReader$Element;

发布我的完整代码块和 XML:

public static Tile[][] LoadMap(String file)
{
    Tile[][] map = new Tile[0][0];

    XmlReader xml = new XmlReader();
    try {
        Element root = xml.parse(Gdx.files.internal(file));
        int width = root.getInt("width");
        int height = root.getInt("height");
        map = new Tile[width][height];

        map = new Tile[width][height];

        for (XmlReader.Element l : root.getChildrenByName("layer"))
        {
            //Populate floor tiles
            if (l.getAttribute("name").equals("floor"))
            {
                Element data = l.getChildByName("data");                    
                Array<Element> tiles = data.getChildrenByName("tile");

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        map[x][y] = new Tile();
                        map[x][y].tileTexture = tiles.get(x+y).getInt("gid");
                        //add properties since we know it uses the floor tileset.
                    }
                }
            }

            //Populate wall tiles
            else if (l.getAttribute("name").equals("wall"))
            {
                Element data = l.getChildByName("data");                    
                Array<Element> tiles = data.getChildrenByName("tile");

                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width;x++)
                    {
                        map[x][y].wallTexture = tiles.get(x+y).getInt("gid");
                        //add properties since we know it uses the wall tileset.
                    }
                }
            }               
        }

这运行良好,if 语句通过,并且每次运行的每一行代码都将至少被读取一次。

XML(对于较小尺寸的 map 可能更实用,但这只是 10x10):

<map version="1.0" width="10" height="10" tilewidth="64" tileheight="32">
 <tileset firstgid="1" name="tilesheet01" tilewidth="66" tileheight="33">
  <image source="tilesheet01.png" width="462" height="495"/>
 </tileset>
 <tileset firstgid="106" name="wallsheet01" tilewidth="66" tileheight="140">
  <image source="wallsheet01.png" width="990" height="980"/>
  <tile id="0">
   <properties>
    <property name="eWall" value="0"/>
    <property name="nWall" value="1"/>
   </properties>
  </tile>
  <tile id="1">
   <properties>
    <property name="eWall" value="1"/>
    <property name="nWall" value="0"/>
   </properties>
  </tile>
  <tile id="2">
   <properties>
    <property name="eWall" value="1"/>
    <property name="nWall" value="1"/>
   </properties>
  </tile>
 </tileset>
 <layer name="floor">
  <data>
   <tile gid="110"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="1"/>
   <tile gid="2"/>
   <tile gid="3"/>
   <tile gid="4"/>
   <tile gid="5"/>
   <tile gid="6"/>
   <tile gid="7"/>
   <tile gid="8"/>
   <tile gid="9"/>
   <tile gid="10"/>
   <tile gid="11"/>
  </data>
 </layer>
 <layer name="wall">
  <data>
   <tile gid="200"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="108"/>
   <tile gid="107"/>
   <tile gid="107"/>
   <tile gid="107"/>
   <tile gid="107"/>
   <tile gid="106"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="109"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="106"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="106"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="106"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="106"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="106"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="107"/>
   <tile gid="106"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="108"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="107"/>
   <tile gid="110"/>
   <tile gid="107"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
   <tile gid="0"/>
  </data>
 </layer>
</map>

最佳答案

这里有一个逻辑错误:

tiles.get(x+y).getInt("gid");

这将导致它按以下顺序遍历图 block 元素:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
2, 3, 4, 5, 6, 7, 8, 9, 10, 11,

...依此类推,直到元素 19。由于除了第一个之外,前 20 个图 block 的所有值都相同,这解释了您所看到的行为。您应该使用的是:

tiles.get(x + y * width).getInt("gid");

关于java - 如何通过索引获取该数组中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25453011/

相关文章:

java - 在游戏中使用 'Number' 类而不是原语?

xml - 除 web.config 之外的配置文件的 xdt 转换

ruby-on-rails - 如何使用 Nokogiri 解析 XML 并拆分节点值?

java - LibGdx 双击

java - 我可以定义 Map<String, List< 吗?扩展 BaseView>> 以便我可以从 map 中 get() 列表而无需未经检查的转换?

java - 备用java语言

java - 使用 Hibernate 访问 Oracle DBLink 表

python - Odoo 10 : Cant call my python function from menu item

java - LibGDX:让舞台上的所有 Actor 都不受检查

android - libGDX - Android Studio 中的 Google Play 游戏服务