Reputation: 73
I want to pass the "galleryArr.length * 400;"out side the function onGalleryLoaded so I made the function "initmyWidth" and called it from outside the functions with "trace(initmyWidth());" but it gets an error "Call to a possibly undefined method initmyWidth" is there a way to pass it one more level outside the functions?
function onGalleryLoaded(e:Event):void
{
var imageX:int = 0;
var imageY:int = 0;
for each (var image:Sprite in galleryArr)
{
var my_mc:MovieClip = new MovieClip();
my_mc.addChild(image);
parent_mc.addChild(my_mc);
my_mc.width = 400;
my_mc.height = 400;
my_mc.x -= parent_mc.width;
my_mc.y = imageY;
function initmyWidth():Number
{
var myWidth:Number = galleryArr.length * 400;
return myWidth;
}
imageX -= my_mc.width + PADDING_X;
if ( (imageX+my_mc.width) > stage.stageWidth )
{
imageX = parent_mc.width;
imageY = 0;
}
}
}
trace(initmyWidth());
Upvotes: 0
Views: 365
Reputation: 3151
Nested functions are anonymous, they cannot be accessed outside of the the parents functions scope. So no, you cannot call it from outside.
But you can simply put it as a class member and be able to use it without any difference.
Nested methods aren't evil, but they are mostly designed for non-stictly typed languages (e.g. JavaScript) and there is no need to use that in AS3.
The way to use nested functions if you want to dynamically create a function, e.g var myFunc = function(){}; and the be able to delete it by setting it to null. But in AS3 such functions have the longest call time and can be real performance hitters, so a rule of thumb - don't use nested functions unless there's a really good reason to do it.
Upvotes: 1