PDA

View Full Version : Variables in a class



nikomax
June 8th, 2008, 02:12 AM
Hi there,
I had tried searching this for the last 2 hours =[ so I decided to give in and ask.

I am have a number of variables defined in my document class and I have noticed that if I were to call a function in the same class to edit the value of the variable, the change would only apply to the scope of the function.

Now I hope I used scope in the right context there =D

Here is my code to show what I mean:


package {
import flash.display.*;
import flash.xml.*;
import flash.events.*;
import com.xml.xmlLoad;
import com.gui.mainGui;

public class main extends MovieClip {

private var _targetURL:String = "Musicinfo.xml";
private var getXMLLoad:xmlLoad;
private var getGui:mainGui;
public var xmlData:XML;
public var _lessonInfo:XMLList;

public function main() {
loadXML(_targetURL);
processXML();//This will return TypeError: Error #1009:
//Cannot access a property or method of a null object reference.
//at main/::processXML()
trace(xmlData);//This will also return an error similar to above

}
function loadXML(_targetURL) {
getXMLLoad = new xmlLoad(_targetURL);
getXMLLoad.addEventListener(Event.COMPLETE, onComplete);
}
function onComplete(evt:Event) {
xmlData = getXMLLoad.getXML();
trace(xmlData.fileInfo.blurb);//This traces the XML data
trace(xmlData.fileInfo.author);//And so does this
}
function processXML() {
trace(xmlData.fileInfo.blurb);//This dosen't =[
}
}
}


Could someone explain why this is?
This may be a AS2 understanding but I thought that if I at changed the value of a variable made in the class, then the value would be the new value for every time the variable was called after the new value was put there.

Anogar
June 8th, 2008, 02:31 AM
You can't access the xmlData variable until you've instantiated the variable, or it's null.

TheCanadian
June 8th, 2008, 03:31 AM
Problem is you call processXML before it's loaded. You should call the function in the onComplete handler.

nikomax
June 8th, 2008, 09:13 AM
Ok I probably thought that by the time processXML(); was called the XML would be already loaded...

Does flash process in a liner way? I thought the functions would of been run in this order:

loadXML(); -> onComplete(); -> processXML();

Therefore I thought that the value of xmlData would then be the XML file when it came to do the processXML(); function.

djheru
June 8th, 2008, 10:05 AM
no, it will start the processXML() immediately after loadXML() unless you specifically tell it to wait until the loadXML complete event dispatches.

nikomax
June 8th, 2008, 12:49 PM
How would I do that?

EDIT::
Well thinking about it could i just add an even listener?

amarghosh
June 9th, 2008, 05:38 AM
How would I do that?

EDIT::
Well thinking about it could i just add an even listener?

yeah.. and u r already doing it. call processXml from onComplete