PDA

View Full Version : using XML in other functions


savingedmund
11-19-2009, 04:28 PM
Ok, I'm getting more than a little annoyed with this.

I'm loading XML into a file, and then I want to replace the onscreen text with different snippits of text from the XML based upon button clicks.

However, I keep getting an undefined property error every time I try to use the xml in another function other than the load function, because the variable doesn't exist until the load function runs. I get that part.

First, I couldn't pass it to the function that parses the XML and puts the content on the screen (adds the text to the text box and inserts line breaks). So, I added an XML property, so that works finally. However, now I'm passing to a button event. I can't require an XML property there. To get around that, I've tried to encapsualte the XML into every variable type I can think of, and it doesn't work, because it's coersion. It's like a regressive issue now. So I'm screwed? I hope not!

Does that make sense? I need help!!


It's the bottom line of this that gives me the issue: "addXMLText(quoteNum, myQuote);"


var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, showXML);
xmlLoader.load(new URLRequest("quote.xml"));

var quoteNum:Number = new Number(0);

function showXML(e:Event):void
{
XML.ignoreWhitespace = true;
var myQuote:XML = new XML(e.target.data);
trace("in the xml");
//trace(myQuote);
addXMLText(quoteNum, myQuote);
}

function addXMLText(currentQuote:int, theXML:XML):void
{
for (var i:Number = 0; i < theXML.quote[currentQuote].p.length(); i++)
{
if (i==0)
{
onStage_txt.text = theXML.quote[currentQuote].p[i];
}
else
{
onStage_txt.appendText(theXML.quote[currentQuote].p[i]);
}
onStage_txt.appendText("\n");
onStage_txt.appendText("\n");
trace("in the loop");
}
}

function goQ1 (event:MouseEvent)
{
quoteNum = 0;
addXMLText(quoteNum, myQuote);
}

RogerClark
11-19-2009, 08:18 PM
addXMLText(quoteNum, myQuote);


won't work because myQuote is a local variable in showXML

Declare myQuote where you declare quoteNum

e.g.

var quoteNum:Number = new Number(0);
var myQuote:XML;


As far as displaying different quotes based on the button you'll need to do some testing of which button is being rolled over in goQ1

e.g

function goQ1 (event:MouseEvent)
{
switch(event.target)
{
case button0:
addXMLText(0, myQuote);
break;
case button1:
addXMLText(1, myQuote);
break;
}

}

(I've no idea what your buttons are called... button0 and button1 are just examples)

Actually as myQuote is scoped by addXMLText there is no real need to pass it just change addXMLText to use my myQuote directly

savingedmund
11-19-2009, 08:26 PM
thanks!