A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Calling function in root movieclip

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    28

    Calling function in root movieclip

    Hi, I've got a movieclip on the root of the stage called "bgImg", in that clip I've made this function:
    Actionscript Code:
    function loadNew(e:Event) {
        trace("working");
    }

    When you click a button in the nav it loads a movieclip to the stage from the library, inside this movieclip I have a row of buttons, when you click one of these I want to call the function in the movieclip on the root (ie. the function inside "bgImg"
    I tried this:

    Actionscript Code:
    var _root = root;
    empty.addEventListener(MouseEvent.CLICK, loadNewBG);

    function loadNewBG(e:MouseEvent){
        MovieClip(_root.bgImg).loadNew();
    }

    I also tried this:

    Actionscript Code:
    var _root = root;
    empty.addEventListener(MouseEvent.CLICK, loadNewBG);

    function loadNewBG(e:MouseEvent){
        _root.bgImg.loadNew();
    }

    but both throw up errors - any help please?
    Thanks

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Do you think error messages are meaningless? No? Then why wouldn't you tell us what errors you got?

    The error you got was probably something like "possibly undefined property bgImg". The reason it happens is because root is typed as DisplayObject, which does not define a bgImg property. To get the compiler to know that there is a bgImg property, you need to cast the root object to some class which either does declare bgImg, or is dynamic. MovieClip is dynamic.

    There's no need to create a _root variable (and if there were, you should declare a type for it).
    Code:
    function loadNewBG(e:MouseEvent):void{
        MovieClip(root).bgImg.loadNew();
    }
    Doing it this way (having the child movie access methods and properties in the parent) is not recommended. Instead, you could dispatch an event from your child instance and listen for it in the parent/root. This would decouple the child from having to know the structure of the rest of the movie.

    This is THE most common question we get on this forum.

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    28
    no need to get ****ty with me :|
    thanks for the help anyway. I can't remember what errors it was throwing up and don't have the files with me at the moment, hence why I didn't post them. I'll try this later

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    no need to get ****ty with me :|
    You're right. I'm not having a great day. Good luck with your project.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center