Game Development Reference
In-Depth Information
break your code. A new feature in ActionScript 3 is the ability to
parse through XML data just like any other object in Flash. This
feature is known as E4X (ECMAScript for XML), and it makes XML
a native data type in Flash, just like numbers or strings. Because of
this, parsing XML is much faster and allows you to move through a
structurelikeyouwouldasetofobjectsandarrays.Thefollowing
example uses a URLLoader to load the XML used in the previous
example.
var quiz:XML;
var loader:URLLoader = new URLLoader(new URLRequest( " quiz.xml " ));
loader.addEventListener(Event.COMPLETE, onXMLLoad, false,
0, true);
function onXMLLoad(e:Event) {
quiz = XML(e.target.data);
trace(quiz.problem[0].question); //
"
What does this topic
cover?
"
}
To use the data as XML once it is loaded, you simply use the
XML conversion function and assign it to a variable. To learn more
about the more advanced features of E4X such as filtering and
searching, look to the Flash reference documentation on XML. For
the purposes of this chapter, our use of XML will be more straight-
forward. Let
s look at a practical example of how XML can be used
to store puzzle data for a game.
'
Crossword Puzzle
One of the most popular types of word games in print or electronic
media is the crossword puzzle. There are many variations of the
crossword puzzle, but the traditional American square grid type is
the style we will work with ( Fig. 10.1 ). It consists of overlapping
horizontal and vertical words with unique numbers denoting the
start of a word. Each word has a clue associated with it, which can
be another word or an entire phrase.
In the following exercise, we will lay out the structure of a cross-
word puzzle in XML and then create a simple crossword engine
that will display the puzzle and allow a player to fill it in. Any XML
to be used in Flash must have only one root node , that is, the node
that opens and closes the file. Any additional nodes will be ignored
when the XML is parsed. We start with an opening node labeled
which will encase our entire puzzle. Inside an opening
XML tag you can add parameters, called attributes , which allow you
to add any information pertinent to the node. In this case, we
define the width and the height of the puzzle in question. Attribute
values should be in quotes and do not need any type of separator
between them.
crossword,
Search Nedrilad ::




Custom Search