Advertisement
  1. Code
  2. ActionScript
Code

Create a Smooth Rolling Scroller with AS3

by
Difficulty:IntermediateLength:ShortLanguages:

In this tutorial you will learn how to make a scrolling list activated by mouse up and down movements, with different speeds based on the position of the cursor. I'll also cover preparing list data with PHP, loading XML data and a brief explanation of the algorithm I came up with. Enjoy!


Step 1: Introduction

The other day, when I was testing a smart phone, I saw one of these cool, neat list scroller tweening effects. So, I started thinking how I could accomplish such an effect in Flash, did some research on the web and honestly didn't find exactly what I was looking for. So here is the result of my little assignment.


Step 2: Planning the Application

After some research I found a nice algorithm. It simply relates the mouse movement with the percentage of the visible area (later on, this will be identified as being the mask) that is actually being pointed. Thereafter, the container is moved in the same proportion.

Let's say, for instance, that the mouse is near the bottom of the blue curly bracket. In this scenario the container would move 20% towards the top.

As you can see this works pretty fine. When the mouse cursor reaches the top or the bottom of the visible area, we have the certainty that we've reached the beginning or the end of the list. This technique is quite straightforward because we don't have to worry about the list reaching, for example, the beginning and continuing moving along.

But... What if we have hundreds of list items? Now we have a problem. If our visible area (mask) has, let's say, 500 pixels of height and we have a list of 100 items (all content area), each of them with a height of 20 pixels, moving the mouse 1/5 of the visible area (mask) would result in the transition of 20 items (400px / 20px) at the speed of light. So I guess this approach wouldn't result very well.

So, I came up with this solution:

Basically, I've defined two zones – below and above the center. Each zone has a value associated. This value is calculated based on the distance between the mouse cursor and the center. So, if we actually associate this value with a variable we could change the speed based on the mouse position. The minus sign is just for deciding the direction of the movement.

Having said that, let's get some real content.


Step 3: Grabbing the Data

We will be using an external xml file to feed the application because:

  • We will be able to add, remove, edit, delete data very easily
  • Actionscript 3 has a great API to work with xml

So, we need a big list of items... What about the list of countries around the world?

