
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.
42 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!
charlie
Jun 28th 2008
1:57 am
hey when i run the code ( i just want to learn by the way), all i get is the two arrows you put in as symbols in the .fla. None of the additems you put in the main file show up. could you help me explain why? and i think some code is missing because ‘label’ and ‘bg’ are never defined. maybe you can send me an updated version? thanks
ivo
Jun 28th 2008
12:20 pm
hey i have downloaded this but doesnt work =/
can anyone sent me one working ?
a.free.mind@hotmail.com
Soulwire
Jun 28th 2008
12:59 pm
Charlie and Ivo - I presume you’re using flash CS3? Also, if nothing but the arrows are appearing you may need to try adding an absolute classpath. Extract the contents of the zip download into a new folder, then in your document settings in the FLA point the classpath to the src folder. Try adding ‘trace(this)’; to the constructor in the class Main.as and see if it outputs anything when you compile.
I can’t think of any other reason why you might be having problems.
ROBIN
Aug 7th 2008
5:40 am
Hi again Justin
Sorry to bother you again with my simple problems. But I feel I learn quite a bit from your help. After my source FLA crashed and disappeared mysteriously I converted to the AS3 version of your menu (still have the AS2 code in separate .as files but I feel it’s better to go AS3). Yesterday I rebuilt the whole gallery in AS3. I have connected all my textfields to the constructor and it works perfectly with text strings, but I have some small problems though:
You helped me import an image from an array to an empty movieclip in AS2, how can I do this with the constructor for the menu in AS3? I have tried myself but I can’t seem to find the right way to do this. I also wonder if you could explain this with a code example:
“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.”
I hope this will be the last time I will have to ask for your help.
Best Regards,
Robin
Soulwire
Aug 9th 2008
7:57 pm
Hey Robin,
Loading images in AS3 is a little harder unfortunately, but it can still be done in a similar way. You use the Loader class, which is a DisplayObject and so can be added to the stage. Something like this:
It works much like the MovieClipLoader class in AS2. If you use the code above but populate the URLRequest with a string from your array, you should be fine.
Regarding the parameters example you mentioned; what’s happening there is that the MouseEvent which is passed to the function associated with the button contains a variable ‘currentTarget’, which will be the particular button which was clicked (they all trigger the same function in this case). So to access the specific URL of a button, you need to store that URL in a variable within the button object. In other words, when you create the button using the new MenuItem constructor, you pass in the function which you want it to fire once its clicked, but you also pass in some parameters, such as the button specific URL (just running with the same example here). These get stored in the button (or MenuItem) object, within a child object called ‘params’, and are then retrieved by the function when it’s clicked. The pageLink example was just an attempt to show that, because the generic Object class (which is what ‘params’ is an instance of) is dynamic, you can specify any arbitrary variable and value to be held within it. So when you put it all together e.currentTarget.params.pageLink is tantamount to “the MouseEvent triggering the function (e) has a property called currentTarget, this will be one of the menu items, so inside that menu item, access the ‘params’ object, and from that extract the variable named ‘pageLink’. This will be a URL string, so then open the page (or whatever).”
Object Orientated programming is a beautiful and elegant thing, but can take a bit of getting used to at first as it can sometimes go against the linear approach that you could get away with in previous versions of Actionscript.
Hope this helps! :)
John Irving
Aug 19th 2008
4:30 pm
Thius menu system looks really nice :)
I was wondering how to make this menu system continuous so when it gets to the bottom of the menu it push the items from the top of the menu below the last item in the menu and vice versa.
Any adivce on this Justin?
Cheers.
thomas
Sep 10th 2008
11:59 am
I love this menu and i’m trying to start using as3 and document classes. I have been staring at the code for about a week now trying to get the menu swf to load on top of another file but i’m getting all these errors and can’t quite load it. I’m also trying to make the menu, when clicked, load another menu, sort of a sub menu. Once i go through a couple of “sub menus”, i wanted to load a picture gallery from an external swf. I’m trying to wrap my head around this as3 and it’s been slow going. I would love any help that might be extended my way. I hope this makes sense.
tom
rich
Oct 28th 2008
4:49 am
hi,
has anyone done a version with horizontal scrolling?
Soulwire
Oct 28th 2008
11:24 am
Hey Rich,
I think it will really only be a question of switching y for x and width for height. The rest of the logic should hold which ever way you’re scrolling!
Hope you work it out.
Cindy
Nov 3rd 2008
6:54 pm
Hello,
I’m having the same problem as Charlie and Ivo. I’m really new to AS, so I may be making a mistake, but when I open the .fla file, I don’t see actions in the main timeline. When I export the movie, I just see the arrows. Is there supposed to be an .as file in the .zip too? Sorry for the newbie question, but I’m not seeing any of the code. I am using Flash CS3.
Thank you!
Soulwire
Nov 3rd 2008
7:06 pm
Hey Cindy,
Yep, there is no timeline code, instead there are classes controlling the movie. Make sure the folder called ’src’ is in the directory you unzipped the archive to, and then publish. If you’re still having trouble, click the pencil icon next to the ‘Document Class’ item in the properties panel (Flash CS3). If it gives you an error saying that it cant find the definition for the document class, edit your publish settings and under ‘classpath’ give an absolute path to the ’src’ folder.
I’m certain all will be fixed once you unzip the entire contents of the download to a new folder and then try and compile the movie. In the meantime, have a little read up on classes and OOP and this whole external .as file malarkey will make much more sense :)
Cindy
Nov 3rd 2008
8:22 pm
Excellent! Now I have it working. Thanks for your help and for the tips - I’ll do some reading! :)
Fabio
Dec 5th 2008
7:26 pm
Hello. How are you?
It’s very good tutorial. I changed your menu to x position. But the buttons are above my btns Up and Down.
You know how I can change the depth? For the list of buttons is created behind the btns Up and Down?
Thanks
Fabio
Dec 5th 2008
8:20 pm
Hi!
I made a change in the public Main () of Main.as.
Where is the call addChild (exampleMenu)
I changed to
Var index: UINT this.getChildIndex = (btnNext);
AddChildAt (exampleMenu, (index - 1));
Thanks
gustavo
Dec 30th 2008
2:50 am
Hi… excellent. Sorry for my english… I’m spanish… but… I try.
addEventListener (MouseEvent. MOUSE_WHEEL, onMouseWheel);
Doesn’t work fine.
Thanks for this code!.
Gus
brendan
Jan 12th 2009
8:48 am
@ Gustavo -
Are you on a Mac? there are issues with Mousewheel and MAC ( google it).
hope that helps a little
b
Soulwire
Jan 12th 2009
11:32 am
Cheers Brendan!
Yeah, I think the best solution I’ve seen is to use ExternalInterface to recieve the scroll events. There’s a plugin for SWFObject which should help: http://blog.pixelbreaker.com/flash/as30-mousewheel-on-mac-os-x/
neil
Jan 19th 2009
8:09 am
Hi and first of all thanks for the tutorial, very helpful.
I’m trying to load this into another file and keep getting the error : 1009
“Der Zugriff auf eine Eigenschaft oder eine Methode eines null-Objektverweises ist nicht möglich.”
Everything works fine on it’s own, just the problem calling the swf. Hope this makes sense?
Grateful for any help. Best wishes Neil
neil
Jan 20th 2009
12:59 am
Hi, problem solved :) i removed
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
and now it works fine. Thanks Neil
Ruben
Jan 27th 2009
11:26 pm
Hi, I came up with same problem as Lance, the buttons are still clickable when they’re not visible. I didn’t quite understand the solution Lance gave us, so I solved this problem by setting the buttonmode to false in the sort() function, this did the trick, but if you accidentally click above or under the menu, where the non-visible items are, the mouseEvent is still there and calls the page. Is there a way to remove the mouseEvent on the non-visible items?
This is my code in the sort() function:
Ruben
Jan 27th 2009
11:30 pm
mmm, the code is completely messed up with submitting… Probably the reason why the code of Lance didn’t work…
Soulwire
Jan 28th 2009
11:42 am
Hi Ruben,
Sorry your code got messed up. I need to stop Wordpress from escaping code in comments…
You won’t need to remove the event listeners. Try using mouseEnabled instead of buttonMode and that should do it.
Ruben
Jan 28th 2009
11:15 pm
Thanks! Mousenabled didn’t work at the first time, but also adding mouseChildren gave me the wanted result!
Soulwire
Jan 28th 2009
11:48 pm
Good stuff. Yeah, TextFields often cause this problem. Glad you got it sorted…
rich
Mar 4th 2009
6:19 pm
any AS3 guru able to make this already-great menu into a continuous one? I hate to ask for free assistance, but Im willing to make some $ donation if anyone can help… Post your email so I can contact you, thanks.
cicit
Mar 8th 2009
3:02 am
hey when you clicked the arrow fast the menu getting an error
Soulwire
Mar 8th 2009
6:59 pm
What’s the error you’re getting? I can’t seem to reproduce it…
hotroddd1
Apr 7th 2009
9:21 am
Hey, thank you for this example. It hes helped me understand many things with AS3 and coding in general. One showing a solid way to keep things dynamic with custom classes. Two helping me see how parameters from functions can be passed to other functions.
So now I’m going to try and modify what you have given me into 3 different things
1. A Drop down menu system
2. A side bar
3. A constant horizontal news bar
So i have to figure out how to make 3 instances of this custom class
By the way I have fixed the clickable item issue on the edge of the screen.
Here is what I did for those who can’t get the other fixes to work.
Arif
Apr 11th 2009
6:54 pm
I love the tutorials& knowledge you’re sharing. I have a quick question about adding a feature.
How would I go about making the middle item display an image. Like this : http://www.normann-copenhagen.com/ (go to products & you’ll see the middle item displays an image)
Thanks,
Arif
Robin van Emden
Apr 19th 2009
7:12 pm
Hi!
I would like to thank you for making your source code available, saved me a lot of time while working on a project with a tight schedule :)
Robin
Kris
May 24th 2009
12:19 am
Justin,
I am trying to integrate an event handler for screen resize. I’m not really sure where the most appropriate place would be to append my code.
in its own separate flash project, I am using the following code, and it works fine with no errors:
However, when I place that in the Main.as file that goes with the g94scroller project, I get the following error:
Soulwire
May 25th 2009
7:36 am
Hi Kris,
The event you need to add to the stage is the resize event:
…though the 1046 error you’re getting means that the compiler can’t find the code for the Event class, meaning that you need to import it:
gustavo
Jun 3rd 2009
11:01 pm
better animartec.com
Soulwire
Jun 3rd 2009
11:47 pm
huh?
Praveen Ghatta
Jun 16th 2009
7:58 am
Cool Work, It feels awesome to read the code structure and the way its been implemented.
Leave a Reply