Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we'll take a look scrolling our text field by clicking the button's layer. If you select the top arrow button you'll see in the Property inspector that the button's Instance Name is up_btn. And if you select the bottom button, what you'll see in the Property inspector is that the buttons Instance Name is down_btn. Let's select the first keyframe of actions layer and open the Actions panel by pressing Option+F9 on the Mac or F9 on the PC. Below the easing variable we created in the last movie, let's create a new variable called scrollAmt, with capital A. We'll set the data type to a number and the value to 15.
This will refer to how many pixels will scroll each time we click a button. Scroll down in your code to find the scrollUp and scrollDown functions. In the scrollUp function run a function called setDirection with capital D. And in parentheses pass in the scroll amount. We'll define the setDirection function later in this exercise. In the scrollDown function, run the setDirection function and pass in negative scroll amount, just scroll amount with a minus sign before. So -scrollAmt.
And now we'll define the setDirection function. Let's do this right below the scrollDown function. Type function setDirection, with a capital D. It'll need to receive one value and we'll call that dir, short for direction, and the data type will be a number. The function will not return a value, so it will be void. And in the curly braces we'll type text_mc.y += dir What we're also going to do inside of this function, is we'll stop animating the text field with the scrollbar once the user clicks a button.
So let's scroll up and find the code that we commented out in the last exercise. That should be in the stop dragging function. Mine is on line 31 through 35. We're going to select all of line 34, the stage.removeEventListener line of code, and I'm going to press Command+X on my keyboard to cut that code. You can press Ctrl+X on the PC to do the same thing. Scroll down in your code. Find the set direction function. At the bottom of the function, paste the code that you cut by pressing Command+V on the mac, or Ctrl+V on the PC. I'm going to delete the // at the beginning of this line to disable the comment and to enable the code.
Now let's test the movie using Command+Return on the Mac or Ctrl+Enter on the PC. Click the down button to scroll down and click the up button to scroll up. Notice if you hold down your mouse on the buttons, the text field doesn't move. We'll add that functionality in the next exercise. So there's a look at controlling the scrolling of a text field using buttons.
Get unlimited access to all courses for just $25/month.Become a member