AS3 Drop Down Menu Class

November 28th 2008 in AS3 Drop Down Menu Class, Actionscript Classes, Laboratory

I briefly Googled for a drop down class to no avail. I say briefly as I only went one page deep. I’m sure there are others out there, but I instead got impatient as usual and just wrote my own. I thought I’d share it for other impatient people’s benefit. Without further ado here’s the AS3 Drop Down Class code. It’s a work in progress and most likely will be updated when I give it any more thought.

You can see the drop down in use on my homepage.

Edit [ 12.16.08 ] : added directional code.


//usage
// array for drop
var dropOtherArray:Array = new Array()
dropOtherArray.push( {title:"iWork", name:"folio"} )
dropOtherArray.push( {title:"iExperiment", name:"lab"} )
dropOtherArray.push( {title:"iWrite", name:"blog"} )
dropOtherArray.push( {title:"iPhotograph", name:"photo"} )
dropOtherArray.push( {title:"iFlickr", name:"flickr"} )
dropOtherArray.push( {title:"iRecord", name:"vimeo"} )
dropOtherArray.push( {title:"contactMe", name:"contact"} )

fmat.color = 0xffffff
fmat.font = font.fontName
fmat.size = 11

addChild( _dropOther = new DropDown( 180, 25, "iLiveElsewhere:", fmat, 0x000033, dropOtherArray, "down", other ) )

package com.teso.ui
{
	import com.gskinner.motion.*
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;
	import flash.text.*;
	import fl.transitions.*;
	import fl.transitions.easing.*;

	public class DropDown extends Sprite
	{
		private var _items:Array = new Array()
		private var _overC:uint;
		private var _backC:uint;
		private var _w:Number;
		private var _h:Number;
		private var _timer:Timer;
		private var _open:Boolean = false;
		private var _defaultText:TextField;
		private var _title:String;
		private var _direction:String;
		private var _fmt:TextFormat;

		public function DropDown( w:Number, h:Number, title:String, fmt:TextFormat, colorBack:uint, itemArray:Array, direction:String, callback )
		{
			// timer
			_timer = new Timer( 300 );
			_timer.addEventListener( TimerEvent.TIMER, closeDrop )

			// vars
			_w = w
			_h = h
			_backC = colorBack
			_items = itemArray
			_title = title
			_direction = direction
			_fmt = fmt

			// create a back for the holder
			var holder:MovieClip = new MovieClip();
			holder.name = "holder"
			holder.graphics.beginFill( _backC, 1 );
			holder.graphics.drawRoundRect( 0, 0, _w, _h, 2, 2 )
			holder.graphics.endFill()

			// add the drop
			addChild( holder )

			// set listeners
			holder.buttonMode = true;
			holder.addEventListener( MouseEvent.MOUSE_OVER, openDrop )
			holder.addEventListener( MouseEvent.MOUSE_OVER, cancelClose )
			holder.addEventListener( MouseEvent.MOUSE_OUT, startClose )

			// create a text field
			var t:TextField = new TextField()
			t.name = "holderText"
			t.selectable = false;
			t.autoSize = TextFieldAutoSize.LEFT;
			t.htmlText = title
			t.setTextFormat( fmt )
			t.y = ( holder.height/2 ) - ( t.height/2 )

			_defaultText = t

			// add the text
			holder.addChild( t )

			// create children
			for( var i=0; i<_items.length; i++ )
			{
				// create a back
				var back:MovieClip = new MovieClip()
				back.name = _items[i].name;
				back.graphics.beginFill( _backC, 1 )
				back.graphics.drawRoundRect( 0, 0, _w, _h, 10, 10 )
				back.graphics.endFill()

				// create a text field
				t = new TextField();
				t.name = "t"
				t.x = 5
				t.selectable = false;
				t.autoSize = TextFieldAutoSize.LEFT;
				t.htmlText = _items[i].title;
				t.setTextFormat( fmt )
				t.y = ( back.height/2 ) - ( t.height/2 )

				if( _items[i].d )
				{
					_defaultText.htmlText = _title+" "+_items[i].title
					_defaultText.setTextFormat( _fmt )
				}

				// make them invisible for now
				back.visible = false;

				// set a listener
				back.buttonMode = true;
				back.addEventListener( MouseEvent.CLICK, closeDrop )
				back.addEventListener( MouseEvent.CLICK, setDefaultText )
				back.addEventListener( MouseEvent.CLICK, callback )
				back.addEventListener( MouseEvent.MOUSE_OUT, startClose )
				back.addEventListener( MouseEvent.MOUSE_OVER, cancelClose )

				// add the text
				back.addChild( t )

				// add it to the holder
				addChildAt( back, 0 )

				_items[i].mc = back
			}
		}

		private function openDrop( e:Event )
		{
			if( !_open )
			{

				for( var i=0; i<_items.length; i++ )
				{
					// set a var
					var item:DisplayObject = _items[i].mc

					// set the items alpha to zero
					item.alpha = 0;

					// make the item visible
					item.visible = true

					// fade it in
					var tweenIn:GTween;

					if( _direction == "down" )
					{
						tweenIn = new GTween( item, .3, {y:_h + ( _h * i ),  alpha:1} )
					}
					else
					{
						tweenIn = new GTween( item, .3, {y:-_h - ( _h * i ),  alpha:1} )
					}
					tweenIn.ease = Regular.easeOut
				}
			}
			_open = true;

		}
		private function cancelClose( e:Event )
		{
			if( e.currentTarget.name != "holder" )
			{
				e.currentTarget.alpha = .8
			}
			_timer.stop()
		}
		private function startClose( e:Event )
		{
			e.currentTarget.alpha = 1
			_timer.start()
		}
		private function setDefaultText( e:Event )
		{
			_defaultText.htmlText = _title+" "+e.currentTarget.getChildByName( "t" ).text
			_defaultText.setTextFormat( _fmt )
		}
		private function closeDrop( e:Event )
		{
			closeIt()
		}
		private function closeIt()
		{
			if( _open )
			{
				for( var i=0; i<_items.length; i++ )
				{
					// set a var
					var item:DisplayObject = _items[i].mc

					// make the item visible
					item.visible = true

					// fade it in
					var tweenOut:GTween = new GTween( item, .3, {y:0, alpha:0}, {completeListener:done, data:item} )
					tweenOut.ease = Regular.easeOut
				}
			}

			_timer.stop()

			_open = false;
		}
		private function done( e:Event )
		{
			e.currentTarget.data.visible = false

		}

	}
}
Permalink:
http://www.christeso.com/index.php/lab/as3-drop-down-menu-classas3-drop-down-menu-class/
Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Print

