0

我正在 Starling 中创建一个游戏,并希望使用 XML 来保留游戏中出现的所有消息对话。以下是如何使用它的示例:

<?xml version="1.0" encoding="utf-8" ?>
<data>
    <items>
        <bottle>
            <interact>
                <examine>
                    <state id="1">
                        <message id="1">This is the first time I have seen this bottle</message>
                        <message id="2">This is the first time I have seen this bottle 2</message>
                        <message id="3">This is the first time I have seen this bottle 3</message>
                        <message id="4">This is the first time I have seen this bottle 4</message>
                    </state>

                    <state id="2">
                        <message id="1">This is the second time I have seen this bottle</message>
                        <message id="2">This is the second time I have seen this bottle 2</message>
                        <message id="3">This is the second time I have seen this bottle 3</message>
                        <message id="4">This is the second time I have seen this bottle 4</message>
                    </state>

                    <state id="3">
                        <message id="1">This is the third time I have seen this bottle</message>
                        <message id="2">This is the third time I have seen this bottle 2</message>
                        <message id="3">This is the third time I have seen this bottle 3</message>
                        <message id="4">This is the third time I have seen this bottle 4</message>
                    </state>    
                </examine>
                <chat>
                    <state>
                        <message id="1">I cannot talk to a bottle</message>
                        <message id="2">I cannot talk to a bottle 2</message>
                        <message id="3">I cannot talk to a bottle 3</message>
                        <message id="4">I cannot talk to a bottle 4</message>
                    </state>
                </chat>

                <action>
                    <state>
                        <message id="1">I picked up the bottle</message>
                        <message id="2">I picked up the bottle 2</message>
                        <message id="3">I picked up the bottle 3</message>
                        <message id="4">I picked up the bottle 4</message>
                    </state>
                </action>
            </interact>
        </bottle>

        <item2>
            <interact>
                <examine>
                    <state id="1">
                        <message id="1">This is the first time I have seen item 1</message>
                        <message id="2">This is the first time I have seen item 2</message>
                        <message id="3">This is the first time I have seen item 3</message>
                    </state>

                    <state id="2">
                        <message id="1">This is the second time I have seen item 1</message>
                        <message id="2">This is the second time I have seen item 2</message>
                        <message id="3">This is the second time I have seen item 3</message>
                    </state>

                    <state id="3">
                        <message id="1">This is the third time I have seen item 1</message>
                        <message id="2">This is the third time I have seen item 2</message>
                        <message id="3">This is the third time I have seen item 3</message>
                    </state>    
                </examine>
                <chat>
                    <state>
                        <message id="1">I cannot talk to a item</message>
                        <message id="2">I cannot talk to a item 2</message>
                        <message id="3">I cannot talk to a item 3</message>
                        <message id="4">I cannot talk to a item 4</message>
                    </state>
                </chat>

                <action>
                    <state>
                        <message id="1">I picked up the item</message>
                        <message id="2">This may be useful</message>
                        <message id="3">I will use this for something</message>
                        <message id="4">I have put it in my pocket</message>
                    </state>
                </action>
            </interact>
        </item2>
    </items>

    <characters></characters>

    <backgrounds></backgrounds>

</data>

如您所见,有一个与项目交互的 3 种类型的结构。检查、谈话和行动(例如接送)。state id 决定了游戏的状态(游戏中取得了多少进展),这很重要,因为消息可能会根据发生的情况而改变。当事件发生时,这会更新游戏中的所有内容。例如

状态 1:

“在我与罗杰交谈之前,我无法取走这件物品!”

与 Roger 交谈并进入状态 2:

状态 2:

“我捡起那件东西是因为我和罗杰谈过了!”

我创建了一个名为 message box 的类,它根据 state 和 currentTarget (item) 获取 XMLList

什么是只获取我想要的节点的好方法:

我想得到:

<message id="1">I picked up the item</message>
<message id="2">This may be useful</message>
<message id="3">I will use this for something</message>
<message id="4">I have put it in my pocket</message>

我不想写出一个巨大的 if 语句来返回我想要的节点:

//get a list
if (item2 && state == 1) {
var message:XMLList = messageXML.items.item2.interaction.action.state.text();
}

else if(item2 && state = 2) {
var message:XMLList = messageXML.items.item2.interaction.action.state.text();
}

etc....

大问题,任何帮助将不胜感激。

更新:

我希望它返回一个 XMLList,其中的字符串以这样的方法传递:

private function searchForMessages(message:XML, target:String, state):XMLList 
    {

    }
4

1 回答 1

2

你又给解释留下了很大的空间。我的意思是item2,你的if条件是什么?String, Boolean,XMLNode等?在某些情况下,您可能还需要访问examinechat节点吗?action无论您的代码访问如何state,但您的 XML 示例中只有一个动作状态?

无论如何,这是一个基本的E4X样式示例,用于通过名称和id属性动态访问节点:

var itemName:String = 'item2';
var interactionType:String = 'examine';
var stateId:uint = 1;
var messageId:uint = 2;

var message:XMLList = messageXML.items[itemName].interact[interactionType].state.(@id == stateId).message.(@id == messageId);

这等于以下“静态”路径

messageXML.items.item2.interact.examine.state.(@id == 1).message.(@id == 2)

所以message将包含带有文本的节点This is the first time I have seen item 2

有关 XML 处理的更多信息,请参阅http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html

于 2013-10-06T13:49:04.370 回答