Domanda

I am creating a game in Starling and want to use XML to keep all the message dialogue that appears in the game. Here is an example of how it will be used:

<?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>

As you can see there is a structure for 3 types of interaction with an item. examine, talk and action(e.g.pickup). The state id determines the state of the game (how much progress has been made in the game) this is important as messages may change based on what has happened. When an event has occured this updates everything in the game. E.G.

State 1:

"I cannot pickup this item until I have spoke to Roger!"

Speak to Roger and move on to state 2:

State 2:

"I picked up the item because I have spoke to Roger!"

I Have created a class called message box that fetches an XMLList based on the state and currentTarget (item)

What is a good way of getting ONLY the nodes I want:

I want to get:

<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>

I do not want to write out a huge if statement to return the nodes I want:

//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....

Big problem, any help would be greatly appreciated.

UPDATE:

I want it to return an XMLList with the strings passed through in a method like this:

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

    }
È stato utile?

Soluzione

You are again leaving a lot of room for interpretations. I mean, what is item2 in your if conditions? String, Boolean, XMLNode, etc? Do you maybe also need to access the examine and chat nodes in some situations? Your code access action no matter of the state, but there is only a single action state in your XML example?

Anyways, here's a basic E4X style example for dynamically accessing nodes by their name and their id attributes:

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);

This equals the following "static" path

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

so message would contain the node with the text This is the first time I have seen item 2.

See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html for more information on XML processing.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top