A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Help calling nested movie clips from an AS3 class file

  1. #1
    FrazzledNewbie
    Join Date
    Mar 2007
    Location
    Los Angeles
    Posts
    23

    Help calling nested movie clips from an AS3 class file

    Here's what I did:

    1) I created a movieclip (book_mc) on the stage. Inside the book is another movieclip (bookPages_mc). Inside of bookPages_mc is a third movieclip (subNav_mc).

    2) On the root layer I created another movieclip (button).

    3) On "button" I went to Linkage and gave it a base class of my own creation, "MainNav".

    Now in the MainNav class I want to tell the subnavigation movieclip to animate.

    The problem is - I can't figure out how to reference nested movie clips from an external class.

    (For the record - everything has an instance name and has been double checked. The code works from within the FLA but not from an external class.)

    Here is my class code (everything works except for the reference to the nested movieclip):

    Code:
    package {
    	//imports
    	import flash.display.MovieClip;
    	import flash.display.DisplayObject;
    	import flash.events.MouseEvent;
    	import flash.display.SimpleButton;
    	import gs.TweenLite;//Importing TweenLite (make sure "gs" folder is in same folder as this file
    	import fl.motion.easing.*;
    
    	//declare class
    	public class MainNav extends MovieClip {
    
    		public function MainNav() {
    			trace("activate Main Navigation button");
    			this.buttonMode = true;
    			this.addEventListener(MouseEvent.CLICK, handleClick);
    		}//end constructor function
    
    		function handleClick(event:MouseEvent):void {
    			//trace("handleClick");
    			book_mc.bookPages_mc.subNav_mc.gotoAndPlay("slide");
    		}//end handleClick method
    
    	}//end class
    //end package
    }

    Please help! And please consider, when replying, that I pretty much only learned how to use classes YESTERDAY so I'm still a noob at this. Thanks in advance!

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Your code is referring to book_mc as if it were a property of the MainNav instance. It is not. book_mc is a property of the main movieclip on which the button lives. The quickest fix is to reach up to the parent to get a reference to book_mc that you can use:
    Code:
    function handleClick(event:MouseEvent):void {
    	//trace("handleClick");
            var book_mc:MovieClip = MovieClip(parent).book_mc;
    	book_mc.bookPages_mc.subNav_mc.gotoAndPlay("slide");
    }//end handleClick method
    This is not very good object oriented style, since MainNav will now only work when the parent has a book_mc property. They are tightly coupled. For smaller one-off projects, this isn't a big deal. But in order to make something re-useable, you should attempt to abstract out all the code that is specific to the particular structure. You could do this by having a variable in MainNav to keep track of what instance to manipulate, or even what function to call, then set that variable from outside:

    Code:
    package {
    	//imports
    	import flash.display.MovieClip;
    	import flash.display.DisplayObject;
    	import flash.events.MouseEvent;
    	import flash.display.SimpleButton;
    	import gs.TweenLite;//Importing TweenLite (make sure "gs" folder is in same folder as this file
    	import fl.motion.easing.*;
    
    	//declare class
    	public class MainNav extends MovieClip {
    
                    public var clickFunction:Function;
    
    		public function MainNav() {
    			trace("activate Main Navigation button");
    			this.buttonMode = true;
                            clickFunction = doNothing;
    			this.addEventListener(MouseEvent.CLICK, handleClick);
    		}//end constructor function
    
    		private function handleClick(event:MouseEvent):void {
                       clickFunction(event);
    		}//end handleClick method
    
                    private function doNothing(event:Event):void{
                    }
    
    	}//end class
    //end package
    }
    Now by setting button's clickFunction property you can specify the particular function to use outside of MainNav.

    In your main swf frame script:
    Code:
    function slideSubNav(event:Event):void{
    	book_mc.bookPages_mc.subNav_mc.gotoAndPlay("slide");
    }
    button.clickFunction = slideSubNav;

  3. #3
    FrazzledNewbie
    Join Date
    Mar 2007
    Location
    Los Angeles
    Posts
    23
    I almost 100% understand that! Thank you very much. I didn't even know that I could declare a variable typed as a function!

    In another forum I got the response to use "MovieClip(this.parent)" to get to the parent of the button (and then more .parents as needed). Your way sounds simpler.

    I also appreciate the description of how to make the code re-usable!

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