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.
-
/**
-
* Creates a menu like the classic dropdown menu with the options to direct it up, down, left, or right depending on values set.
-
*
-
* @usage
-
* <code>
-
* <pre>
-
import com.reintroducing.ui.PullOutMenu;
-
import fl.transitions.easing.*;
-
var optionalObj:Object = new Object({openSpeed: .25, closeSpeed: .25, axis: "y", easeFunc: Regular.easeOut, eventType: "rollover"});
-
var pomDown:PullOutMenu = new PullOutMenu(this, downMenu_mc.button_mc, downMenu_mc.options_mc, downMenu_mc.mask_mc, -129, 26, optionalObj);
-
* </pre>
-
* </code>
-
*
-
* @author Matt Przybylski [http://www.reintroducing.com]
-
* @version 1.0
-
*/
-
-
package com.reintroducing.ui
-
{
-
import flash.display.MovieClip;
-
import flash.events.MouseEvent;
-
import flash.utils.clearInterval;
-
import flash.utils.setInterval;
-
-
import fl.transitions.Tween;
-
import fl.transitions.easing.Regular;
-
-
public class PullOutMenu
-
{
-
//- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
-
-
private var _checkInterval:Number;
-
private var _timeline:Object;
-
private var _hitButton:MovieClip;
-
private var _menu:MovieClip;
-
private var _mask:MovieClip;
-
private var _startHeight:Number;
-
private var _endHeight:Number;
-
private var _tween:Tween;
-
private var _openSpeed:Number;
-
private var _closeSpeed:Number;
-
private var _axis:String;
-
private var _easeFunc:Function;
-
private var _eventType:String;
-
-
//- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
-
-
-
-
//- CONSTRUCTOR -------------------------------------------------------------------------------------------
-
-
/**
-
* Creates a new instance of the PullOutMenu class.
-
*
-
* <p>
-
* The $optionalParams parameter takes in a couple of values.
-
* <ul>
-
* <li>openSpeed: An integer representing the speed you want your menu to open, in seconds (default: .25)</li>
-
* <li>closeSpeed: An integer representing the speed you want your menu to close, in seconds (default: .25)</li>
-
* <li>axis: A string representing the axis that the menu opens on, "x" or "y" (default: "y")</li>
-
* <li>easeFunc: A function representing the ease type you'd like to use, as per the Tween class easing functions (default: Regular.easeOut)</li>
-
* <li>eventType: A string representing when to trigger the showing of the menu, "rollover", "press", or "release" (default: "rollover")</li>
-
* </ul>
-
* </p>
-
*
-
* @usage <pre><code>var pomDown:PullOutMenu = new PullOutMenu($root, $hitButton, $menu, $mask, $startHeight, $endHeight, $optionalParams);</code></pre>
-
*
-
* @param $timeline A reference to the timeline that the menu is contained in
-
* @param $hitButton The MovieClip used as the button you initially roll over or press to see the menu
-
* @param $menu The MovieClip where the menu items are housed
-
* @param $mask The MovieClip acting as the mask for the menu
-
* @param $startHeight A number that represents the initial position of the menu (its resting state)
-
* @param $endHeight A number that represents the final position of the menu (after you have opened it)
-
* @param $optionalParams See above
-
*/
-
-
public function PullOutMenu($timeline:Object, $hitButton:MovieClip, $menu:MovieClip, $mask:MovieClip, $startHeight:Number, $endHeight:Number, $optionalParams:Object):void
-
{
-
this._timeline = $timeline;
-
this._hitButton = $hitButton;
-
this._menu = $menu;
-
this._mask = $mask;
-
this._startHeight = $startHeight;
-
this._endHeight = $endHeight;
-
-
this._openSpeed = ($optionalParams.openSpeed == undefined) ? .25 : $optionalParams.openSpeed;
-
this._closeSpeed = ($optionalParams.closeSpeed == undefined) ? .25 : $optionalParams.closeSpeed;
-
this._axis = ($optionalParams.axis == undefined) ? "y" : $optionalParams.axis;
-
this._easeFunc = ($optionalParams.easeFunc == undefined) ? Regular.easeOut : $optionalParams.easeFunc;
-
this._eventType = ($optionalParams.eventType == undefined) ? "rollover" : $optionalParams.eventType;
-
-
this.init();
-
}
-
-
//- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
-
-
private function openMenu($evt:MouseEvent):void
-
{
-
this._tween = new Tween(this._menu, this._axis, this._easeFunc, this._menu[this._axis], this._endHeight, this._openSpeed, true);
-
this._checkInterval = setInterval(checkOpen, 100);
-
}
-
-
private function checkOpen():void
-
{
-
var mx:Number = _timeline.mouseX;
-
var my:Number = _timeline.mouseY;
-
var isHitting:Boolean = this._mask.hitTestPoint(mx, my);
-
-
if (!isHitting) this.closeMenu();
-
}
-
-
private function init():void
-
{
-
switch (this._eventType)
-
{
-
case "rollover":
-
this._hitButton.addEventListener(MouseEvent.MOUSE_OVER, openMenu);
-
break;
-
-
case "press":
-
this._hitButton.addEventListener(MouseEvent.MOUSE_DOWN, openMenu);
-
break;
-
-
case "release":
-
this._hitButton.addEventListener(MouseEvent.MOUSE_UP, openMenu);
-
break;
-
}
-
}
-
-
//- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
-
-
/**
-
* 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.
-
*
-
* @usage <pre><code>pomDown.closeMenu();</code></pre>
-
*
-
* @return Nothing
-
*/
-
-
public function closeMenu():void
-
{
-
this._tween = new Tween(this._menu, this._axis, this._easeFunc, this._menu[this._axis], this._startHeight, this._closeSpeed, true);
-
clearInterval(this._checkInterval);
-
}
-
-
//- EVENT HANDLERS ----------------------------------------------------------------------------------------
-
-
-
-
//- GETTERS & SETTERS -------------------------------------------------------------------------------------
-
-
-
-
//- HELPERS -----------------------------------------------------------------------------------------------
-
-
public function toString():String
-
{
-
return "com.reintroducing.ui.PullOutMenu";
-
}
-
-
//- END CLASS ---------------------------------------------------------------------------------------------
-
}
-
}
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.
3 Comments
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.
If I am using external xml file's data on text_mc, how should I rewrite your array for "option"? and how can I add more items on the menu and length of the mask with actionscript, so it will change depending on the xml.data? Please help! Thank you so much.













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 ?