PDA

View Full Version : access variables declared inside functions


kayaScript
06-11-2008, 09:22 PM
Hi guys, i have a problem in AS3

I have a problem with a function that needs to access a variable declared inside another function.

What I understand is how a variable can change due to var X:Number=0
because var PictureLink:String = Array[X] when X increments. What i don't understand is how when an event listener listens for a mouse click on a movieclip which load another movieclip with link PictureLink, it knows which Array[X] it is when the X has already incremented through all the pics.

By the tutorial I watched (Cartoon Smart AS3 XML Gallery), he said "we need to create variable PictureLink to store Array[X] at a particular time to when user click on thumbnail, it goes to the correct link.

This all worked fine but the code was getting too long and i wanted to seperate this massive functions into their own .as files, this is when it stuffed up.

WHY, because the loaded was refering to Array[X] when that var was declared in the previous function and now I can't reference it .

Here's my code. Some things aren't in the, like gallery pane and some other layout script because they're in other .as files, they won't matter though to solve the problem.

var mainPicArea:MovieClip = new MovieClip();
mainPicArea.buttonMode = true;
mainPicArea.useHandCursor = true;
addChild(mainPicArea);

var all_thumbs:MovieClip = new MovieClip();
all_thumbs.buttonMode = true;
all_thumbs.useHandCursor = true;
addChild(all_thumbs);

galleryPane.source = all_thumbs;

var fadeIn:Tween;
var fadeOut:Tween;

var urlVar:String = "xml/gallery.xml";

var highResPathList:Array = [];
var lowResPathList:Array = [];
var thumbPathList:Array = [];
var pictureTitleList:Array = [];
var pictureDescList:Array = [];

var totalPics:Number;

var mainPicPlacementX:Number;
var mainPicPlacementY:Number = 100;

//LOAD THUMBNAILS
var thumbLoader:Loader;
// variable for load thumbs function loop
var c:Number = 0;
// spacing between each thumb on x-axis. Number includes thumb width plus spacing
var xSpacing:Number = 105;
// spacing between each thumb on y-axis
var ySpacing:Number = 105;
//how many thumbs are occupying a row
var xs:Number = 0;
//how many thumbs are occupying a column
var ys:Number = 0;
var thumbRowCount:Number;
var thumbColumnCount:Number;
var thumb_orientation:String;

//////////////////////


function loadTheThumbs()
{
thumbLoader = new Loader();
var thumbRequest:URLRequest = new URLRequest(thumbPathList[c]);
thumbLoader.load(thumbRequest);
thumbLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, whenThumbsLoad);

function whenThumbsLoad(event:Event):void
{
var thisMainsLink:String = highResPathList[c];
var thisThumbsLink:String = lowResPathList[c];
var thisPicsTitle:String = pictureTitleList[c];
var thisPicsDesc:String = pictureDescList[c];

//spread thumnails along x-axis by xs
thumbLoader.x=(xs*xSpacing); //add pixels to end of equation for a buffer. For eg ....+8 (optional)
//spread thumnails along y-axis by ys
thumbLoader.y=(ys*ySpacing);

if (thumbColumnCount==0){
xs = xs + 1;
//trace("row space")
}
if (thumbRowCount==0){
//trace("column space")
ys = ys + 1;
}
if (thumbColumnCount==0 && xs==thumbRowCount) //if row has reached the max thumbs per row, then start a new row
{
//trace("row add")
//by reseting the x placement
xs = 0;
//on a different row
ys = ys + 1;
}
if (thumbRowCount==0 && ys==thumbColumnCount) //if row has reached the max thumbs per column, then start a new column
{
//trace("column add")
//by reseting the x placement
ys = 0;
//on a different row
xs = xs + 1;
}

thumbLoader.addEventListener(MouseEvent.CLICK, loadMainPic); //listener when thumbnail has been clicked

all_thumbs.addChild(thumbLoader);
c = c + 1;

if (c<totalPics)
{
loadTheThumbs(); // calls the first function in this file again (for the amount of thumbs)
} else {
xs=0;
ys=0;
c=0;
}
} // end if(c<totalPics)
} // end whenThumbsLoad
} // end loadTheThumbs

///////////////////

function loadMainPic(event:MouseEvent):void
{
thumbLoader.removeEventListener(MouseEvent.CLICK, loadMainPic);
if (mainPicArea.numChildren == 1){
mainPicArea.removeChildAt(0);
}

picDesc.text = "";
picTitle.text = "";
loadingText.x = mainPicPlacementX;
loadingText.y = mainPicPlacementY;

var mainLoader:Loader = new Loader();
var mainRequest:URLRequest = new URLRequest(thisThumbsLink); ////THIS IS THE PROBLEM, I CAN'T GET "thisThumbsLink from 'whenThumbsLoad function"
mainLoader.load(mainRequest);
mainLoader.contentLoaderInfo.addEventListener(Prog ressEvent.PROGRESS, progressHandler); //listener while main pic loading
mainLoader.contentLoaderInfo.addEventListener(Even t.COMPLETE, mainLoaded); //listener when main pics has loaded

function progressHandler(event:ProgressEvent):void //pre-loader function
{
var kbLoaded:String = Number(event.bytesLoaded/1024).toFixed(1);
var kbTotal:String = Number(event.bytesTotal/1024).toFixed(1);
loadingText.text = "Loaded "+kbLoaded+" of "+kbTotal + " KB";
} // end progressHandler

function mainLoaded(event :Event):void
{
thumbLoader.removeEventListener(MouseEvent.CLICK, loadMainPic);
loadingText.text="";

if (thumb_orientation == "horizontal" || thumb_orientation == "vertical"){
mainPicPlacementX = (stage.stageWidth / 2) - (mainLoader.width / 2); //if you want mainPic to move on x-axis in relation to stage
mainPicArea.x=mainPicPlacementX;
}

mainPicArea.y=mainPicPlacementY;
mainPicArea.addChild(mainLoader);
fadeIn = new Tween(mainPicArea,"alpha", None.easeNone, 0, 1,1, true)
picTitle.text = thisPicsTitle;
picDesc.text = thisPicsDesc;
picTitle.x = mainPicArea.x;
picDesc.x = mainPicArea.x;
picTitle.y = mainPicArea.y + mainPicArea.height + 5;
picDesc.y = mainPicArea.y + mainPicArea.height + 20;
picTitle.width = mainPicArea.width;
picDesc.width = mainPicArea.width;
} //end mainLoaded
} // end loadMainPic

