A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: MC inside MC

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    11

    MC inside MC

    Guys, I have an MC instantiated as "menu", and inside "menu" there are MCs instantiated with the names of their pages.

    I'm trying to define a function for each of these MCs on the main timeline and what I did was as follows (party is the name of one of those MCs that are inside the MC "menu". The third "party" is this code is a scene):



    var party = menu
    party.addEventListener(MouseEvent.CLICK, goto);
    function goto (e:MouseEvent):void
    {
    stage.removeEventListener(Event.RESIZE, sizeListener);
    stage.removeEventListener(Event.RESIZE, onStageResize);
    removeEventListener(Event.ENTER_FRAME, onFrame);
    gotoAndStop(1, "party");

    }

    The problem is that this way anywhere I click the MC "menu", go to the same location.
    How do I define what I call the variable "party" that is within menu?

  2. #2
    Member
    Join Date
    Mar 2003
    Location
    The Netherlands
    Posts
    61
    This line:

    var party = menu

    will assign the 'menu' movieclip to the variable party as another reference to it, thus the mouse listener is added to the entire 'menu' movieclip.

    If party is a movieclip within 'menu', make it accessible using a public property or getter function ( or if you're using the Flash library for creating the menu graphics, make sure the "party" movieclip has a name identifier by entering it in it's properties panel ) using the name 'party'.

    That way you can write:

    menu.party.addEventListener( MouseEvent.CLICK, goto );

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    I try this, but Flash show the following error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at index_fla::MainTimeline/frame2()

  4. #4
    Member
    Join Date
    Mar 2003
    Location
    The Netherlands
    Posts
    61
    how is the "menu" movieclip made ? is it built from a class, or is it an asset in your library ? the above code you posted shows that the name 'party' isn't registered within the menu movieclip. Perhaps you can ZIP part of your project ? ( or show the contents of the menu class if it's class based )

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    menu is a mc item in my libray. I put it in stage a called "menu".

    inside menu, there are two MCs, one of then called party

    Here is the code inside menu mc:

    Actionscript Code:
    // Import classes needed for tweening  
    import fl.transitions.Tween;  
    import fl.transitions.easing.*;  
    import flash.events.MouseEvent;

    // Define the tween variable for the bar's "x" position  
    var barX:Tween;  
    // Define the tween variable for the bar's fade-in  
    var barAlphaIn:Tween;  
    // Define the tween variable for the bar's fade-out  
    var barAlphaOut:Tween;  
    // Define the tween variable for the bars width  
    var barWidth:Tween;

    // Add an event listener for the roll over of button  
    party.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);  
    // Add an event listener for the roll out of button  
    party.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
    // Add an event listener for the roll out of button  

    // Add an event listener for the roll over of button  
    casual.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);  
    // Add an event listener for the roll out of button  
    casual.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);


    // Execute function for rollover event  
    function rollOverHandler (e:MouseEvent):void  
    {  
    //  Adjust the bar's "x" position to the same as the button. Parameters are:  
    //  object to tween (i.e. to animate)  
    //  property of the object to be tweened  
    //  type of easing (type of motion) to use  
    //  initial value (i.e. value to tween the property from)  
    //  end value (i.e. value the property will end up with when the tween's finished)  
    //  duration of tween  
    //  whether to measure the duration in seconds (true) or frames (false)  
    barX = new Tween(bar, "x", Back.easeOut, bar.x, e.target.x, 1, true);  
    // Tween the bar's alpha to make it fade in  
    barAlphaIn = new Tween(bar, "alpha", Regular.easeOut, bar.alpha, 1, 1, true);  
    // Tween the bar's width to become the same as the button's  
    barWidth = new Tween(bar, "width", Regular.easeOut, bar.width, e.target.width, 1, true);  
    }  
       
    // Execute function for rollout event  
    function rollOutHandler (e:MouseEvent):void  
    {  
    // Tween the bar's alpha to make it fade out  
    barAlphaOut = new Tween(bar, "alpha", Regular.easeOut, bar.alpha, 0, 1, true);  
    }

    Now, here is the code in main timeline:

    Actionscript Code:
    menu.party.addEventListener(MouseEvent.CLICK, goto);
    function goto (e:MouseEvent):void
    {
        stage.removeEventListener(Event.RESIZE, sizeListener);
        stage.removeEventListener(Event.RESIZE, onStageResize);
        removeEventListener(Event.ENTER_FRAME, onFrame);
        gotoAndStop(1, "party");
       
    }

  6. #6
    Member
    Join Date
    Mar 2003
    Location
    The Netherlands
    Posts
    61
    Seeing as you already have rollover / rollout listeners for "party" and "casual" ( these work with the current library menu, right ? ), I would add the click listeners for these here as well, something like this:

    Actionscript Code:
    // Import classes needed for tweening
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    import flash.events.MouseEvent;

    // Define two events
    public var PARTY :String = "Menu::PARTY";
    public var CASUAL:String = "Menu::CASUAL";

    // Define the tween variable for the bar's "x" position
    var barX:Tween;
    // Define the tween variable for the bar's fade-in
    var barAlphaIn:Tween;
    // Define the tween variable for the bar's fade-out
    var barAlphaOut:Tween;
    // Define the tween variable for the bars width
    var barWidth:Tween;

    // Add an event listener for the roll over of button
    party.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
    // Add an event listener for the roll out of button
    party.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
    // Add an event listener for the roll out of button
    party.addEventListener(MouseEvent.CLICK, clickHandler);
    // Add an event listener for the click of button

    // Add an event listener for the roll over of button
    casual.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
    // Add an event listener for the roll out of button
    casual.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
    // Add an event listener for the click of button
    casual.addEventListener(MouseEvent.CLICK, clickHandler);

    // Execute function for rollover event
    function rollOverHandler (e:MouseEvent):void
    {
    //  Adjust the bar's "x" position to the same as the button. Parameters are:
    //  object to tween (i.e. to animate)
    //  property of the object to be tweened
    //  type of easing (type of motion) to use
    //  initial value (i.e. value to tween the property from)
    //  end value (i.e. value the property will end up with when the tween's finished)
    //  duration of tween
    //  whether to measure the duration in seconds (true) or frames (false)
    barX = new Tween(bar, "x", Back.easeOut, bar.x, e.target.x, 1, true);
    // Tween the bar's alpha to make it fade in
    barAlphaIn = new Tween(bar, "alpha", Regular.easeOut, bar.alpha, 1, 1, true);
    // Tween the bar's width to become the same as the button's
    barWidth = new Tween(bar, "width", Regular.easeOut, bar.width, e.target.width, 1, true);
    }
       
    // Execute function for rollout event
    function rollOutHandler (e:MouseEvent):void
    {
    // Tween the bar's alpha to make it fade out
    barAlphaOut = new Tween(bar, "alpha", Regular.easeOut, bar.alpha, 0, 1, true);
    }

    function clickHandler( e:MouseEvent ):void
    {
         var eType:String;
         // detect which if the two buttons was clicked
         // so we can format the event accordingly
         switch( e.target )
         {
              case party:
                   eType = PARTY;
              break;
              case casual:
                   eType = CASUAL;
              break;
         }
         // dispatch this event to the listening main class
         dispatchEvent( new Event( eType ));
    }

    use this code in the main class:

    Actionscript Code:
    // add event listeners to the menu for the individual button click events
    menu.addEventListener( menu.PARTY, goto);
    menu.addEventListener( menu.CASUAL, goto);
    function goto (e:Event):void
    {
        // which button was clicked ?
        if ( e.type == menu.PARTY )
        {
             // it was party, do this:
             stage.removeEventListener(Event.RESIZE, sizeListener);
             stage.removeEventListener(Event.RESIZE, onStageResize);
             removeEventListener(Event.ENTER_FRAME, onFrame);
             gotoAndStop(1, "party");
        }
    }

  7. #7
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    I change the code and erase (public) in lines

    Actionscript Code:
    var PARTY :String = "Menu::PARTY";
    var CASUAL :String = "Menu::CASUAL";

    because Flash say that "public" is use only in package.

    But appear the following error:

    TypeError: Error #2007: Parameter type must be non-null.
    at flash.events::EventDispatcher/addEventListener()
    at index_fla::MainTimeline/frame2()

  8. #8
    Member
    Join Date
    Mar 2003
    Location
    The Netherlands
    Posts
    61
    Aaah my bad, I'm used to coding outside the timeline, seems like the properties of the code in the menu MC aren't made public, the error is the exact same as the one you got by previously trying to access "menu.party".

    You could try this, altough I don't think it's as pretty:

    Actionscript Code:
    // add event listeners to the menu for the individual button click events
    menu.addEventListener( "Menu::PARTY", goto);
    menu.addEventListener( "Menu::CASUAL", goto);
    function goto (e:Event):void
    {
        // which button was clicked ?
        if ( e.type == "Menu::PARTY" )
        {
             // it was party, do this:
             stage.removeEventListener(Event.RESIZE, sizeListener);
             stage.removeEventListener(Event.RESIZE, onStageResize);
             removeEventListener(Event.ENTER_FRAME, onFrame);
             gotoAndStop(1, "party");
        }
    }

  9. #9
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Now, there is no error message. But nothing happen when I click in "party". The function goto don't send me to the another scene.

    I put trace("party") in button, but nothing appear.

  10. #10
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Now its works fine. But I have more than 2 buttons. What can I do in this function?

    Actionscript Code:
    function goto (e:Event):void
    {
        // which button was clicked ?
        if ( e.type == "Menu::PARTY" )
        {
             // it was party, do this:
             
             stage.removeEventListener(Event.RESIZE, sizeListener);
             stage.removeEventListener(Event.RESIZE, onStageResize);
             removeEventListener(Event.ENTER_FRAME, onFrame);
             gotoAndStop(1, "party");
        }
       
        else ( e.type == "Menu::CASUAL" )
        {
             // it was party, do this:
             
             stage.removeEventListener(Event.RESIZE, sizeListener);
             stage.removeEventListener(Event.RESIZE, onStageResize);
             removeEventListener(Event.ENTER_FRAME, onFrame);
             gotoAndStop(1, "casual");
        }
    }

  11. #11
    Member
    Join Date
    Mar 2003
    Location
    The Netherlands
    Posts
    61
    That's a hard question as a solid navigation base is built around a few objects / classes. Ideally you'd map a button to a certain behaviour / page and make your functions fit dynamically around these. You could create a Event type and have it contain a property telling you where to navigate to, for instance.

    Am I correct in thinkin you ALWAYS want to remove the sizelistener after clicking a menu button ? If so, then we don't repeat it.

    Actionscript Code:
    function goto (e:Event):void
    {
        switch( e.type )
        {
              case "Menu::PARTY":
                          gotoAndPlay( 1, "party" );
                    break;
              case "Menu::CASUAL":
                          gotoAndPlay( 1, "casual" );
                    break;
        }
        stage.removeEventListener(Event.RESIZE, sizeListener);
        stage.removeEventListener(Event.RESIZE, onStageResize);
        removeEventListener(Event.ENTER_FRAME, onFrame);
    }

    Where in the switch statement you look as to what event / button was clicked, and there you set the scene you want to switch to. Since the listeners have to removed anyways, we add the removal at the bottom.
    Last edited by igorski; 02-16-2011 at 12:10 PM.

  12. #12
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Great idea.

    I try using "else if" but your code is most economic

    Just one more doubt:

    Each scene have a different name for the same functions. Example: sizeListener_party, sizeListener_casual...

    I had to do this because otherwise the code would not work Fullscreen Background.

    So, when I click, for example, in party, I had to remove all others listeners.

    Is there an easier way?

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