Hey folks,
I have written a script that will display the contents of an xml file (football results) in a flash movie.
The results are displayed 10 teams at a time. After viewing the first ten, the user clicks a 'MoveForward()' button to go to the next ten.
OK so far
There is a button that calls a function 'update()'. This function should load a different XML file (the next league) and display the results.
Under any circumstances, results are displayed using the 'moveForward()' function.
The function should load the new XML file, populate the arrays holding the results of the league and then call the 'moveForward()' function to display the results.
However, when it calls the 'MoveForward()' function, all arrays are empty. By clicking the 'MoveForward' button (which only calls the 'moveForward()' function, the results are visible (hence the arrays are populated).
I fail to understand how this works, I have traced expressions till I am blue in the face!!
My only theory is that the 'update()' function is calling the 'moveForward()' function before the onLoad callback for the XML file is complete.
I have attached my script (flash file available if required) and would be extremely grateful for any advice!
Thanks in advance,
Graeme
I have written a script that will display the contents of an xml file (football results) in a flash movie.
The results are displayed 10 teams at a time. After viewing the first ten, the user clicks a 'MoveForward()' button to go to the next ten.
OK so far
There is a button that calls a function 'update()'. This function should load a different XML file (the next league) and display the results.
Under any circumstances, results are displayed using the 'moveForward()' function.
The function should load the new XML file, populate the arrays holding the results of the league and then call the 'moveForward()' function to display the results.
However, when it calls the 'MoveForward()' function, all arrays are empty. By clicking the 'MoveForward' button (which only calls the 'moveForward()' function, the results are visible (hence the arrays are populated).
I fail to understand how this works, I have traced expressions till I am blue in the face!!
My only theory is that the 'update()' function is calling the 'moveForward()' function before the onLoad callback for the XML file is complete.
I have attached my script (flash file available if required) and would be extremely grateful for any advice!
Thanks in advance,
Graeme
Code:
// define arrays which will be populated with results
var teamNames = new Array();
var aPlayed = new Array();
var aWon = new Array();
var aDrawn = new Array();
var aLost = new Array();
var aPoints = new Array();
var aDifference = new Array();
var nHitCount = 0
var nTeams;
var nStart; // declares starting point in array when iterating thru for display
// e.g. displays first ten, then next ten....
var urlXML = new XML();
function init(cFileName) {
// reset all variables and arrays
urlXml.close;
teamNames = new Array();
aPlayed = new Array();
aWon = new Array();
aDrawn = new Array();
aLost = new Array();
aPoints = new Array();
aDifference = new Array();
// set callback function
urlXML.onLoad = convertXML;
//load xml file
urlXML.load(cFileName);
// set counters to zero
nTeams=0;
nStart=0;
}
function convertXML () {
mainTag = new XML();
elementTag = new XML();
aTeamList = new Array();
mainTag = this.firstChild.nextSibling;
nTeams=0;
if (mainTag.nodeName.toLowerCase() == "teams") {
// if we have a match, collect all of the questions beneath it as an array of xml objects
aTeamList = mainTag.childNodes;
// get tags
for (i=0; i<=aTeamList.length; i++) {
if (aTeamList[i].nodeName.toLowerCase() == "team") {
// we get the child node array beneath the questions
elementList = aTeamList[i].childNodes;
nTeamsuestions=elementList.length;
// and loop through that looking for the data we need
for (j=0; j<=elementList.length; j++) {
elementTag = elementList[j];
elementType = elementTag.nodeName.toLowerCase();
if (elementType == "name") {
teamNames[nTeams]=elementTag.firstChild.nodeValue ;
nTeams++;
}
if (elementType == "played") {
aPlayed[nTeams-1]=elementTag.firstChild.nodeValue ;
}
if (elementType == "won") {
aWon[nTeams-1]=elementTag.firstChild.nodeValue ;
}
if (elementType == "drawn") {
aDrawn[nTeams-1]=elementTag.firstChild.nodeValue ;
}
if (elementType == "lost") {
aLost[nTeams-1]=elementTag.firstChild.nodeValue ;
}
if (elementType == "points") {
aPoints[nTeams-1]=elementTag.firstChild.nodeValue ;
}
if (elementType == "goaldiff") {
aDifference[nTeams-1]=elementTag.firstChild.nodeValue ;
}
}
}
}
}
}
function moveForward() {
// textbox are all called 'question' this is legacy from old system
if (nStart < TeamNames.length) {
if (nStart > 0) {
question.title = "";
question.q1 = "";
question.q2 = "";
question.q3 = "";
question.q4 = "";
question.q5 = "";
question.q6 = "";
}
var nFinish = nStart + 9
for (i=nStart; i<=(TeamNames.length) && (i<=nFinish); i++) {
question.title = question.title+TeamNames[i]+chr(10);
question.q1 = question.q1+aPlayed[i]+chr(10);
question.q2 = question.q2+aWon[i]+chr(10);
question.q3 = question.q3+aDrawn[i]+chr(10);
question.q4 = question.q4+aLost[i]+chr(10);
question.q5 = question.q5+aPoints[i]+chr(10);
question.q6 = question.q6+aDifference[i]+chr(10);
// once displayed ten records, set the start point for next block
if (i == nStart + 9) {
nStart = nStart + 10;
}
}
}
}
function update() {
var cFileName = new String;
nHitCount++;
if (nHitCount == 1) {
cFileName = "Premier.xml";
} else if (nHitCount == 2) {
cFileName = "First.xml";
}
clearScores(); // removes content of text boxes
init(cFileName);
moveForward();
stop;
}
function clearScores() {
question.title = "";
question.q1 = "";
question.q2 = "";
question.q3 = "";
question.q4 = "";
question.q5 = "";
question.q6 = "";
}