Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie ActionScript Question

Status
Not open for further replies.

MadJock

Programmer
May 25, 2001
318
GB
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
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() == &quot;team&quot;) {

				// 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 == &quot;name&quot;) {
						teamNames[nTeams]=elementTag.firstChild.nodeValue ;
						nTeams++;
					}
					if (elementType == &quot;played&quot;) {
						aPlayed[nTeams-1]=elementTag.firstChild.nodeValue ;
					}
					if (elementType == &quot;won&quot;) {
						aWon[nTeams-1]=elementTag.firstChild.nodeValue ;
					}
					if (elementType == &quot;drawn&quot;) {
						aDrawn[nTeams-1]=elementTag.firstChild.nodeValue ;
					}
					if (elementType == &quot;lost&quot;) {
						aLost[nTeams-1]=elementTag.firstChild.nodeValue ;
					}
					if (elementType == &quot;points&quot;) {
						aPoints[nTeams-1]=elementTag.firstChild.nodeValue ;
					}
					if (elementType == &quot;goaldiff&quot;) {
						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 = &quot;&quot;;
			question.q1 = &quot;&quot;;
			question.q2 = &quot;&quot;;
			question.q3 = &quot;&quot;;
			question.q4 = &quot;&quot;;
			question.q5 = &quot;&quot;;
			question.q6 = &quot;&quot;;
		}
		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 = &quot;Premier.xml&quot;;
	} else if (nHitCount == 2) {
		cFileName = &quot;First.xml&quot;;
	}
	clearScores();    // removes content of text boxes
	init(cFileName);
	moveForward();
	stop;
}

function clearScores() {
		question.title = &quot;&quot;;
		question.q1 = &quot;&quot;;
		question.q2 = &quot;&quot;;
		question.q3 = &quot;&quot;;
		question.q4 = &quot;&quot;;
		question.q5 = &quot;&quot;;
		question.q6 = &quot;&quot;;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top