i'm new to as3 so i would love any knowledge on the issue or any direction. Also another thing i would love to know, how to send values from function along with eventListener, that would solve the problem, being able to send variables through the event listener into the function that gets triggered.

Cheers

KayaScript

DiamondDog
06-12-2008, 07:12 AM
You're declaring the variable 'thisThumbsLink' in the 'whenThumbsLoad' function. The trouble is, once that function has finished executing, 'thisThumbsLink' no longer exists, and that's why you can't access it later on.

I've made two very simple changes to your code.

[1] Declare 'thisThumbsLink' at the top of your code, so that it's within the scope of your 'loadMainPic' function
[2] In your 'whenThumbsLoad' function, set the value of 'thisThumbsLink' but don't actually declare it there.

By doing it that way, 'thisThumbsLink' exists before you call 'whenThumbsLoad', it's value is altered within 'whenThumbsLoad' and it still exists after 'whenThumbsLoad' has finished doing its stuff.

Hope that makes sense. Try it and see. Let us know if it doesn't solve the problem.
var mainPicArea:MovieClip = new MovieClip();
mainPicArea.buttonMode = true;
mainPicArea.useHandCursor = true;
addChild(mainPicArea);

var all_thumbs:MovieClip = new MovieClip();
all_thumbs.buttonMode = true;
all_thumbs.useHandCursor = true;
addChild(all_thumbs);

galleryPane.source = all_thumbs;

var fadeIn:Tween;
var fadeOut:Tween;

var urlVar:String = "xml/gallery.xml";

var highResPathList:Array = [];
var lowResPathList:Array = [];
var thumbPathList:Array = [];
var pictureTitleList:Array = [];
var pictureDescList:Array = [];

var totalPics:Number;

var mainPicPlacementX:Number;
var mainPicPlacementY:Number = 100;
// ************************************************** *********
var thisThumbsLink:String; // ******** this line has been added - declare 'thisThumbsLink' here,
// so that it's in the scope of your 'loadMainPic' function
// ************************************************** ****************
//LOAD THUMBNAILS
var thumbLoader:Loader;
// variable for load thumbs function loop
var c:Number = 0;
// spacing between each thumb on x-axis. Number includes thumb width plus spacing
var xSpacing:Number = 105;
// spacing between each thumb on y-axis
var ySpacing:Number = 105;
//how many thumbs are occupying a row
var xs:Number = 0;
//how many thumbs are occupying a column
var ys:Number = 0;
var thumbRowCount:Number;
var thumbColumnCount:Number;
var thumb_orientation:String;

//////////////////////


function loadTheThumbs()
{
thumbLoader = new Loader();
var thumbRequest:URLRequest = new URLRequest(thumbPathList[c]);
thumbLoader.load(thumbRequest);
thumbLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, whenThumbsLoad);

function whenThumbsLoad(event:Event):void
{
var thisMainsLink:String = highResPathList[c];
//**********************************
thisThumbsLink = lowResPathList[c]; // ****** this line has been changed *******
//*******************************************
var thisPicsTitle:String = pictureTitleList[c];
var thisPicsDesc:String = pictureDescList[c];

// (I haven't changed anything else)

kayaScript
06-12-2008, 05:05 PM
Hey thanks so much for your time but unfortunately, i've already tried that and it doesn't work for some reason. I thought it would too. If i declare the variable out side the function, when i click the thumb i want to load, it always load the last picture in the xml, any thumbnail i click. So in other words the variable "c" in...

thisThumbsLink = lowResPathList[c];

equal the highest element in the xml fbecause in that function aswell, it has

c = c + 1;
if (c<totalPics)
{
loadTheThumbs();
}

So it doesn't store the different paths to load when the mouse clicks the thumb.

Only when the var is declared in the loadThumbs() function does it have all paths.

I don't understand how the var stores and when the thumb gets clicked, the event listener can retrieve that right path from the right value for x, when the value for x has reached it's maximum. Where do these variables get stores when it does work, under what instance, target, name??? Does the event listerner capture the value of x when the "addEventListerner" gets triggered everytime it get's looped.

omg, i think i finally figured it out when i was writing this. I think i'm right. I can't declare the 'thisThumbsLink' outside because then it only gets declared once, and i only have one instance of that variable. Whereas if I declare it in the loop function then i have an instance of 'thisThumbsLink' for every time it gets looped, meaning - I don't rewrite over the same variable and create a different variable for each pic path. Add when the event listener gets added, an different listenr get's added for each instance of the var 'thisThumbsLink'.

But how do i do that then, how can i declare var 'thisThumbsLink' each time and connect it to the addEventListener???

kayaScript
06-12-2008, 06:01 PM
i can't figure it out but i figured what i need to be done.

i've taken out function loadMainPic() from loadTheThumbs() function and put it in an .as file called loadMainPic.as and called it in my FLA

include "actionscript/load_low_res.as";

how do i instead of leaving it in the loadTheThumbs() function, just called the .as file in it's place.