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
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