(This step is kind of an extra topic because I'll be using other language to help me out with the xml file preparation. So if this isn't of your interest don't worry, this will be in the download files)

I've just Googled [list countries world] and in the first result I got this list:

  • 1 Afghanistan
  • 2 Akrotiri
  • (...)
  • 256 Zambia
  • 257 Zimbabwe

(source: http://www.listofcountriesoftheworld.com)

Copy everything to some text file and save it. I named it countries.txt.

The script below is written in PHP. You will need a web server to run it. I'm using wampserver, a nice bundle that installs PHP, MySQL, and Apache with a couple of clicks.

In order to use this script you will have to initialize the web server, and run the script in a subdirectory of your \www\ directory. One last note: the script and the countries.txt have to be in the same folder.

The output of this script will be saved in a file with the name data.xml. If you don't understand this step don't worry because you can download it from here.


Step 4: Setting up the .fla File

If you watch the demo, you will notice that our application has an interface, buttons and so on. So let's start putting all the graphics together.

Open your Adobe Flash and create a new ActionScript 3 file with the following settings:

  • Width: 450px
  • Height: 500px
  • frame rate: 63
  • Class: DocumentClass

Save the file. I've called mine scroll.fla.


Step 5: The Background Image

File > Import > Import to Stage and select the background.jpg image (all images are included in assets.zip, within the source download).

Now:

Window > align

This will open the Align Panel. Center the image on the stage, clicking on the buttons shown in the image below.

And now give your layer the name Background (don't forget to lock it):


Step 6: The Scroller's Background

Create a new layer and import the image pad.png. The process of importing is precisely the same as the previous step. Press F8 to convert it to a MovieClip and give it the name mcPad. Next click on the Export for ActionScript check box and, in the Class input field, type the name Pad.

Basically we are defining the pad MovieClip as a subclass of the MovieClip class itself. Therefore we are able to use it directly from our code and treat it as a normal MovieClip since it inherits from the MovieClip class. Pretty cool huh?

Now, delete this instance from the stage, but not from the library (because like I said we'll be using it from the code).


Step 7: The Buttons

Import the image itemBackground.jpg, press F8 to convert it to a MovieClip. Give it the name mcItem. Once again export for ActionScript and name the class Item.

Now the process will be slightly different:

  • double-click the mcItem MovieClip for making some changes inside
  • rename the only layer there as Back and lock it
  • create a new layer and call it Over
  • import the image itemBackground-over.jpg to the Over layer
  • convert it to a MovieClip and give it a name mcItemOver
  • position the mcItemOver MovieClip at x = 0 and y = 0
  • give it an instance name of item_btn_over

Finally, lock the Over layer, get back to the main timeline, and delete the mcItem movie clip instance from the stage.


Step 8: The Font

For this application I'm using the Arial Rounded MT Bold Negrito font (it's a free font, I've downloaded mine from here). Since it isn't a system font we will have to import it too:

  • on the library separator click on the upper right button (the one with a little arrow)
  • select New Font
  • call it fontArialRounded, then select the font on the dropdown box below. Once again Export for ActionScript and in the class field give it the name ArialRounded

In the main timeline we only need the Background layer, so you can delete the remaining layers.

That's it for the Flash Authoring tool. Let's start coding.


Step 9: Adding the Application Background to the Stage

Create a new ActionScript file, save it as DocumentClass.as. Then, add this code:

(If you're not sure what we're doing here, check out this quick introduction to document classes.)

Remember the mdPad MovieClip? Recall that we have exported it for ActionScript. So whenever we want to instantiate this MovieClip we just have to type

This code basically creates an instance of the mcPad MovieClip and adds it to the stage using the addChild() method. So now, the _pad is on our DisplayList!

Then I've centered it on the stage (vertically and horizontally).

Test the application and you should see the background layout on the flash player.


Step 10: Container

Our application will use a container to hold all the items together. So, we are going to create a new MovieClip in the DocumentClass. Declare the property:

Inside the constructor, at the end:


Step 11: Importing the XML Data

Add the following import statements:

Declare the following properties:

Then implement the following methods:

Finally add this method call to the constructor:

So far, we have:

Test the application and you should see, in the console, all the xml data. Let's move on.


Step 12: Building Each Button

Add the following import statements:

Add these new variables:

Update the dataLoaded method:

Now we've created the 200+ buttons and put them on the screen. If you run the application you will notice that this list begins at postion (0, 0) and fills down the entire stage. That's not what we want so let's handle that in the next step.


Step 13: Masking the List

Let's create a shape to mask the list so that we only see the visible area, as in the picture from before:

At the end we will center the mask and the container. So, add another import statement:

Properties:

And... In the dataLoaded method at the end, add the following code:


Step 14: Adding a Background to the Container

Just a little improvement... Right now our container has a lot of holes because there's a margin between each item. So let's give it a background to avoid undesired results when listening for events.

Property:

Still in the dataLoaded method, below the existing code:


Step 15: Adding the Event Listeners

As you probably know, listeners listen for events. Here we will use one for the MouseOver event and another one for the MouseOut event.

Add this at the end of the dataLoaded method:

Let's now implement the movingOver and movingOut methods:

Now we can listen for events, more specifically MOUSE_OVER and MOUSE_OUT. Events are like messages that notify any object waiting for that same event. The "message" just broadcasts that the event has occurred. In this case the _container object will be listening for MouseOver and MouseOut events. When they take place the _container has the ability to respond accordingly.

So, first we assign the event listeners to the _container object and then, later on, if the _container is hovered over the MouseOver listener is removed. On the contrary, when the mouse is out of the _container object the mouse over listener is assigned again to the _container.


Step 16: Event Flow

The event flow is a powerful mechanism that allows the programmer to handle several objects with just one event listener. So, as you probably imagine, _container.addEventListener will "listen" for events involving the container, the items and the background shape – that is, the container and everything inside it. You can confirm this by adding the following code in movingOver and movingOut methods:

This will trace the object that dispatched the event.


Step 17: OnEnterFrame

When listening for this event we can define a behavior that will take place every frame, in our case 63 times per second. That's why the OnEnterFrame event is very useful for animations based on the fps. We will need this to move our scroll, so the movingOver and movingOut methods should look like this:


Step 18: The enterFrame Method

Add these properties:

Add the following method at the end of the code (below the movingOut method) . This will be run every frame, thanks to the EnterFrame event listener we created earlier. I'll explain what this code does below.

And here is the logic of the scroller:

This line of code gets the speed by dividing half of the stage height with the mouse Y position.

Let's say that half of the stage is 200px and the mouse cursor is at position 300px. Applying the formula we get:

(200 – 300) / 200 * 15 (_maxSpeed property) = -7.5 which is negative, so the list will give us a downward movement. That's precisely what we are looking for. If the mouse cursor was above the center we would have a positive value and the list would move up.

Next, with this statement:

We are assigning the actual speed, every frame (63 times per second) to the _container.y position. Then we check with the if statements whether the container's y position is where it's supposed to be. Remember this picture:


Step 19: Animating the Item's Button

I'm going to use greensock's TweenMax Library (you can download it from here) to animate to each button on and off (when the mouse is hovering over or leaving the item).

Add this import statement:

Update the movingOver and movingOut methods:

Let's analyze this code in detail. The event.target will reference the object that will dispatch the event, in this case MouseOver or MouseOut. This object could be an _item, the _background or the _container but we are only interested in Item objects so we specify:

Then, if so, we will animate whatever Item object dispatched the event by typing Item(event.target).item_btn_over. The item_btn_over is the instance name of the mcItemOver MovieClip that we created in Step 7. .2 is the animation's time, and in the last parameter we specify which property we want to animate (alpha).


Step 20: Refactoring

Right now, our application works pretty fine. However, we have some code repeated and a very centralized application as well. Notice that the dataLoaded method is doing almost everything.

Actionscript has excellent support for Object Oriented Programming. We could use that to detach data loading from application logic etc.

There is a nice design pattern called Model View Controller that works really nicely with user interfaces. This pattern basically separates the application in three distinct layers. The Model deals with the business logic, data handling. The Controller deals with human interaction with the application. And finally the View deals with the visual interface of the application.

In this case our application is too small to implement this pattern. So, let's adapt just the Model and a miscellaneous View/Controller to our application. First let's just handle some repeated code.


Step 21: Common Tasks

Create a new ActionScript file and type the following code:

We have to perform this task several times. So I decided to make a class that aligns any display object to the stage whenever we call it. In order to make this work you have to make a directory structure like this:

\com\tutsplus\active\util\

in your production directory or inside your class path. Next, in the refactored DocumentClass you will see how to use this class.


Step 22: Model

The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view) and responds to instructions to change state (usually from the controller)

source: MSDN.

Loading the data and storing it in data structures are nice operations for composing our Model. Create a new ActionScript File and call it ScrollModel.as:

With this update, our dataLoaded method has only 3 lines of code now!

It's almost the same code that we used in the DocumentClass for loading the data, with just one difference:

After assigning our 200+ items to the XMLList property this line of code dispatches one event to whoever is listening. In practice we will need to know when this occurs to use these data in another class.


Step 23: The ScrollBox Class

Create a new Actionscript File and name it ScrollBox.as:

Notice the Align class methods that we are using:

Now we just have to use the method Align.centerHorizontallyInStage() to align any display object horizontally.

The createRollingScroller method will only start when we finish storing all the data in the items:XMLList property.


Step 24: Final DocumentClass

Change the DocumentClass:

Once again, note how easy it is to center display objects on the stage with the Align.centerInStage() method:

Conclusion

That's it for now guys! I've covered some interesting Actionscript 3 topics and I hope you find this useful. Why not improve this application to a next level, recfactoring, adding functionalities, etc?

Thanks for following. Take care!

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.