0

I am trying to load high score data in to an SWF. I have 20 dynamic text fields. 10 for initials, and 10 for scores.

Here is my Actionscript in my SWF:

var xmlData:XML = new XML();
var theURL_ur:URLRequest = new URLRequest("joust_test.xml");
var loader_ul:URLLoader = new URLLoader(theURL_ur);
loader_ul.addEventListener("complete", fileLoaded);

function fileLoaded(e:Event):void
{
   xmlData = XML(loader_ul.data);

   n1_txt.text = xmlData.name1;
   n2_txt.text = xmlData.name2;
   n3_txt.text = xmlData.name3;
   n4_txt.text = xmlData.name4;
   n5_txt.text = xmlData.name5;
   n6_txt.text = xmlData.name6;
   n7_txt.text = xmlData.name7;
   n8_txt.text = xmlData.name8;
   n9_txt.text = xmlData.name9;
   n10_txt.text = xmlData.name10;
   s1_txt.text = xmlData.score1;
   s2_txt.text = xmlData.score2;
   s3_txt.text = xmlData.score3;
   s4_txt.text = xmlData.score4;
   s5_txt.text = xmlData.score5;
   s6_txt.text = xmlData.score6;
   s7_txt.text = xmlData.score7;
   s8_txt.text = xmlData.score8;
   s9_txt.text = xmlData.score9;
   s10_txt.text = xmlData.score1;

}

Here is my XML file:

<?xml version="1.0" encoding="utf-8"?>
<joust>
    <first>
        <name1>BBH</name1>
        <score1>515250</score1>
    </first>
    <second>
        <name2>BBH</name2>
        <score2>250600</score2>
    </second>
    <third>
        <name3>MRM</name3>
        <score3>239050</score3>
    </third>
    <fourth>
        <name4>n/a</name4>
        <score4>229800</score4>
    </fourth>
    <fifth>
        <name5>REI</name5>
        <score5>215500</score5>
    </fifth>
    <sixth>
        <name6>CTS</name6>
        <score6>177650</score6>
    </sixth>
    <seventh>
        <name7>JTK</name7>
        <score7>175150</score7>
    </seventh>
    <eighth>
        <name8>JTK</name8>
        <score8>173900</score8>
    </eighth>
    <ninth>
        <name9>ACH</name9>
        <score9>171900</score9>
    </ninth>
    <tenth>
        <name10>ZAQ</name10>
        <score10>153110</score10>
    </tenth>
</joust>

Can someone help me figure out what I may be doing wrong? The SWF and XML file are in the same folder on my desktop for testing.

1 Answer 1

1

References to elements in your ActionScript code do not account for hierarchy of your XML structure.

For example, name1 and score1 are a child of first, which would be referenced as:

xmlData.first.name1
xmlData.first.score1

Or, you can use the descendant accessor (..) operator to access child properties of an XML object:

xmlData..name1
xmlData..score1

So, your code would be as follows:

var loader:URLLoader = new URLLoader(new URLRequest("joust_test.xml"));
loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);

function loaderCompleteHandler(event:Event):void
{
    var xml:XML = new XML(event.target.data);

    n1_txt.text = xml.first.name1;
    s1_txt.text = xml.first.score1;

    /* or, you can use descendant accessor */

    n2_txt.text = xml..name2;
    s2_txt.text = xml..score2;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.