PDA

View Full Version : Making variables defined in a function available globally...



Tedri Mark
June 5th, 2009, 08:57 AM
As above, really. The following bit of code outputs that 'myRequest' and 'i' are not defined.





//Load external asset


function loadfail():void
{
var myRequest:URLRequest = new URLRequest("images/default.jpg");
}

function loadsuccess():void
{
var myRequest:URLRequest = new URLRequest(thumb[i]);
}

private function Load_thumbnail():void
{



for (var i:uint = 0; i<numphot; i++)
{

var testLoader:Loader = new Loader()
var testRequest:URLRequest = new URLRequest(thumb[i]);
testLoader.contentLoaderInfo.addEventListener(IOEr rorEvent.IO_ERROR, loadfail);
testLoader.contentLoaderInfo.addEventListener(Even t.COMPLETE, loadsuccess);
testLoader.load(testRequest);



var myLoader:Loader = new Loader();
myLoader.load(myRequest);
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, Thumb_loaded);
pInfor[myLoader] = i;
}

}


after a bit of reading I find that it is because 'myRequest' and 'i' are defined within functions, and so those variables are not available outside those functions.
How can I get around this? Make a class?

milkmit
June 5th, 2009, 09:10 AM
This may seem like an unnecessary pain to you, but believe me, when it comes to developing even moderately larger applications/sites, this sort of structure is a blessing.

I'm not entirely sure how it's done without classes, but I think you may be able to just throw the variable definition outside of the functions at the top of the frame's actions.

For classes, it's as simple as defining the variable in the class's definitation somewhere outside of the function (typically at the top, before the constructor), like so:

private var timelineWidth:Number;


...and then using it elsewhere in the code without the "private var" (since it's already been defined), as such:

timelineWidth = 990;


The same sort of thing should apply without classes, though I'm not really sure if you need the 'private' declaration, or if it really matters.

Tedri Mark
June 8th, 2009, 07:27 AM
okay so I've changed the code to the following


//Load external assest


//function loadfail():void
// {
var myRequest:URLRequest = new URLRequest
//= new URLRequest("images/default.jpg");
//}
//
// function loadsuccess():void
// {
//var myRequest:URLRequest //= new URLRequest(thumb[i]);
//}

private function Load_thumbnail():void
{



for (var i:uint = 0; i<numphot; i++)
{

var testLoader:Loader = new Loader()
var testRequest:URLRequest = new URLRequest(thumb[i]);
testLoader.contentLoaderInfo.addEventListener(IOEr rorEvent.IO_ERROR,{myRequest:URLRequest("images/default.jpg")});
testLoader.contentLoaderInfo.addEventListener(Even t.COMPLETE,{myRequest:URLRequest(thumb[i])});
testLoader.load(testRequest);



var myLoader:Loader = new Loader();
myLoader.load(myRequest);
myLoader.contentLoaderInfo.addEventListener(Event. COMPLETE, Thumb_loaded);
pInfor[myLoader] = i;
}

}

But this gives me the following error on both eventlistener lines:


1118: Implicit coercion of a value with static type Object to a possibly unrelated type Function.

changing the code to

testLoader.contentLoaderInfo.addEventListener(IOEr rorEvent.IO_ERROR,loadfail);
testLoader.contentLoaderInfo.addEventListener(Even t.COMPLETE,loadsuccess);

and putting


function loadfail():void
{
var myRequest:URLRequest = new URLRequest
new URLRequest("images/default.jpg");
}
function loadsuccess():void
{
var myRequest:URLRequest = new URLRequest(thumb[i]);
}

outside of the for loop but within the 'Load_thumbnail' function gives the following in the output window:

Error #2007: Parameter url must be non-null.

If I put those two functions out of the 'Load_thumbnail' function then i get


1120: Access of undefined property i.


:crying: