
Please update your flash player
Welcome to the latest instalment of the Group 94 style scrolling menu tutorial! (With downloadable source files of course)
I have been very busy lately, and haven’t had a chance to share much code. I have been learning AS3 and have to say that I love it! It is much easier to adhere to OOP principles and building class based applications has many advantages.
Anyway, I received an email today from Adriaan, who was wondering how to convert the Group94 scrolling list into a menu system, with rollovers and click functions. I hacked the old source to do this, but thought it would be a good opportunity to revisit the ‘offset scroll’ idea within the contexts of a fully functional scrolling menu.
So here it is. I have built it into a simple class, which means you need to do very little to get it working :)
The class is very flexible, and you can assign functions and parameters to each menu item that you add. This can mean calling the same function with different parameters, or entirely different functions for each item.
Adding menu items is as brilliantly simple as newMenuItem(label, function, parameters)
You can download the source code and pick it apart all you like. However, here is a quick explanation of how to use the G94Menu class.
Firstly we import the classes:
import com.soulwire.g94menu.G94Menu; import com.soulwire.g94menu.MenuItem;
MenuItem is linked to a movieClip in you library, so you can use any style of menu item you like.
Now, we create a new instance of the G94Menu class and add it to the stage.
exampleMenu = new G94Menu(3, 1, 25); addChild(exampleMenu);
The parameters the class constructor requires are:
- Visible items: The amount of items visible at one time
- Item spacing: The gap between each item
- Item offset: How far the items scroll up or down as they fly off to the top or bottom
Then we simply add as many items as we want using the new MenuItem constructor, for example:
myMenu.addItem(new MenuItem('Text', func, { params } ))
Here is a breakdown of the parameters you pass to the MenuItem constructor inside the addItem method:
- Item label: A text string which will be the visible label of the item
- Function: This is optional, but if present will make the item a clickable button, executing this function when clicked.
- Parameters: This is an object containing as many parameters as you need. Again, this parameter is optional, and only needed if you have passed a function and it requires parameters.
Now we create a function that will be called when a menu item is clicked:
function someFunction (e:MouseEvent) { if(e.currentTarget.params != null) { var info:Object = e.currentTarget.params; trace(info.page); } }
In this example, we check that the parameters are not null, then access the parameters object using e.currentTarget.params. If your function will open a link (for example), and you pass a URL identified in the parameters object as pageLink, then you can use this by calling e.currentTarget.params.pageLink.
Lastly, to listen for mouse clicks on the scroll arrows, and to enable the mouse wheel, both of which will scroll our menu up or down, we add some event listeners:
function enableScrolling():void { btnUp.buttonMode = btnDown.buttonMode = true; btnUp.addEventListener(MouseEvent.CLICK, exampleMenu.scrollUp); btnDown.addEventListener(MouseEvent.CLICK, exampleMenu.scrollDown); stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel); } function onMouseWheel(e:MouseEvent):void { e.delta <0 ? exampleMenu.scrollDown() : exampleMenu.scrollUp(); }
The scrollUp and scrollDown methods can be called whenever you like (i.e. remotely) on any instance of the G94Menu class.
Once again, I really hope this is useful, even if it is just a simple introduction to Actionscript 3 and writing classes.
Thanks to Adriaan and all of you who have been commenting for inspiring me to update this example.
If anyone wants to use this in older projects, and needs an AS2 version, please let me know and I’ll convert it when I get the time.
7 Responses to AS3 Group94 Menu
pascal
Nov 17th 2007
7:19 pm
Haha, thanks!
You make our life much easier!
Could you quickly convert the rest of our portfolio as well while you’re at it?
;)
Pascal - group94
David
Nov 28th 2007
11:51 am
Hi
Love the new scrolling Menu, looks very elegant compared to a lot of things on the web. well done.
Really like the way it moves so had a little play with a few other colours today and they look great.
i do have a quick question.
Im trying to get the buttons to work as links so i can change the menu contents when an option is selected. Similar to the way that the ipod menus change. I’ve tried editing the code on the Main page but can seem to get it right. Do i need to edit the code on the MenuItem page as well to get a like to work properly ?
Thanks
David
David
Nov 30th 2007
5:37 am
Hi
I’ve solved the probelm of adding working links to the buttons. Now im trying to add a sub menu in the same style. do you know if this can be done ?
I could load another swf into the file but it would be easier to just use one file with sub menus
Thanks
David
V
Dec 20th 2007
12:20 pm
Hi!
Great menu! I’m wondering if you can post a AS2 version of it.
Greatly appreciated!
V.
Nik
Jan 31st 2008
4:01 pm
Thanks Justin! This will be a great example to learn a few things from…
Lance
Feb 11th 2008
2:20 pm
I borrowed your source and added it to a sight I’m working on. Then added some code to make the button list dynamic based on an XML document. The only problem I’m having is that the buttons, (when they are supposed to be gone) are still clickable.. Like your example above. if you pass your mouse under your last entry a little ways down into the blue, you can still see they button is their with the alpha reduced to 0. I’ve been trying to fix it, but haven’t gotten it yet.
Lance
Feb 11th 2008
7:22 pm
Hey, I figured it out…. A change to the sort() function (posted below): Now it works nicely, and I made my version scaleable with the browser and XML driven.
Thanks for the awesome start!
Leave a Reply