I am a Junior at Wheaton College, I have just finished my first class in AS3. I have programmed a number of smaller projects, as well as a substantial bigger American Sign Language fingerspelling parser. I am a computer science major and enjoy learning languages, such as ML, Java, C, and most recently, AS3. In this tutorial you will learn how to read from XML files
in your Actionscript 3.0 programs, specifically in the context of a quiz
program. XML is a file type that
is versatile in its application and implementation. Basically, an XML file is a tree. Here is an example. [XML file] This is a list of strings, a simple document. Each string is contained in a node of
type STRING with an attribute of DATUM, which is the individual string (i.e.
godfather – unicycle).
This program tests the users ability to read American Sign Language
fingerspelling. There are two modes in this program; the default mode allows a user to input a word and view that word spelled out via fingerspelling. The second mode is a quiz mode that tests a user on how well he or she can read a word fingerspelled.
This first section imports the necessary library functions and then establishes the interface with the Sprites and textfields.
[as]package
{
import flash.display.*;
import flash.events.*;
import flash.media.*;
import flash.net.*;
import flash.text.*;
import flash.ui.*;
import flash.utils.*;
public class Fingerspeller extends Sprite
{
public function Fingerspeller()
{
var background:Sprite = new makeRectangle(0x000000, 30, 10, 330, 250);
addChild(background);
//Speed label
var speedLabel:TextField = makeLabel(285, 58, 33, 15, 0x333ccc, "Speed");
addChild(speedLabel);
//Scorekeeper correct & incorrect textboxes
var numCorrect:TextField = makeLabel(34, 200, 60, 18, 0x333ccc, "Correct: ");
addChild(numCorrect);
var numWrong:TextField = makeLabel(34, 220, 60, 18, 0x333ccc, "Incorrect: ");
addChild(numWrong);
//Quiz toggle button
var quiz:TextField = makeLabel(285, 200, 60, 15, 0x333ccc, "Quiz Me");
quiz.addEventListener(MouseEvent.CLICK, quizListener);
addChild(quiz);
//Exit Quiz toggle button
var exitQuiz:TextField = new makeLabel(285, 220, 60, 15, 0x333ccc, "Exit Quiz");
exitQuiz.addEventListener(MouseEvent.CLICK, exitQuizListener);
addChild(exitQuiz);
//repeat button
var repeat:TextField = new makeLabel(285, 180, 60, 15, 0x333ccc, "Repeat");
repeat.addEventListener(MouseEvent.CLICK, repeatListener);
addChild(repeat);
//create the display rectangle for images/movies
var rect:Sprite = makeRectangle(0xff00ff, 100, 50, 175, 131);
//add the rectangle to the stage
addChild(rect);
//rectangle to which buttons are added
var rect2:Sprite = makeRectangle(0x333ccc, 285, 75, 35, 70);
//add the rectangle to the stage
addChild(rect2);
//slow button
var button1:Sprite = makeRectangle(0x00ffff, 287, 77, 30, 15);
button1.buttonMode = true;
button1.addEventListener(MouseEvent.MOUSE_DOWN, slowSpeed);
addChild(button1);
//medium speed button
var button2:Sprite = makeRectangle(0x00ffff, 287, 98, 30, 15);
button2.buttonMode = true;
button2.addEventListener(MouseEvent.MOUSE_DOWN, regSpeed);
addChild(button2);
//fast button
var button3:Sprite = makeRectangle(0x00ffff, 287, 120, 30, 15);
button3.buttonMode = true;
button3.addEventListener(MouseEvent.MOUSE_DOWN, fastSpeed);
addChild(button3);
//textbox for input
var myText:TextField = makeLabel(130, 200, 100, 18, 0xff00ff, "");
myText.border = true;
myText.borderColor = 0x00ff55;
myText.type = TextFieldType.INPUT;
addChild(myText);
//debugging textbox
var debug:TextField = makeLabel(130, 220, 100, 18, 0xff00ff, "");
debug.border = true;
debug.borderColor = 0x00ff55;
addChild(debug);
//Instructional textbox
var instruct:TextField = makeLabel(50, 20, 280, 18, 0xff00ff, "");
instruct.border = true;
instruct.borderColor = 0x00ff55;
addChild(instruct);
//textbox to highlight the selected speed button
var highlight:TextField = makeLabel(0, 0, 30, 15, 0xff00ff, "");
highlight.border = true;
highlight.borderColor = 0x00ff55;[/as]
Each of these sections call one of two methods, makeLabel, which creates a new Sprite object, or makeLabel, which creates a new TextField object. Here is the code for these functions:
[as]//make a rectangular Sprite
function makeRectangle(color:Number, xPos:int, yPos:int, xSize:int, ySize:int):Sprite
{
var toReturn:Sprite = new Sprite();
toReturn.graphics.lineStyle(1);
toReturn.graphics.beginFill(color);
toReturn.graphics.drawRect(xPos, yPos, xSize, ySize);
return toReturn;
}
//make a textbox
function makeLabel(xPos:int, yPos:int, width:int, height:int, color:Number, text:String):TextField
{
var toReturn:TextField = new TextField();
toReturn.x = xPos;
toReturn.y = yPos;
toReturn.width = width;
toReturn.height = height;
toReturn.background = true;
toReturn.backgroundColor = color;
toReturn.text = text;
return toReturn;
}[/as]
This next function is an XML parser that will parse through an XML file and place
the string datums within an array.
First, import the appropriate library functions needed. The private variables are the array
into which we will place the strings, the XMLList we will get from the XML
flie, and my_total, which is the total number of elements in the array.
We have several functions local to the this function; these are used only
within the this function and do not need to be accessed by any outside client. First, declare and
initialize the URLLoader. This
will allow us to load an external file into our program. Next, attach an eventlistener to the
loader, listening for its completion.
When the loading is complete, the listener will run the function process
XML. Then load quiz1.xml into the
program using the URLLoader and URLRequest. This will request this file from the source repository and
then load it into a data object that is passed on into processXML.