0

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());
1
  • 1
    Do not nest functions. Commented Mar 19, 2014 at 2:08

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is funny becuse this isent really about nested or not nested, it's How can I get the math done out side of this function, I'm looking for parent_mc.width but it only give it to me here.
Well, the title of the question suggests that the question IS about nested functions and access to them. And there is an answer - bring the desired function out as a class member or if you write in a frame just get it out from the parent function.
but thats the original problem I'm having i can get the number I need "I want to pass the "galleryArr.length * 400;"out side the function onGalleryLoaded" out side of onGalleryLoaded its always 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.