在AS3中按属性(XML)查找节点

时间:2014-07-31 05:07:04

标签: xml actionscript-3

我试图根据它包含的属性来定位节点。我环顾四周,显然XML.Node.(@attribute == "desired attribute")应该返回具有该特定值的所有节点但由于某种原因它不适合我。它也不适用于XML.Node.(@attribute != "desired attribute")。这就好像它没有认识到价值是相同的,或者不是。但是,如果我只做XML.Node,它将返回该类型的所有节点。这是我的代码(仅在一个类中):

package  
{
    import net.flashpunk.Entity;
    import net.flashpunk.FP;
    import net.flashpunk.graphics.Graphiclist;
    import net.flashpunk.graphics.Tilemap;
    import net.flashpunk.masks.Grid;
    import net.flashpunk.World;

    /**
     * ...
     * @author Owen 
     */
    public class Map extends Entity 
    {
        public var layer_background:Tilemap;
        public var layer_forground:Tilemap;

        public var collisionMap:Grid;

        public var player:Player;

        public function Map()
        {

        }

        public function loadMap(xmlData:Class, spawn:int = 0):void
        {
            var mapXML:XML = FP.getXML(xmlData);
            // Loading the background from XML
            layer_background = new Tilemap(Assets.TILEMAP, mapXML.@width, mapXML.@height, Constants.TILE_WIDTH, Constants.TILE_HEIGHT);
            layer_background.loadFromString(mapXML.Background);

            // Loading the forground from XML
            layer_forground = new Tilemap(Assets.TILEMAP, mapXML.@width, mapXML.@height, Constants.TILE_WIDTH, Constants.TILE_HEIGHT);
            layer_forground.loadFromString(mapXML.Forground);

            // Loading the collision map from XML
            collisionMap = new Grid(mapXML.@width, mapXML.@height, Constants.TILE_WIDTH, Constants.TILE_HEIGHT);
            collisionMap.loadFromString(mapXML.Collision);

            // Adding the player, getting location and data from XML
            player = new Player;

            var spawnNode = mapXML.Misc.Spawn.(@direction == "down");
            trace("spawnNode = " + spawnNode);

            // Nevermind this
            player.setLoc(1, 2);
            player.setDir("down");

            // Tracing the "Spawn" nodes, not attribute specific
            trace(" Not attribute specific: " + mapXML.Misc.Spawn);
            // This doesn't trace anything
            trace(" == with int (nothing traces): " + mapXML.Misc.Spawn.(@entrance == 0));

            // Same thing if we try with a string
            trace(" == with string: " + mapXML.Misc.Door.(@desLevel == "level1"));

            // This however works
            trace(" This works: " + mapXML.Misc.Door.@desLevel);

            graphic = new Graphiclist(layer_background, layer_forground);
        }





    }

}

这就是XML(它是一个.oel,只是伪装的xml)

<level width="640" height="480">
  <Collision exportMode="Bitstring">11011111111111111111
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000000000001
10000000000011100001
10000000000010100001
10000000000000100001
10000000000010100001
11111111111110111111</Collision>
  <Background tileset="tileset" exportMode="CSV">32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32
32,32,32,32,32,32,32,32,32,32,32,32,4,4,32,32,32,32,32,32
32,32,32,32,32,32,32,32,32,32,32,20,33,33,5,32,32,32,32,32
32,32,32,32,32,32,32,32,32,32,32,20,33,33,33,5,32,32,32,32
32,32,32,32,32,32,32,32,32,32,32,20,33,33,33,5,32,32,32,32
32,32,32,32,32,32,32,32,32,32,32,32,21,21,21,32,32,32,32,32
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32
32,32,32,32,32,4,4,4,4,32,32,32,32,32,32,32,32,32,32,32
32,32,32,32,20,33,33,33,33,5,32,32,32,32,32,32,32,32,32,32
32,32,32,20,33,33,33,33,33,5,32,32,32,32,32,32,32,32,32,32
32,32,32,32,21,21,21,21,21,32,32,32,32,32,32,32,32,32,32,32
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32</Background>
  <Forground tileset="tileset" exportMode="CSV">2,0,34,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,36,17,37,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,-1,16,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,35,-1,16,-1,-1,-1,-1,1
16,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,-1,16,-1,-1,-1,-1,1
18,17,17,17,17,17,17,17,17,17,17,17,19,-1,18,17,17,17,17,19</Forground>
  <Entities />
  <Misc>
    <Door id="0" x="64" y="0" desLevel="&quot;level1&quot;" desSpawn="0" />
    <Spawn id="1" x="64" y="32" entrance="0" direction="&quot;down&quot;" />
    <Door id="2" x="384" y="384" desLevel="&quot;level2&quot;" desSpawn="0" />
    <Spawn id="3" x="352" y="384" entrance="1" direction="&quot;left&quot;" />
  </Misc>
</level>

我也在我的游戏中使用Flashpunk,所以如果进口来自某些地方。我正在使用Ogmo Editor,与FlashPunk一起使用,以创建和导出XML。

2 个答案:

答案 0 :(得分:0)

它有效,但不像你期望的那样。

此代码mapXML.Misc.Spawn.(@entrance == 0)仅返回已建立节点的值和子节点。所以在你的情况下,它将是空的。 例如,trace(mapXML.Collision.(@exportMode=="Bitstring"))将返回1101...

我不知道如何使用E4X语法查找节点,但您可以通过其他方式执行此操作。这样的事情:

for each (var node:XML in mapXML.Misc.Spawn)
{
    if (node.@id == 0)
        return node;
}

答案 1 :(得分:0)

您确定mapXML.Misc.Spawn.(@entrance == 0)没有返回值吗?我已经设置了一个相同的系统,但确实如此,但它没有追溯......

尝试使用这两个命令中的一个,看看你是否仍然没有得到任何结果。

trace("length:", mapXML.Misc.Spawn.(@entrance == 0).length());
trace("xml":, mapXML.Misc.Spawn.(@entrance == 0).toXMLString());

虽然绝对确保您的XML设置完美。好像没有没有入口属性的Spawn节点会抛出错误。您可以随时使用以下安全。

mapXML.Misc.Spawn.(attribute('entrance') == 0);