15 comments to...
“AS3 Drop Down Menu Class”
Avatar
est

hi chris! just wanted to praise you for your work. sorry i’m new at this and i’m not a coder but i sort of want to understand just the basics of as3 and i wanted to know all the movie clips names and class exports you made for this to be possible.
seems like some stuff was made dynamic without even creating a clip and exporting in the library? not sure.
i think if you could help me out and just name all the movieclips and classes in the library that would help be a bit more helpful.
thanks so much!

est


Avatar
tebor

Est, thanks.

There are no clips that exist in the library. When I get a chance I’ll post a .fla with source for you.


Avatar
jeff

sweet class, thanks!


Avatar
Shannon

Very creative, I am just starting on as3 and I find diving head first into complicated code helps me best, I too would be very happy to see a .fla file, I have only programmed within Flash on the timeline. Since your work entails .as files I think I could understand as3 more for use in making my own projects. Once again great work, I enjoy your website a lot.


Avatar
Mo

Hi Chris,

Thank you for posting this, I think it’s what I’m looking for. I just have one question though. I don’t see the function “callback” defined anywhere in your script — what is it supposed to refer to? I know it’s the final variable in the function — I just don’t know what it does.


Avatar
eric

Hello this looks really interesting.
I am sadly, somewhat clueless as to how to implement your
fine example in a fla file.

It seems you have a class there, which could be DropDown.as stored in
a class path, but I am unable to properly use the class, and always get errors.

How would I structure my fla actionscript to use this class?

thanks for a great example


Avatar
m

looks like nice code, but I’m still learning as3. how exactly do you implement this.

cheers for the example.


Avatar
Damien

Hi Chris, thanks for this class . You had make a very good work. However I’ve got a question with the name of the package. I never had some package with a name like this. What I have to do with the name of the package if I want to use it by myself.

Thanks.


Avatar
Damien

