For a project my company is working on we had to develop a Flash menu that was "infinite" meaning that when you scrolled horizontally it keeps going forever and wraps the menu elements around in a loop. When I first was thinking how I was going to do this I thought it would be fairly simple, but it ended up being more complicated then I thought.
I decided a few things up front, first that each element would be the same width and that each time you moved the menu it would move the width of one menu element. Next I decided only to focus on moving it in one direction, left. Getting the elements to move was simple. I first was thinking about using some type of hit test to tell what objects were visible and based on the width of the menu figuring out what I needed to show. I decided that this would not be a good way to do it since it might make the script unadaptive to some situations (and one of the goals of this was to be able to reuse it in future projects).
I then decided that I should just keep track of the last, or furthest to the left, block (block is what I called menu elements). Doing this would let me figure out what elements I needed to wrap around because, first I know the width of the whole menu (or the number of viable blocks) and the number of total blocks. This worked pretty simply for the left movement and it worked for the right movement as well but it took a few more lines of code. The problem with the right movement was that it had to pay more attention to the total width of the menu.
Anyways I'm not sure what else I could do better, there might be a much better way to think about this but this is what I came up with and it works well. You can get the code at http://github.com/Benmills/flash-infinite-scroller/tree/master. Application.as is the document class and Block is the menu element. The block class doesn't have any real logic regarding the infinite scroller concept, just a simple constructor to get it on the stage / menu./
Infinite Horizontal Menu in AS3
Published on September 21, 2009 by Ben Mills
