AS3: PullOutMenu

View Example
View Documentation
Download Class & Example Files

The PullOutMenu class creates a menu like the classic dropdown menu with the options to direct it up, down, left, or right depending on values set. It also has a bunch of other customizable options.

This is a direct port of my AS2 PullOutMenu. The documentation is pretty much the same for both versions except that in the AS3 version the $root parameter was renamed to $timeline. If you take a look at the source you'll see I threw the rollover/rollout/release code for the option items directly on the timeline just for the sake of brevity. I don't normally condone this but it was just to give you an idea of how it could be done/look.

Actionscript:
  1. /**
  2. * Creates a menu like the classic dropdown menu with the options to direct it up, down, left, or right depending on values set.
  3. *
  4. * @usage
  5. * <code>
  6. * <pre>
  7. import com.reintroducing.ui.PullOutMenu;
  8. import fl.transitions.easing.*;
  9. var optionalObj:Object = new Object({openSpeed: .25, closeSpeed: .25, axis: "y", easeFunc: Regular.easeOut, eventType: "rollover"});
  10. var pomDown:PullOutMenu = new PullOutMenu(this, downMenu_mc.button_mc, downMenu_mc.options_mc, downMenu_mc.mask_mc, -129, 26, optionalObj);
  11. * </pre>
  12. * </code>
  13. *
  14. * @author Matt Przybylski [http://www.reintroducing.com]
  15. * @version 1.0
  16. */
  17.  
  18. package com.reintroducing.ui
  19. {
  20.     import flash.display.MovieClip;
  21.     import flash.events.MouseEvent;
  22.     import flash.utils.clearInterval;
  23.     import flash.utils.setInterval;
  24.    
  25.     import fl.transitions.Tween;
  26.     import fl.transitions.easing.Regular;   
  27.  
  28.     public class PullOutMenu
  29.     {
  30. //- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
  31.  
  32.         private var _checkInterval:Number;
  33.         private var _timeline:Object;
  34.         private var _hitButton:MovieClip;
  35.         private var _menu:MovieClip;
  36.         private var _mask:MovieClip;
  37.         private var _startHeight:Number;
  38.         private var _endHeight:Number;
  39.         private var _tween:Tween;
  40.         private var _openSpeed:Number;
  41.         private var _closeSpeed:Number;
  42.         private var _axis:String;
  43.         private var _easeFunc:Function;
  44.         private var _eventType:String;
  45.        
  46. //- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
  47.        
  48.        
  49.        
  50. //- CONSTRUCTOR -------------------------------------------------------------------------------------------
  51.    
  52.         /**
  53.          * Creates a new instance of the PullOutMenu class.
  54.          *
  55.          * <p>
  56.          * The $optionalParams parameter takes in a couple of values.
  57.          * <ul>
  58.          * <li>openSpeed: An integer representing the speed you want your menu to open, in seconds (default: .25)</li>
  59.          * <li>closeSpeed: An integer representing the speed you want your menu to close, in seconds (default: .25)</li>
  60.          * <li>axis: A string representing the axis that the menu opens on, "x" or "y" (default: "y")</li>
  61.          * <li>easeFunc: A function representing the ease type you'd like to use, as per the Tween class easing functions (default: Regular.easeOut)</li>
  62.          * <li>eventType: A string representing when to trigger the showing of the menu, "rollover", "press", or "release" (default: "rollover")</li>
  63.          * </ul>
  64.          * </p>
  65.          *
  66.          * @usage <pre><code>var pomDown:PullOutMenu = new PullOutMenu($root, $hitButton, $menu, $mask, $startHeight, $endHeight, $optionalParams);</code></pre>
  67.          *
  68.          * @param $timeline A reference to the timeline that the menu is contained in
  69.          * @param $hitButton The MovieClip used as the button you initially roll over or press to see the menu
  70.          * @param $menu The MovieClip where the menu items are housed
  71.          * @param $mask The MovieClip acting as the mask for the menu
  72.          * @param $startHeight A number that represents the initial position of the menu (its resting state)
  73.          * @param $endHeight A number that represents the final position of the menu (after you have opened it)
  74.          * @param $optionalParams See above
  75.          */
  76.        
  77.         public function PullOutMenu($timeline:Object, $hitButton:MovieClip, $menu:MovieClip, $mask:MovieClip, $startHeight:Number, $endHeight:Number, $optionalParams:Object):void
  78.         {
  79.             this._timeline = $timeline;
  80.             this._hitButton = $hitButton;
  81.             this._menu = $menu;
  82.             this._mask = $mask;
  83.             this._startHeight = $startHeight;
  84.             this._endHeight = $endHeight;
  85.            
  86.             this._openSpeed = ($optionalParams.openSpeed == undefined) ? .25 : $optionalParams.openSpeed;
  87.             this._closeSpeed = ($optionalParams.closeSpeed == undefined) ? .25 : $optionalParams.closeSpeed;
  88.             this._axis = ($optionalParams.axis == undefined) ? "y" : $optionalParams.axis;
  89.             this._easeFunc = ($optionalParams.easeFunc == undefined) ? Regular.easeOut : $optionalParams.easeFunc;
  90.             this._eventType = ($optionalParams.eventType == undefined) ? "rollover" : $optionalParams.eventType;
  91.            
  92.             this.init();
  93.         }
  94.        
  95. //- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
  96.        
  97.         private function openMenu($evt:MouseEvent):void
  98.         {
  99.             this._tween = new Tween(this._menu, this._axis, this._easeFunc, this._menu[this._axis], this._endHeight, this._openSpeed, true);
  100.             this._checkInterval = setInterval(checkOpen, 100);
  101.         }
  102.        
  103.         private function checkOpen():void
  104.         {
  105.             var mx:Number = _timeline.mouseX;
  106.             var my:Number = _timeline.mouseY;
  107.             var isHitting:Boolean = this._mask.hitTestPoint(mx, my);
  108.            
  109.             if (!isHitting) this.closeMenu();
  110.         }
  111.        
  112.         private function init():void
  113.         {
  114.             switch (this._eventType)
  115.             {
  116.                 case "rollover":
  117.                     this._hitButton.addEventListener(MouseEvent.MOUSE_OVER, openMenu);
  118.                     break;
  119.                
  120.                 case "press":
  121.                     this._hitButton.addEventListener(MouseEvent.MOUSE_DOWN, openMenu);
  122.                     break;
  123.                
  124.                 case "release":
  125.                     this._hitButton.addEventListener(MouseEvent.MOUSE_UP, openMenu);
  126.                     break;
  127.             }
  128.         }
  129.        
  130. //- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
  131.    
  132.         /**
  133.          * Closes the menu and puts it in the initial position. Used also when a button inside the menu is pressed and the need to close the menu right then arises.
  134.          *
  135.          * @usage <pre><code>pomDown.closeMenu();</code></pre>
  136.          *
  137.          * @return Nothing
  138.          */
  139.        
  140.         public function closeMenu():void
  141.         {
  142.             this._tween = new Tween(this._menu, this._axis, this._easeFunc, this._menu[this._axis], this._startHeight, this._closeSpeed, true);
  143.             clearInterval(this._checkInterval);
  144.         }
  145.    
  146. //- EVENT HANDLERS ----------------------------------------------------------------------------------------
  147.    
  148.        
  149.    
  150. //- GETTERS & SETTERS -------------------------------------------------------------------------------------
  151.    
  152.        
  153.    
  154. //- HELPERS -----------------------------------------------------------------------------------------------
  155.    
  156.         public function toString():String
  157.         {
  158.             return "com.reintroducing.ui.PullOutMenu";
  159.         }
  160.    
  161. //- END CLASS ---------------------------------------------------------------------------------------------
  162.     }
  163. }

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

2 Comments

i have tried some variations of the code to adjust it to match my movie but whit no succes :( For example when i try to load the .swf via URLloader and Loader into another movie , the menu gets fucked up , probably couse of it timeline specifics... i have tried to set parent on the timeline object, and in the .as file itself but again no succes .... can somebody tell me how can i nest this .swf into another and still get it to work ?

Vlad, it's probably a scoping issue somewhere in your code. Unfortunately nobody will be able to help you without seeing the code.

My suggestion would be to isolate the problem into a file all by itself (or two if you have to since youre loading it externally) and trace out everything you can to track where the problem is. if you've still got nothing, you can send me the isolated file and i can take a look at it as a last resort but please ONLY do this if you have done all of the above and still can't solve it as I'm very busy right now.

Leave a comment

(required)

(required)