Hi again Chris, but now I’ve got some problems with the package because with the var DropOtherArray. I think that’s the package must be in the beginning of the class. I tried some few changes but that’s still not work. Is it possible that’s you give us a Fla with the class ?


Avatar
Armegalo

What a lovely class :)
To anyone with problems implimenting the class, here’s how I did it

the example given above leaves out a coupla variable definitions that need to go
above the rest of the code.

var fmat:TextFormat;
var _dropOther;

Also, don’t forget you need to obtain gskinners motion package from
http://gskinner.com/libraries/gtween/

both gskinners motion package and your new DropDown.as package need to be
placed correctly in your folder structure to work.
com/teso/ui/DropDown.as
com/gskinner/motion/*

Finally, you will need a function called “other” to accept the mouseEvent triggered
when a menu item is clicked. Thus event.currentTarget.name will be the name of
button clicked and you can add your functionality from there.

I think that’s it… have fun!

armegalo


Avatar
Morgan Sutherland

NOTE: does not work with new version of GTweener. I got it working with Beta 4 (could work with later betas, but I didn’t test): http://www.gskinner.com/libraries/gtween/versionHistory.html

Working Example:

var fmat:TextFormat = new TextFormat;
var dropDown;

// array for drop
var dropOtherArray:Array = new Array()
dropOtherArray.push( {title:”iWork”, name:”folio”} )
dropOtherArray.push( {title:”iExperiment”, name:”lab”} )
dropOtherArray.push( {title:”iWrite”, name:”blog”} )
dropOtherArray.push( {title:”iPhotograph”, name:”photo”} )
dropOtherArray.push( {title:”iFlickr”, name:”flickr”} )
dropOtherArray.push( {title:”iRecord”, name:”vimeo”} )
dropOtherArray.push( {title:”contactMe”, name:”contact”} )

fmat.color = 0xffffff;
fmat.font = “Arial”;
fmat.size = 11;

dropDown = new DropDown( 180, 25, “iLiveElsewhere:”, fmat, 0×000033, dropOtherArray, “down”, dropTest );

addChild( dropDown );

//callback function
function dropTest() {
trace(”works!”);
}


Avatar
Anton

hii..

its realy great class
i have been try and its workss..

i am new in AS3.. how to make link to another movieClip at the button???

thanks…

anton


Avatar
Doccie

hello chris,

Thanks for a great class. I used it as the base for my own and it turned out really well…
One of the things I changed was the callback function. I don’t much like the use of callbacks, so instead I just dispatch an event now, after storing the selected item in a class variable.

So it now goes something like this:

var drop:DropDown = new DropDown(”MY DROPDOWN”, 180, 14, fmt, items);
drop.addEventListener(Event.SELECT, selectHandler);

private function selectHandler(evt:Event):void {
trace(DropDown(evt.target).selectedItem.name);
trace(DropDown(evt.target).selectedIndex);
}

and for the dropdown class itself…

private function selectHandler(e:Event):void {

// set label
_defaultText.htmlText = e.currentTarget.getChildByName(”t”).text;
_defaultText.setTextFormat(_fmt);

// set selected
_selectedIndex = int(e.currentTarget.name);
_selectedItem = _items[_selectedIndex];

// dispatch event
dispatchEvent(new Event(Event.SELECT));

}

Just thought you might be interested :)


Avatar
Rudi Hansen

Nice dropdown list, but for some reason when I copy/paste the code into a AS3
script file I get an error when I want to run it

“1083: Syntax error: package is unexpected.” | “package com.teso.ui”

I have DL the beta4 of gskinner and added the folders to my folder so that I
have both:
“com/teso/ui/DropDown.as”
“com/gskinner/motion/*”

hope someone can help me with this :)


Avatar
Audio Interconnect Cables

Your weblog is so informative … maintain the perform!!!!




required



required - won't be displayed


Your Comment:

CommentLuv Enabled

Twitter Users!
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.

iStream

folio

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Print
iStreamPrevious Entry

Blogging While Driving

some random thoughts while driving:

1. in the near future, aka now, it will be increasingly important to be able to publish while driving, moving and generally doing anything.
2. reading Gladwells latest, outliers. good book thus far.
3. 40 percent of Canadian pro hockey players are born in jan-march. its not how talented you are, its what [...]

Share and Enjoy:
  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Print
Blogging While DrivingNext Entry