如何通过索引在此数组中获取元素?

时间:2014-08-22 18:00:07

标签: 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,我无法看到实际的&#34; arraytree&#34;在那里。

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

当我试图将这个数组转换为普通数组[]或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(对于较小尺寸的地图可能更实用,但这只是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>

1 个答案:

答案 0 :(得分:1)

这里有一个逻辑错误:

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

这将使它按此顺序遍历tile元素:

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个图块的所有值都是相同的值,但第一个图块除外,这解释了您所看到的行为。您应该使用的是:

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