- Home
- Tutorials
- Flash
- Intermediate
- Creating a Scrolling menu

Creating a Scrolling menu
The following tutorial will show you how to create scrolling menu. When you've got many pages in a website sometimes you might need to conserve space that'd be allocated for the long menu containing links to each page. With a scrolling menu you can conserve space as well as add some appeal to you're website. I'll be showing you how to create such a menu using Flash MX and ActionScript.
After you've opened up a new document, re-name the first default layer to "actions". Insert two more layers and name the second layer "background" and the third layer "menu". 
Select the "menu" layer and draw rectangle on the stage using the Rectangle tool. Select the whole rectangle and changle alpha to 60. With the whole rectangle still selected press F8 to convert it into a Movie Clip and give it the name "menu".

Double-click the the rectangle. Now just like above, select the whole rectangle and convert it into a movie and give it the name "button" with an instance name of "button0". There should be one layer in the timeline, Double-click it and re-name it to "button".
Insert another layer and name it "actions" and click, hold and drag that layer below the "button" layer. Select the first frame of the the "actions" layer and open up the Actions panel. When the Actions panel is opened, Copy-and-paste the following code in:
var tmi = 6;
var homeY = button0._y;
var homeW = button0._width;
var homeI = button0._x + homeW + 10;
/*
Duplicate the button tmi (total movie instances) times giving the newly duplicated movie a x location that is 10 pixels plus the width of the button
*/
for (i=1;i<tmi;i++)
{
button0.duplicateMovieClip("button"+i,i,{_x:homeI, _y:homeY});
homeI += homeW + 10;
}
Now select the "button" layer and then single click the button on the stage, open up the Actions panel again. Copy-and-paste the following code into it:
/*
Change size of button when button is hovered and change back when button hovers out
*/
on (rollOver) {
this._width += 10;
this._height += 10;
}
on (rollOut, dragOut) {
this._width -= 10;
this._height -= 10;
}
After you've pasted the code above, navigate back to the main timeline and select the first frame of the Actions layer, afterwards open up the Actions panel. When you've opened up the Actions panel Copy-and-paste the following code into it:
var bxi = 0; //Holds current speed of scrolling menu
var stageW = Stage.width;
/*
midStage holds the pixels located in the middle of the stage
*/
var midStage = mouseBuffer = stageW / 2;
/*
Declare a mouse listener object, have onMouseMove event handler point
to the directMenu function and then add listener via addListener(...)
*/
mouseListener = new Object();
mouseListener.onMouseMove = directMenu;
//Declare and define the directMenu function
Mouse.addListener(mouseListener);
function directMenu()
{
/*
mouseBuffer stores the x coordinate of the mouse point from the last
mouse move event. Then on the next mouse move the following code checks
the current x coordinate of the mouse pointer with the one previously
stored in the mouseBuffer and takes appropriate actions
*/
if (mouseBuffer > _xmouse)
{
/*
If mouseBuffer is greater that means that you moved left so move
so move menu right by adding to bxi. If the xmouse is less than the
the middle of the stage increase scrolling speed.
*/
bxi += (_xmouse < midStage) ? .2 : .1;
}
else if (mouseBuffer < _xmouse)
{
bxi -= (_xmouse > midStage) ? .2 : .1;
}
mouseBuffer = _xmouse;
}
When I got to this point I had already put in a background, but because of how nice it looked I decided to change the background which is why I figured I'd wait until the last part of the tutorial to have you put in a background. As I do with most my tutorials, I let you choose the background, if any, to allow you to be creative. Ok, now select the "background" layer and import your background image to the stage and position it appropriately.
Well, that concludes the tutorial and I'll omit a description of what the code does because the comment in the code does a fair job.
Spread The Word
Related Links
Attachments
8 Responses to "Creating a Scrolling menu" 
|
said this on 03 Mar 2007 2:03:26 AM CDT
the code is incomplete, the var bxi is set but never used, i may be able to figure it out but i feel lost
|
|
said this on 08 Mar 2007 2:08:28 AM CDT
The bxi is used, fyi. It is used within the "directMenu()" function an allows you to adjust the speed of the scrolling menu. If it weren't complete, why did it work for me?
|
|
said this on 16 Mar 2007 9:17:18 AM CDT
Well guys I got a totally different problem
I kept receiving this message //**Error** Symbol=menu, layer=actions, frame=1:Line 17: Mouse events are permitted only for button instances on (rollOver) { **Error** Symbol=menu, layer=actions, frame=1:Line 21: Mouse events are permitted only for button instances on (rollOut, dragOut) { Total ActionScript Errors: 2 Reported Errors: 2// I am on Flash 8 professional. Am I a dunce or was the code really not legitimate. As a result, I could not even see the first reaction in the tested movie, cos I could not test it |
|
said this on 06 Apr 2007 12:10:03 PM CDT
You have put button events on the main timeline, you need to put those events directly to the button... hence you get an error.
|
|
said this on 30 Apr 2007 4:37:54 PM CDT
How about if you want to customize each button and make each one of them access different items/menus/options..?
|
|
said this on 22 Jun 2007 9:25:27 AM CDT
I love the look your technique delivers but how would i ad functionality to the rollover buttons generated randomly?
|
|
said this on 24 Jul 2007 11:13:28 AM CDT
Sorry for the prolonged delay, but I'm back. I guess you could employ the mouse click event, get the coordinates of the mouse cursor, scan each button object and see if the mouse cursor coord's were within the area of a given button - using looping and condition. When a winner is found, you can eval a function label convention prefix with an incremented value, or something like that, and execute an already defined associated function, which would have the role of a event handler.
|
|
said this on 08 Nov 2007 10:03:09 PM CDT
brian, i know what you mean, i had the same problem. on testing the swf, menu won't move ! i searched on the sample file, and what is missing is a clipEvent applied to the menu movieClip in the _root timeline. get the .fla for the code
cheers ! |



Author/Admin)