PDA

View Full Version : global access to variables in a function.


fxr
04-17-2008, 12:21 AM
i have these two functions that work hand in hand to populate an array with xml data.

can someone tell me/explain how i can get access to that array outside of that 2nd function? this no global variable thing is melting me.

function grabQuotes ():void

{
var variables:URLVariables = new URLVariables();
var varSend:URLRequest = new URLRequest("http://192.168.1.3/pam/read_quotes_for_day_xml2.php");
var varLoader:URLLoader = new URLLoader;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
variables.date = myDate.Value.text;
varLoader.load(varSend);
}

function completeHandler(event:Event):void

{
var quotes:Array=new Array();
XML.ignoreWhitespace = true;
var data:XML = new XML(event.target.data);

for each ( var q:XML in data.quote )
{

quotes.push(q);
// trace( q );
}
//displayQuote(theResult.quote.x3,44,150);
//displayQuote(theResult.quote.x3,62,200);
// upto 48 quotes ...
}


please bear in mind your dealing with a ickle weak noob here.
thanks.

Groady
04-17-2008, 12:45 AM
You need to declare the variable outside of the function. At the moment it's only declared inside completeHandler so therefore only accessible from inside there.

fxr
04-17-2008, 12:47 AM
ok thanks Groady for the reply.. can you tell where and how to declare that array outside the function please? i have a tried a myriad of things but i just cant put my finger on it .

fxr
04-17-2008, 12:52 AM
here all of the code fwiw .. my calls to displayQuote are returing null values



package

{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;

public class charts_as extends MovieClip

{
//public var quotes:Array=new Array();
public function charts_as ()

{


var myDate:dateClass = new dateClass ();
myDate.Value.text = "2008-04-8";

function displayQuote(quote:String, quote_x:Number, quote_y:Number )

{
var myQuote:quoteClass = new quoteClass ();
addChild(myQuote);

myQuote.x = quote_x;
myQuote.y = quote_y;
myQuote.Value.text = quote;

}

function grabQuotes ():void

{
var variables:URLVariables = new URLVariables();
var varSend:URLRequest = new URLRequest("http://192.168.1.3/pam/read_quotes_for_day_xml2.php");
var varLoader:URLLoader = new URLLoader;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
variables.date = myDate.Value.text;
varLoader.load(varSend);

}

function completeHandler(event:Event):void

{
XML.ignoreWhitespace = true;
var data:XML = new XML(event.target.data);

for each ( var q:XML in data.quote )
{

quotes.push(q);
trace( q );
}
//displayQuote(theResult.quote.x3,44,150);
//displayQuote(theResult.quote.x3,62,200);

// upto 48 quotes ...
}

//var myDate:dateClass = new dateClass ();
addChild(myDate);

myDate.x = 152.5;
myDate.y = 89.8;
myDate.alpha = 0.23;
grabQuotes ();
displayQuote(quotes[45],8,50);
displayQuote(quotes[47],26,100);

}

}

}

Groady
04-17-2008, 12:55 AM
I think this is what you are wanting?

function grabQuotes ():void

{
var variables:URLVariables = new URLVariables();
var varSend:URLRequest = new URLRequest("http://192.168.1.3/pam/read_quotes_for_day_xml2.php");
var varLoader:URLLoader = new URLLoader;
var Loader.addEventListener(Event.COMPLETE, completeHandler);
var Send.method = URLRequestMethod.POST;
var Send.data = variables;
variables.date = myDate.Value.text;
var Loader.load(varSend);
var quotes:Array;
}

function completeHandler(event:Event):void

{
quotes = new Array();
XML.ignoreWhitespace = true;
var data:XML = new XML(event.target.data);

for each ( var q:XML in data.quote )
{

quotes.push(q);
// trace( q );
}
//displayQuote(theResult.quote.x3,44,150);
//displayQuote(theResult.quote.x3,62,200);
// upto 48 quotes ...
}

fxr
04-17-2008, 01:19 AM
nope, but thanks, its still not playing. i want access to that 'quotes' array outside of them functions completey, so i can manipulate that array in the main body of my package. You can see me trying to send two elements of the array to the displayQuotes function in the last lines of my complete code, but its sending null values to the text box.

Durnus
04-17-2008, 01:28 AM
package

{
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;

public class charts_as extends MovieClip

{
public var quotes:Array; //You almost had it.

public function charts_as ()

{


var myDate:dateClass = new dateClass ();
myDate.Value.text = "2008-04-8";

function displayQuote(quote:String, quote_x:Number, quote_y:Number )

{
var myQuote:quoteClass = new quoteClass ();
addChild(myQuote);

myQuote.x = quote_x;
myQuote.y = quote_y;
myQuote.Value.text = quote;

}

function grabQuotes ():void

{
var variables:URLVariables = new URLVariables();
var varSend:URLRequest = new URLRequest("http://192.168.1.3/pam/read_quotes_for_day_xml2.php");
var varLoader:URLLoader = new URLLoader;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
variables.date = myDate.Value.text;
varLoader.load(varSend);

}

function completeHandler(event:Event):void

{
XML.ignoreWhitespace = true;
var data:XML = new XML(event.target.data);

for each ( var q:XML in data.quote )
{

quotes.push(q);
trace( q );
}
//displayQuote(theResult.quote.x3,44,150);
//displayQuote(theResult.quote.x3,62,200);

// upto 48 quotes ...
}

//var myDate:dateClass = new dateClass ();
addChild(myDate);

myDate.x = 152.5;
myDate.y = 89.8;
myDate.alpha = 0.23;
grabQuotes ();
displayQuote(quotes[45],8,50);
displayQuote(quotes[47],26,100);

}

}

}
...
;)

fxr
04-17-2008, 01:51 AM
ok thanks Durnus .. but still getting an undefined value when i try to access an element of that 'quotes' array outside of my completeHandler function.

is how i am populating my array correct, for an array variable that defined outside of a function?

function completeHandler(event:Event):void

{
XML.ignoreWhitespace = true;
var data:XML = new XML(event.target.data);

for each ( var q:XML in data.quote )
{
quotes.push(q);
trace( q );
}