简体   繁体   English

AS3:使用可变命令解析XML

[英]AS3: Parsing XML with a variable command

I'm doing some level load parsing from Ogmo and I ran into a problem. 我正在从Ogmo进行一些级别的负载解析,但遇到了问题。 If I have just one layer, I can read it in like this: 如果我只有一层,我可以这样阅读:

private function drawLayer(layer:String,xml:Class):void
    {
        var rawData:ByteArray = new xml;
        var dataString:String = rawData.readUTFBytes(rawData.length);
        var typeString:String = "LevelData." + layer + ".tile";
        trace ("Type STring:" + typeString);
        LevelData = new XML(dataString);

        var dataList:XMLList;
        var dataElement:XML;
        dataList = LevelData.terrain.tile; 
        //trace ("dataList: " + dataList);
        for each(dataElement in dataList)
        {    tIndex = (int(dataElement.@tx) / 32) + ((int(dataElement.@ty) / 32) * 9);
            //trace("tIndex is: " + tIndex); 
            _tiles.setTile(int(dataElement.@x) / 32, int(dataElement.@y / 32), tIndex);
        }

Where LevelData.terrain.tile is the XML parse string. 其中LevelData.terrain.tile是XML解析字符串。 However, I have a few ifferent layers and I wanted to be able to parse dynamically, ie: 但是,我有一些ifferent层,我希望能够动态解析,即:

dataList = typeString;

But that doesn't work,. 但这不起作用。 but it attempts to parse typeString out of the XML, not the "typeString" string. 但它会尝试从XML而不是“ typeString”字符串中解析出typeString。 I can't see a way to do what I'm trying to do, but I figured if anyone knew a way, they'd be on StackOverflow. 我看不到一种方法来做我想做的事情,但是我发现如果有人知道一种方法,他们就会在StackOverflow上。

Thanks in advance! 提前致谢!

You could try: 您可以尝试:

dataList = LevelData[layer].tile;

Tip: don't use capitalized names for variables; 提示:请勿对变量使用大写名称; normally those are used with class names. 通常那些与类名一起使用。 Your code might get confusing if you mix those. 如果将它们混合使用,您的代码可能会造成混乱。

I'm assuming your xml has the following structure: 我假设您的xml具有以下结构:

<level>
    <layer>
        <terrain>
            <tile/>
        </terrain>

        ...

    </layer>
</level>

Based on your method header, I'm presuming you wish to dynamically access either layer 0 tiles, or layer n tiles. 根据您的方法标题,我假设您希望动态访问第0层图块或第n层图块。 You can use E4X in the following manner: 您可以通过以下方式使用E4X:

var tiles:XMLList = levelData.layer[index]..tile;

If you have indicies associated with each layer as an attribute: 如果您有与每个图层相关的索引作为属性:

<layer index="0"/>

The above statement could become: 上面的语句可能变成:

var tiles:XMLList = levelData.layer.(@index == 0)..tile;

the ".." expression may be replace with the full path to the tile tag. “ ..”表达式可以替换为tile标记的完整路径。

Senocular has a great E4X page: Senocular具有出色的E4X页面:

http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4 http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4

I'm not too sure if anything in this format 我不太确定是否有这种格式

"LevelData."

will work for you in actionscript. 将在动作脚本中为您工作。 Always consider an object as an dictionary where you do . 在执行操作时,始终将对象视为字典。

LevelData["something"] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM