Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we'll take a look at dragging the scrollbar. If you're following along,. I'm working in 02_Dragging.fla in the Chapter 04 folder in the Exercise Files folder. On the stage, make sure the buttons layer is unlocked and select the scrollbar on the stage. Note that the scrollbar's Instance Name is scroller_mc and it's an instance of mcScroller. The scrollbar here is just a simple rectangle inside a movie clip. Also on the Buttons layer note the line background for the scrollbar. It's an instance of movie clip called mcLine and it's Instance Name is line_mc.
Now let's look at the first keyframe in the actions layer and open up the Actions panel by pressing Option+F9 on the Mac, or F9 on the PC. In the Actions panel scroll down to the bottom of your code and find the code written on line 27, scroller_mc.addEventListener(MouseEvent.MOUSE_DOWN, and then it will run a function called dragScroller. So as soon as your mouse goes down over the scroller_mc movie clip, a function will run called dragScroller. Now that function has already been defined for you if you have access to the Exercise Files.
Let's scroll up and find the dragScroller function. In my code it starts on line 10 and ends on line 13. Inside of the curly braces of this function we're going to specify the boundaries for dragging the scroller movie clip. We need to specify boundaries of the user can't drag the scrollbar all over the screen. To specify drag boundaries, we need to create a rectangle. Now if you've never worked with rectangles before in Flash that may seem kind of confusing, but actually Flash only wants four parameters when you create rectangle. Flash wants the rectangle's x position, y position, width and height.
So let's create variables to hold those values. So I'm going to create a variable called dragX, with a capital X. The data type is going to be Number. This is going to be the x position of the drag boundary for my scrollbar. The value I'm going to pass in is line_mc.x. If I simply have that value here, then x boundary of the scrollbar will be aligned with the x position of the line movie clip. What that will look like on the stage is that the left edge of the scroller will be aligned with the left edge of the line.
What we want is the scrollbar and the line to be center aligned. So we're going to need to write some more code to have Flash center align these two movie clips. After line_mc.x type minus scroller_mc. width divided by two and a semicolon. This shifts the x position of the scroller movie clip to the left, half of the scroller's width. So think about how that works. The entire scrollbar is on the right side of the line.
By subtracting half of the scrollbar's width, the scrollbar becomes centered with the line. Let's create the next line and create a variable to represent the drag or rectangle's y position. We'll call this dragY, capital Y. The data type will also be Number and we'll set this equal to line_mc.y. Now this will specify the highest amount that you can drag the scrollbar up. Here setting the value line_mc.y tells Flash to not let the user drag the scrollbar above the top edge of the line.
And that's the exact value that we want there. Let's go to the next line and now we'll specify the drag width. So create a variable called drag and then capital W. The data type will also be Number and here we'll set the value to 0. We don't want to be able the drag the scrollbar left or right at all. We only want to be able to drag it up and down. Let's go to the next line and create a variable called drag and capital H. This will also be a Number. And this value will specify the height of our rectangle. Now we want it to be close to the same height as the line_mc.
So we'll type line_mc.height. And then we'll subtract scroller_mc.height and that will align the bottom edge of the line with the bottom edge of the scrollbar. So that's as far as the scrollbar down. Let;s go down a few lines and now we'll make our scrollbar draggable. Type scroller_mc.startDrag with a capital D. Passing a value of false here will not lock the registration point of the scroller movie clip to your mouse position when you click and drag.
That way user can click and drag the scrollbar from anywhere the user wants to. If we pass in a value true, the top left of the scroller movie clip would be connected to the user's mouse, which would be kind of awkward. So let's keep a value of false. I'll type a comma and a space. In the second parameter Flash wants a rectangle, and a rectangle is what specifies the drag boundaries. And we created variables for all those boundaries already so it'll be nice and easy to create a rectangle now. Type in new Rectangle, with a capital R. And then ( and notice in the code hinting that Flash tells you four parameters that you need to create a rectangle - the x position, y position, width and height.
We'll just pass in the variables that we created for these values. So type dragX, with a capital X, dragY with a capital Y, dragW with a capital W, dragH capital H. Close out the parentheses for both the rectangle and start drag. Semicolon to end the statement and let's test the movie and drag the scroller movie clip. Command+Return on the Mac, Ctrl+Enter on the PC. I'm dragging the scrollbar down. You may have noticed that the scrollbar slipped a little bit to the left, when I clicked and dragged it.
I'm going to close the Preview window. That's actually a bug with the start-drag method when you specify boundaries. So what I'm going to do is click right at the end of the dragX line of code. I'm going to add 1. Now I'm going to test the movie again, Command+Return on the Mac, Ctrl+Enter on the PC. Now when I click and drag the scrollbar, it doesn't shift the left. You may have noticed when you tested the movie that when you release your mouse button to scroller movie clip never stops dragging. So what we'll do on the next line of code, we'll add an event listener to the stage, so that anywhere you release your mouse in the stage will stop dragging the scroller movie clip.
So Type stage.addEventListener, capital E, capital L for listener. And I'll pass in MouseEvent, capital M, capital E, .MOUSE_UP, all caps and we'll run a function called stopDragging, capital D. Below the dragScroller function, let's create the stopDragging function. So create a function called stopDragging. Make sure it receives an event with the data type of MouseEvent and inside of the stop drag function type scroller_mc.stopDrag with a capital D. Then type some parentheses and a ; to end the statement.
Now we'll test the movie using Command+Return on the Mac, Ctrl+Enter on the PC. Click and drag the scrollbar to move it up and down. Release your mouse anywhere on the stage and the scrollbar stops dragging. I'll close the Preview window now. And there's a look at controlling boundaries for dragging a movie clip.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.