So I have been searching for the past hour on a way to do this but I am not finding what I am looking for.
The main point of my project is to randomly select a name from a list of names contained in an external XML file (located in the same directory as the flash file) and display the name in a text field on the stage. Once the name has been selected then it’s “used” attribute in the xml document needs to be changed from “0” to “1” so that the name is not used more than once until all of the “used” attributes are reset to “0”.
What I am currently having trouble with is getting the XML document to update after a name has been selected. I push the button that initiates the function to draw a random name, the name displays in the text area, and the “used” attribute in the xml is set to 1 for that name. But if I push the button again to pick another name, the previously drawn name’s “used” attribute goes back to 0 and the attribute for the current name is set to 1.
Here is my XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<Student>
<name used="0">James</name>
<name used="0">Cory</name>
<name used="0">Alex</name>
<name used="0">Jamie</name>
<name used="0">Aurthor</name>
<name used="0">Ariel</name>
<name used="0">Bobby</name>
<name used="0">Jennifer</name>
<name used="0">Randy</name>
<name used="0">Tiffany</name>
</Student>
And my flash coding:
import flash.net.SharedObject;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);
var xml:XML;
var updatedList:SharedObject = SharedObject.getLocal("xml");
function onLoaded(e:Event):void
{
xml = new XML(e.target.data);
var students = new Array();
for (var student:String in xml.name)
{
students[student] = xml.name[student];
}
var nameAttributes:XMLList = xml.name.attributes();
var filteredStudents = new Array();
var fsIndex = 0;
for (var index in nameAttributes)
{
if (nameAttributes[index] == "0")
{
filteredStudents[fsIndex] = new Array();
filteredStudents[fsIndex].push(xml.name[index]);
filteredStudents[fsIndex].push(index);
fsIndex = fsIndex + 1;
}
}
var random:Number = Math.floor(Math.random() * (filteredStudents.length));
/*trace(random + ": " + filteredStudents[random][0] + " at position " + filteredStudents[random][1]);*/
// xml.name of (filteredStudents[random][1]) = change "used" attribute to 1;
xml.name[(filteredStudents[random][1])].@used = "1";
updatedList.data.usedname = xml.name[(filteredStudents[random][1])];
updatedList.flush();
nameOutput.text = updatedList.data.usedname;
trace(xml);
}
generateBtn.addEventListener(MouseEvent.CLICK, getStudent);
function getStudent(event:MouseEvent):void
{
loader.load(new URLRequest("my_text_file.xml"));
}