navigate site menu

Become a member and get unlimited access to every course in the library. Try it free for 7 days

Flash CS4 Professional: Building UI Elements

Flash CS4 Professional: Building UI Elements

with Todd Perkins

 


Creating user interface (UI) elements becomes easier with the integration of Flash ActionScript 3.0 and object-oriented programming. In Flash CS4 Professional: Building UI Elements, Todd Perkins demonstrates how these two programming tools work together to build common UI widgets, such as scrollbars, preloaders, sliders, and checkboxes. To streamline the workflow, Todd shows how to reuse custom codes across applications with object-oriented programming (OOP). Step by step, he builds one widget per chapter, covering the skills it takes to develop a fully functioning application. Exercise files accompany the course.
Topics include:
  • Applying OOP to real-world projects
  • Creating customizable elements for a unique look
  • Learning efficient workflow practices with ActionScript 3.0
  • Developing new programming skill sets with OOP
  • Reusing custom codes across several applications

show more

author
Todd Perkins
subject
Web
software
Flash Professional CS4
level
Intermediate
duration
3h 33m
released
May 27, 2009

Share this course

Ready to join? subscribe


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:00(Music playing.)
00:05Let's take a look at what we're going to build throughout this course.
00:08We're going to learn how to build user interface elements. Now what are those elements?
00:14First we'll build a progress bar. So we'll load an external file and have a
00:19progress bar track the progress of that file loading. It will show that update
00:24on a progress bar and in a text field.
00:27We'll create a scrollbar so you can scroll a text field. We'll connect that
00:32scrollbar to a text area that will have customized scaling. We'll create user
00:39interface checkboxes and radio buttons as well as standard user interface
00:45buttons with different states.
00:48Finally, we'll take all of the user interface elements that we created and look
00:52at how they can be applied in a form application. Then I'll show you how to
00:57skin those elements so that you can customize their look and field.
01:01So that's a brief look at what we're going to be doing throughout this title,
01:05hope you're excited. Now let's get started writing some ActionScript 3.0 code
01:09to build these user interface elements.
Collapse this transcript
Using the exercise files
00:00If you are a Premium subscriber to Lynda.com or have purchased this title on a
00:04disc, then you have access to the Exercise Files for this course. Exercise
00:09Files are organized by chapters. Each chapter has the files we'll be working on for that chapter.
00:15In each movie you'll see on your screen, which files to open up so that you
00:19can follow along. For your convenience, I've saved the start and final states
00:25for each movie so you can have the finished version of the files and the
00:29starting version of the files for each movie that you watch.
00:32If you don't have access to the Exercise Files, don't worry. You can still
00:36follow along. For each application that we create, we'll write the code from
00:40scratch and I'll show you how I set up my FLA files so that you can set up yours in the same way.
00:46So let's get started!
Collapse this transcript
Prerequisites
00:00Because of the advanced nature of this title, I expect you to know a lot coming in.
00:05In this movie I want to show you what the prerequisite courses are to watch this course.
00:11First, Flash CS4 Essential Training, that just shows the basics of Flash.
00:15Basically, you need to know what movie clips are and how to organize elements
00:20in Flash. So you should be familiar with the Library, the Properties panel, etc.
00:25Second, Flash CS4 ActionScript 3.0 for Designers. This title teaches the basics
00:32of ActionScript 3.0. You should definitely be familiar with this before coming
00:36in here. If you don't have any ActionScript 3.0 experience, I recommend
00:41starting with that title, especially if you do not come from a programming background.
00:46Finally, I recommend that you have watched Flash CS4 Object Oriented
00:51Programming. This title shows advanced programming with ActionScript 3.0. Much
00:59of what we'll cover in this course assumes that you've already seen that title,
01:03and so things may be confusing if you haven't watched it.
01:06If you're new to ActionScript and are already a programmer, I recommend going
01:10straight to Object Oriented Programming. From there you'll learn the basics of
01:14the language, see how everything works and you'll learn how to apply more of
01:17those principles throughout this title.
01:19So if you have a background in Flash and ActionScript, and feel comfortable
01:24tackling User Interface elements, let's get started.
Collapse this transcript
1. Making a Progress Bar
Exploring the FLA file
00:00In this chapter we're going to build a progress bar that we can attach to any
00:05instance of the loader class that loads an external image or SWF file. We'll
00:09start off the chapter by exploring the FLA file that we'll be using.
00:14On the main Timeline in my FLA file, I have an actions layer and a layer called
00:18prog. Let's look at the code in the actions layer. So there are three lines of
00:23code already. The first line creates an instance of the loader class, the
00:27second line adds a loader to the stage and the third line loads the file flower.jpg.
00:33Instead of flower.jpg, if you don't have that file, you can use any JPG image.
00:40Obviously, just make sure it's not super huge; in other words, it's not ten
00:44times the width of the stage. Try to get something that's about the same size as your stage.
00:49Then we have the prog, which has an instance of a movie clip called Prog and
00:55instance name is prog_mc. I'm going to Double-Click Prog in the Library to
01:00enter its Timeline. So in the Prog movie clip we have four layers. So the text
01:05layer, which is a dynamic text field called prog_txt. As usual, I embedded the
01:13font and I have my font settings here. I have the font to align to the center.
01:19This is actually going to display the load progress, so 1%, 2%, etc. So I want
01:25it to be aligned to the center.
01:27Then in the next layer I have the outline, which is basically just that outline
01:31graphic that you see on the screen. Then in the mask and the main layers, if I
01:37unlock them you can see the content inside of them. The mask layer is a shape
01:41tween, just a rectangle, tweening from left and right revealing the progress
01:46bar. So if I pan to the right a little bit, you could see that. So the mask is
01:50revealing the progress bar and if I lock the mask in the main layer, you could
01:53see that in action.
01:54Now at this point, if you test the movie, you're going to see the flower image
01:59load. Obviously, you'll see that ProgBar moving in the background. But it's not
02:04controlled by ActionScript yet, so it's not really monitoring the status of the load.
02:08So once you test the movie once, you'll see that image load, and if you go to
02:13View, and now make sure that you're in the Flash Player not in Flash,
02:17View, Download Settings. I'm going to set mine to T1 and then Click Simulate
02:22Download, or you can just use the same keyboard to test the movie again. Then
02:26you'll get simulated download.
02:29So you'll wait a few seconds and then the flower image will load. Again that
02:32progress bar was just going through the 100 frame animation and not indicating the load progress.
02:39So we have our file loading properly and once your FLA file is set up, you're
02:44ready to start writing the code for the progress bar.
Collapse this transcript
Defining the ProgressBar class
00:00Now we'll begin defining the ProgBar class that will hold down the code for our
00:05progress bar. If you don't have the Exercise Files, make sure that you set up
00:10your folder structure properly.
00:12So in the same folder as your FLA file, you should have the image that you're
00:15going to load. Mine is called flower.jpg here. In that folder, you should have
00:21a folder called com and in the com folder there should be a folder called
00:24lynda. In that folder, have a folder called ui and in that folder, create the
00:29file ProgBar.as. Make sure to capitalize the P and the B. Also make sure that
00:36com, lynda and ui are all lowercase.
00:39Let's go over to Flash and inside of the ProgBar class file, we'll define the
00:45ProgBar class. The first thing we'll do is declare the package. So I'll type
00:48package com.lynda.ui, some curly braces, and inside of the curly braces, I'm
00:58going to import some code. So import flash.display.*, all those display
01:06classes, and then we'll import flash. events.*. Finally, we'll import the text
01:15field classes, so flash.text.*.
01:17Then we'll declare the class. So public class ProgBar and make sure to spell it
01:24the same as your class file which is ProgBar.as. Then we're going to extend the
01:31MovieClip class. You need to extend the MovieClip class because our progress
01:37bar movie clip has 100 frames. So you can't use Sprite in this case.
01:43So in here, I'm going to define the constructor method. So public function
01:48ProgBar, capital B and some parenthesis and curly braces and I'll declare one
01:55private property right above the constructor. So private var _loader, datatype
02:04it to a Loader, and we're just going to write a little bit of code inside of the constructor.
02:09So the first thing we're going to do is hide the progress bar. So set its
02:13visible property to false. The reason why I'm doing this is because when the
02:18ProgBar initializes, I don't want it to show. I only want it to show when it's
02:24loading a file. So I want to connect it to a loader and only show the progress
02:29bar when that loader is loading content. So before and after, it should not
02:35show. That's why we're initializing it to have its visible property set to false.
02:40On the next line, run the stop method. So type stop and some parenthesis.
02:45Now what we're doing here is making sure that the Timeline doesn't play. Now,
02:49of course, we could have this inside of the Timeline of our movie clip, but
02:53there is no point in putting just a little bit of code in there and then some
02:57other code inside of this class. You might as well have all the code inside of
03:00the class, if we're going to define a class anyway.
03:03So we have that there. Let's do File, Save. I'll go to the FLA file and connect
03:10the progress bar to the class. So I'll go to the Library, Right-Click or
03:15Ctrl-Click the Prog movie clip and then Click Properties, check Export for
03:20ActionScript, make sure you're in Advanced mode so you can see that. Then for
03:25the base class we're going to put that ProgBar class.
03:28In order to do that, we need to put the fully qualified class name which includes
03:34the package. So com.lynda.ui.ProgBar, and that's it. You should Click OK, and
03:43then you should get the definition for the class could not be found. That's
03:45good. Then no other problems. Looking good.
03:49Now at this point I should be able to test the movie and see the file load,
03:54which it does. If I push that keyboard shortcut to test the movie again, that's
03:58Command+Return on the Mac or Ctrl+Enter on the PC. It should take a few seconds
04:02for the image to show. Perfect!
04:05So what we're looking for is no errors and not seeing the progress bar. So
04:10we're good. So now we've successfully defined our ProgBar class.
Collapse this transcript
Connecting a Loader instance
00:00In this movie, we're going to look at connecting a loader instance in our FLA
00:04file to the ProgBar class. So the first thing we're going to do is take this
00:09private property and make a public version of that using Set and Get methods.
00:15Now if you're unfamiliar with Set and Get methods, I recommend watching my
00:19title on Object Oriented Programming in Flash CS4.
00:23So let's define the Set method below the constructor. So declare the method
00:28public, function set loader. In the parenthesis, receive one parameter. We'll
00:40call it l and datatype it to a Loader, : void, no return data, and some curly braces.
00:46Now if you're not familiar with Get and Set methods, basically, Set and Get
00:51methods work like properties. So, for example, you set the loader property in
00:55FLA file using the = sign. So ProgBar. loader equals some loader. Now what that
01:02does is it runs the set loader method. So, our advantage here is that we can
01:07run a huge block of code or as much code as we want whenever our property is
01:12set. That gives us an advantage to just having the loader property be public.
01:16That's what we're doing here.
01:18Again, you could find more detail about Set and Get methods in my Object
01:21Oriented Programming title. So here we have our Set method and in here the
01:26first ting we'll do is set _loader = l, and then we're going to add some event
01:32listeners to our loader. So _loader. contentLoaderInfo. Remember for loaders,
01:37you don't add the event listener to the loader itself. You add the event
01:40listener to its contentLoaderInfo property.
01:43So contentLoaderInfo. addEventListener and the first one is going to be
01:51Event.OPEN. That's when the load starts and we'll run a method called
01:59loadStarted. We'll define that in just a minute. I'll add one more event
02:04listener. So just copy and paste this line of code on the next line, change the
02:08Event to Event.COMPLETE, rename the method to loadFinished in the pasted code.
02:19Then before we define loadStarted and loadFinished, right below the Set method
02:24here we're going to define the Get method. This is for retrieving property. So
02:28public function get loader, no parameters, returns a loader, and then inside of
02:39this method just return _loader.
02:42So basically, if you wanted to, let's say, trace the value of loader from the
02:46FLA file, then the loader property will be triggered here via this Get method,
02:51since we're not setting a value, we're getting a value and we return that _loader property.
02:58Right above the set method, I'm going to define loadStarted. So private
03:03function loadStarted. Again, this is private because we don't need to access it
03:11from the main Timeline or anywhere else. So create an event with the datatype
03:16of event, :void for no return data. When the load starts, all we want to do is
03:22show the ProgBar. So visible = true. Now just copy and paste loadStarted and
03:31then change the name to loadFinished and we'll set visible = false. That's it.
03:42So I'll save the file and then go to the FLA and we'll connect the loader on
03:48the Timeline. So if you don't remember the instance name of the ProgBar, you
03:52can always Click Insert a target path. See right there? It's prog_mc. Cancel
03:56out. Right above loader.load, you always want to do this before you load the
04:01file, we'll set prog_mc's loader property equal to loader. That's that loader
04:10instance we created on line 1 in this code.
04:13So we connect the loader and load the file and the progress bar does its magic.
04:18So you test the movie, you don't see anything right now, but if you test it
04:21again, you'll see the outline of the loader for a few seconds and then the
04:24image. So obviously, we're not seeing the load progress but the loader is
04:29showing and hiding at the right time and we note it's stopping on that first frame.
04:34So we've successfully connected our loader to our progress bar and we've
04:40responded it to the Event.OPEN and Event. COMPLETE events in our progress bar class.
Collapse this transcript
Showing load progress
00:00The last thing we have to do for our progress bar is actually show the progress
00:05of the file loading. So we'll do that in this movie.
00:09The first thing I'm going to do is scroll down to the bottom of my code and
00:13above all the other event listeners in the Set Loader method, I'm going to add
00:18one more event listener to my _loader property. So
00:22_loader.contentLoaderInfo. addEventListener and the event is going to be the
00:29pre-loading event, which is ProgressEvent.PROGRESS. Then we'll run a method
00:38called fileLoading.
00:41Now, we'll define the fileLoading method. I'll do that right above the set
00:45loader method. So private function fileLoading, receive an event with the
00:51datatype of ProgressEvent, call in void, no return data and then here we'll
01:00create a variable called percent. It's going to be a number. It's going to be a
01:05value between 0 and 100, will reflect the progress of your loaded file.
01:11Now, the progress is held in this event object. So it has one property called
01:16bytesLoaded and one called bytesTotal. So you divide bytesLoaded by bytesTotal
01:22and you get the percentage. That's just the number between 0 and 1. To get the
01:28whole number between 0 and 100, we just multiply that value by 100 and round it.
01:34Here's what that code looks like. I'm going to start by writing Math.round on
01:38the outside, some parentheses. So we're going to take this whole number and
01:42round it. So the first thing we're going to do is take event.bytesLoaded and
01:46divide it by event.bytesTotal.
01:48I am going to wrap that in parentheses. So event.bytesLoaded /
01:56event.bytesTotal. Then after the closed parenthesis so I'm still inside of
02:04Math.round but outside of event. bytesLoaded / event.bytesTotal. Then I'm going
02:09to multiply that value by 100. It's going to give me my whole number between 0
02:15and 100. That will show the percentage that's loaded. And we'll use that
02:17variable on the next few lines.
02:20So go down to the next line and we'll put it inside the text field first. So
02:24prog_txt.text. That's our text field inside of that Progress Bar movie clip.
02:31I'm going to set it equal to percent +, and then I'm going to put the percent
02:38string after that. It's a little percent symbol. So we'll say 0%, 1%, 2% and so forth.
02:46So the next line. We'll gotoAndStop, and pass in percent in the parentheses. So
02:55we'll take that value between 0 and 100, translate to a frame number, and go to
03:00that frame. So if it's 0, it's just going to go to Frame 1. So we'll go to each
03:04one of the frames and as the file loads, it will show the percentage in our
03:09Progress Bar. So save this file, test the movie, remember that you have to test
03:15it again to see the load progress, and there you have it.
03:20Now, it's important to note that occasionally you might have an error. So if I
03:24keep testing the movie here like really fast, over and over again, I get this
03:27weird error that says Load never completed.
03:31Now, I just want to tell you here that you don't need to worry about that.
03:33That's just some error that happens only in the Flash Player when you're
03:38testing it in Flash. This is not going to create a problem when it goes online.
03:42This is just some kind of glitch in the Flash Player that's installed in the
03:46Flash. So totally disregard that and you'll be fine. If you absolutely do not
03:52want to see any kind of error, what you need to do is listen for this
03:56IOErrorEvent. You can look up information on that in Flash Help.
04:00So with that code written, that completes our Progress Bar. So remember that
04:06you can listen for that Event. ProgressEvent and you can get that amount that's
04:13loaded by dividing bytesLoaded by bytesTotal and multiplying that by 100,
04:19taking that number and rounding it. That's how we get the percentage, simply
04:23put it in the text field, and then go to that frame to create the Progress Bar.
Collapse this transcript
2. Creating a Scroll Bar
Exploring the FLA file
00:00In this chapter we're going to build a scroll bar that scrolls the text field.
00:05Before we do that I want to walk you through how this FLA file is set up. This
00:09is particularly important to you who don't have the Exercise Files.
00:12So on the stage here I have two layers, one called Actions and one called Art.
00:16The Actions layer currently has no code and the Art layer has two elements on
00:22the Stage. First, there is the main_txt, if you don't have this file. An
00:27optional thing to do is to embed the font.
00:30I always do that whenever I work with dynamic text fields, just because I find
00:33it renders a little bit prettier. So embed the font if you want to and then I
00:39have Show border around text and Selectable. You don't have to do that. It's
00:42just a personal preference.
00:44So here I'm going to go to Scroll Bar and if I Double-Click it, you could see
00:48what's inside. So on the top I have a button called up_btn. That's just a
00:53simple three-frame button, Up, Over and Down. The Hit stage is just the Up,
00:58Over or Down stage, since they're all the same shape and it's pretty much
01:01rectangular. So back to the Scrollbar movie clip.
01:04Then we have down_btn at the bottom. It's an instance of the same Scroll button
01:09that's at the top and we have bar_mc. That is the bar or the background of the
01:15Scroll Bar. If I Double-Click that, you'll see it's just a bitmap image. Back
01:20to the Scrollbar movie clip. Then we have drag_mc and that's just a simple bitmap image.
01:28So pretty straightforward. Basically, if you don't have the Exercise Files,
01:31you're going to need a bar for the background. Name it bar_mc. I'm go to need
01:36up_btn, down_btn and then something to drag called drag_mc. Of course, in the
01:43text for the stage called main_txt. Obviously make sure it's a dynamic text
01:48field. Once your FLA file is set up, you're ready to start coding the scroll bar.
Collapse this transcript
Creating the ScrollBar class
00:00Now we're going to look at creating the Scrollbar class. Now if
00:03you don't have the Exercise Files, I want you to pay particular attention to
00:07how the folder structure is set up.
00:10So I'm going to my Create Scrollbar Class folder and then the Start folder.
00:14If you don't have the Exercise Files, here is what you should have. You should
00:16have an FLA file called Scrollbar.fla and in that same folder I want you to
00:22make a folder called com. And in the com folder, make a folder called lynda.
00:26And in the lynda folder, make a folder called ui. Notice that they're all lower
00:30case. Make sure you have this exact same spelling as me.
00:33In the ui folder, I want you to make an ActionScript file called Scrollbar.as.
00:39So by putting it in the same package as my file, you ensure that your code is
00:43going to be exactly the same as mine. So if there are any problems, you can
00:47look to my code to make sure that you're on the right track. So get that all
00:51set up, and then I'll meet you over in Flash.
00:55So on the Stage here, we have our Scrollbar component and that's all good to
00:58go. I'm going to go to Scrollbar.as. So right now this is just a blank file.
01:03I'm going to minimize the Timeline and the other panels to get the maximum
01:07amount of room for my file here. We'll start writing the code.
01:11So first we'll declare the package. Now, you probably already know what the
01:15package is going to be because it's in com.lynda.ui. That's what we just looked
01:23at. That's the same folder structure. So we declare the package and some curly
01:27braces and then we declare the public class, Scrollbar, and we're going to
01:33extend the Sprite class. So extend Sprite and then some curly braces below that.
01:41Now since we're using the Sprite class, we're going to need to import that
01:44class. Since we're going to be using other display classes, notice we'll just
01:49import all of the display classes of flash.display.*. Remember that imports all
01:55of the classes in the particular package. In this case, the package is flash.display.
02:00While we're importing we might as well import events since we're going to be
02:04using a few different types of events. So flash.events.*. We'll also be using
02:10the Rectangle class. So flash.geom. Rectangle and the TextField class. So import
02:19flash.text.TextField. Go to import statement straightaway.
02:24Now, let's declare the constructive method. So public function Scrollbar, again
02:31you are going to make sure that you have the same spelling and casing as the
02:35name of your ActionScript file and the name of your class.
02:37Now we'll declare the properties that we're going to be using. So we'll start
02:40with two public properties. So public var value, and it's going to be a number.
02:47I'm going to instantiate that here. Notice that value is blue. So that's a
02:52reserve keyword for Flash.
02:54So people consider that a no-no to use reserved keywords. It's a little bit
02:58hard to look at. But one reason why I like to use them in certain instances is
03:04to follow the same consistency as built-in Flash element.
03:08So Flash components or the Flash scrollbar specifically has a property called
03:14value that represents 0-1 value of where the dragged element is on the
03:19scrollbar. So if I use that same value property here, then it's easer to work
03:25with because I already know how to use the built-in property.
03:28So I'm kind of being consistent here with what's built in to Flash. If you
03:32really don't like value being blue or using a reserved keyword, you can feel
03:36free to use something else. Just make sure that you consistently use something else in the code.
03:40So another public property. Call this padding, and that's going to be a number.
03:46We'll set it equal to 5 and this is going to represent some spacing for the
03:51draggable bar. We'll talk about that in another movie.
03:54Then we'll declare some private property. So private var _textField. That's a
04:02TextField. That's going to represent the text field that's going to scroll
04:06private property called Max. This is the maximum position to drag the draggable
04:12element in scrollbar. So that's going to be a number. I don't have the value
04:16yet. And private property called Min, also a number, represents the minimum
04:23draggable area for the scrollbar's draggable element and that's it.
04:27So we have our basic Scrollbar Class set up. So what we want to do is just put
04:32a trace statement in here, so we can just make sure that everything is working
04:35properly. So I'm just going to trace and in quotes I'll put Scrollbar class is running.
04:45So save this file, File, Save, jump over to the FLA, and then we're going to
04:51need to connect the Scrollbar movie clip to that Scrollbar class. So go ahead
04:56and open up the Library, and then right- Click or Ctrl-Click the Scrollbar movie
05:01clip, choose Properties, make sure you're in Advanced mode so you can see the
05:06linkage area, check Export for ActionScript and for the base class, type in
05:13com.lynda.ui.Scrollbar.
05:18One thing you want to look out for is that not just typing name Scrollbar, you
05:23need to type the full package name, which is also called the fully qualified
05:28class name which includes package.
05:30So type that in there, then you should Click OK and you should get this
05:34message, A definition for this class could not be found, Click OK. You
05:38shouldn't get any other error. If you do, check the name of your base class. If
05:42it's correct, check the name of the package in your class file, if that's
05:46correct, check the way that your folders are named to make sure that the casing
05:50and spelling is correct. So we got that.
05:53I'm going to minimize the library, and now that I connected the Scrollbar to my
05:58Scrollbar class, I'll test the movie and I should see that Trace statement in
06:01the output window. We have Scrollbar classes running. Sweet! Close this window,
06:09clear the output window, jump over to Scrollbar.as, delete the trace statement
06:15and save the file. Now, we got our Scrollbar class set up and successfully
06:21connected to the scrollbar on the stage.
Collapse this transcript
Adding drag functionality
00:00Now we are going to add drag functionality to the scrollbar. Scroll down to the
00:04constructor method and we'll set values for max and min here.
00:08So first we'll set the value for min. We'll set that equal to bar_mc.y, which
00:15is the Y position of the bar. It's the highest up the draggable element can go,
00:20and then we'll set max = bar_mc.height - drag_mc.height. So it's as far down as
00:33the draggable element can go. So we have the height of the bar minus the height
00:37of the draggable element. So that will align the bottom of the draggable
00:43element with the bottom of the bar.
00:45So let's go to the next line. Add an event listener to drag_mc. The event is
00:51going to be MouseEvent.MOUSE_DOWN. It's going to be the event where the user
00:57Clicks their mouse down, so it's going to start dragging. To run a method
01:00called dragHandle, I'll define dragHandle right below the constructor method as
01:08a private method, since we don't need to access it anywhere else other than this class.
01:12So private function dragHandle is going to receive an event with the datatype
01:17of MouseEvent, no return data, so : void same as with any event handler
01:24function, and inside of here we are going to set the drag element to start dragging.
01:29So drag_mc.startDrag and I'm going to pass in false as the first parameter,
01:36which is lock center. A value of true would lock the registration point of the
01:41draggable element to the mouse and we don't want to do that because the
01:45registration point is at the top left, so I guess it's just kind of an awkward
01:49drag if we do that.
01:50Usually, use a value of true if you are going to create some sort of custom
01:54cursor. So we are not doing that, so false here and then the next parameter is
01:59a rectangle, and this is the draggable area for the object. So we are just
02:04going to create a new Rectangle inline here.
02:06So new space, rectangle, capital R, some parentheses, and in the parentheses,
02:11we pass in the x and y coordinates for the drag and then we pass in the width
02:18and height that this can be dragged.
02:20So the first one is x, so we are going to pass in 0. Next one is y, which is
02:27going to be min and then 0 for the width. So we don't want to be able to drag
02:32it left or right, and then for the height, we want to use max. So this defines
02:37our draggable boundaries here.
02:40So I'm going to add another closed parenthesis there to close up the startDrag
02:45method. Remember we need one to close out the newRectangle method and one to
02:49close out startDrag. So right after that, semicolon and then we'll save the
02:53file and then go test it in Flash.
02:56So Save, jump over to the FLA, test the movie. Now, Click and drag, up and
03:05down, and we can drag the little draggable element, and that looks great. The
03:10only thing is when you release the mouse button it still drags. So we are going
03:14to need to stop that.
03:17So right under that line of code, we are going to add an event listener to stop
03:23dragging. Now, you may be tempted to add that event listener to drag_mc. But
03:28however if you do that, then user will have to have their mouse on the
03:33draggable element when they release it, and if a mouse is somewhere else on the
03:38stage, then the event won't be caught.
03:40So we are listening for mouseEvent.MOUSE _UP and so it needs to be on stage and
03:46not on drag_mc, or else you are going to run into glitches. So
03:50stage.addEventListener. That event is going to be MouseEvent.MOUSE_UP, and I'll
03:58run a method called stopDragging.
04:02Below the dragHandle method, we'll define another private method called
04:06stopDragging. It too wil receive an event with a datatype of Mouse event,
04:14no return data, so :void, and in the parentheses, we're going to stop dragging.
04:19drag_mc.stopDrag, no parameters and then we are going to remove that event
04:26listener from the Stage.
04:27So just copy and a paste that line of code that says stage.addEventListener
04:31that we wrote a few minutes ago and change addEventListener to
04:34removeEventListener, save the file, and then return to the FLA. Test the movie.
04:44Drag the little draggable element, release your mouse, and it stops.
04:49So it doesn't matter if we release it right on the draggable element or if we
04:53release it anywhere on the Stage. Now, we can successfully drag the scrollbar
05:02in the appropriate boundaries.
Collapse this transcript
Updating the scroll bar's value
00:00In this movie, we'll look at updating the value of the scrollbar. So as the
00:04scrollbar goes up and down, we'll broadcast a change event that will tell a
00:10value the scrollbar is at between 0 and 1.
00:14So we'll start in the dragHandle method. So we'll do this at the very bottom of
00:21that method, and add an event listener to the Stage and it's going to listen to
00:27MouseEvent.MOUSE_MOVE. So it's going to run wherever the mouse moves.
00:33We will run a function called updateValue. We'll define this right below
00:38stopDragging and updateValue will be a private method and it will receive an
00:44event with the datatype of MouseEvent. Sometimes we are actually going to want
00:48to use updateValue without triggering to MouseEvent.
00:52So what we are going to do is give this Event parameter a default value of
00:56null, right inside of the parenthesis there, and so we can run updateValue as a
01:03method without passing in anything and we won't get an error.
01:07Before we define what's in the updateValue function, I want to make sure that
01:11we don't forget to remove the event listener from the Stage. So just copy that
01:14line of code where we added the event listener to the Stage to listen for the
01:17MOUSE_MOVE event and right at the bottom of the stopDragging method, paste that
01:21code and change addEventListener to removeEventListener.
01:25MOUSE_MOVE is definitely an event that you want to make sure you stop listening
01:29for as soon as you don't need to listen for, because it weighs pretty heavily
01:33on the Flash Player to calculate code every time the mouse moves. So make sure
01:39to remove that in appropriate places in the stopDragging method.
01:42So now let's define updateValue. In updateValue, we'll set a value for that
01:46value property. Remember that when we first define this class, so we have that
01:50public property called value. It's going to be a value between 0 and 1.
01:54So when the scrollbar is all the way at the top, it's going to have a value of
01:58zero, all the way at the bottom, it's going to have a value of 1. So here's
02:03how we set that. We basically divide the position of the draggable element by
02:09the max range we can go.
02:11So the way that we get that position is we start out with some parentheses and
02:15we take the Y position of drag_mc, then we subtract the minimum position and we
02:23divide that value in the parentheses by the maximum value. Remember, the
02:27maximum value is not necessarily the maximum Y position of the draggable
02:31element, but it's the length of the drag.
02:35So what we want to do here is then kind of broadcast that the value has been
02:41updated by dispatching an event. So we use the dispatchEvent method. This is
02:47built in to Flash. Capital E for Event and in the parentheses, we are going to
02:52create a new instance of the event class.
02:54So type new space, event, capital E. In the parentheses, we are going to
02:59broadcast the changeEvent. So the changeEvent is part of the event class. So
03:03it's Event.CHANGE and basically what we are saying here is that this value has
03:10changed. So if we want to listen for respond to that changeEvent from somewhere
03:16else like our FLA file for example, we can easily do that.
03:19So let's save this file and then go to the FLA and we'll check that scroll
03:26value, select the first keyframe of that layer, and open up the Actions panel,
03:32and in the Actions panel, type scrollbar_mc. Remember that's the name of the
03:38scrollbar on the Stage. You could see that, if you Click the Insert Target Path
03:41button, and then .addEventListener and the event that we are listening for is
03:47that Change Event. So Event.CHANGE. So whenever the value changes, it's going
03:51to dispatch that Change Event that we just wrote and this event listener will trigger.
03:58So here I'll pass in handleChange. So we'll define that handleChange function
04:05right here. It's going to receive an event with the datatype of Event, capital E.
04:10No return data, and here what it is going to trace, scrollbar_mc.value, and
04:19so test the movie. And then just Click and drag the scrollbar, and you should
04:25see the value update in the output window.
04:28I will just close the Preview window here, and the Actions panel we'll check,
04:31and in the Output window, see what we've got. So there we go.
04:35Now, what I'm going to do is test it one more time. I'm going to drag it all
04:39the way to the top, and you see that it's 0 in the Output window, and I'm going
04:44to drag it all the way to the bottom, and it should be 1 in the Output window.
04:48So what I'm going to do is just scroll all the way to the bottom Output window,
04:51and see that there is 1 there. So we have appropriate values between 0 and 1
04:56and that changeEvent is dispatching properly. Nice. Return to the Actions
05:01panel, delete the code that you wrote there, and we are good to move on.
Collapse this transcript
Connecting the scroll bar to a text field
00:00The first thing that we are going to do is create public functions to handle
00:05this private property. If you watch my title on Object Oriented Programming
00:10then you know what getter and setter methods are. If you didn't watch that
00:15title, don't worry. I'll explain it here.
00:17So getter and setter methods allow you to handle properties like methods. So a
00:24SET method allows you to set properties using a method and a GET method allows
00:30you to retrieve property values using a method.
00:34So we'll declare this as public. So public function setText field, so the
00:40public property is going to be text field, whereas the private one begins with
00:45an underscore. So it's _textField.
00:47This is something that I and other programmers do is have a private version of
00:52a property, have an underscore, and the public version does not have an
00:55underscore. That way, it's easy to manage.
00:57So in the parentheses, I'm going to accept a value of tf and the datatype is
01:02going to be TextField. That's going to return nothing, so void. And then we are
01:09just going to set _textField = tf. So we set the private property there.
01:15Now, remember you are wondering why we go through all of this effort to set the
01:19property here, when we could just have textField be a public property and you
01:23would set it? Well, right now, it's actually exactly the same, so there's
01:26really no benefit to doing that, though some programmers like to have all
01:30properties private and have the retrieval and setting of property values
01:34controlled this way.
01:36I don't really carry that way, and the only reason why I use getter and setter
01:40methods is when I want a property value to set and trigger other code. So
01:45that's what we are going to do here, but we are going to that in another movie.
01:48So below this set method, we'll create a get method. So remember this is going
01:53to return that private property. So public function getTextField, no
01:58parameters, datatype it's going to return is a Text Field and it's just going
02:03to return _textField. Pretty straightforward.
02:07So let's go to File, Save and then jump over to the FLA, and go to the first
02:13keyframe of the Actions layer, open up the Actions panel. Remember that's
02:17Option+F9 if you are on a Mac or F9 if you are on a PC and we'll set
02:21scrollbar_mc.textField = main_txt.
02:28Now, if you want to check that instance name, you can always Click the Insert
02:31Target Path button, and confirm the instance name there. I'll just Click Cancel
02:35to close that window.
02:36All right, you should be able to test the movie, and though the Text Field
02:40won't scroll if you Click the Scrollbar, but you at least shouldn't get an
02:44error. So looks like everything is working properly. So close the Actions
02:48panel, jump back over to the Scrollbar Class.
02:51Now, what we are going to do is handle the scrolling of the text field in the
02:55updateValue method. So find the updateValue method and right below where we set
02:59the value, we are going to see if Text Field is defined.
03:03So if(_textField) and then what we want to do is make sure that this only gets
03:16called when the mouse is moving. So that value has to be set for event. So if
03:23there's a value for the Text Field, so in other words, if Text Field is defined
03:27&, so two ampersand events. So if there is an event and it's not set to null,
03:33so if the mouse is indeed moving and this is not triggering for some other
03:36reason, then we're going to do what's in the Curly Braces here.
03:40So here we'll set text field ScrollV property. So _textField.scrollV, that's
03:48vertical scroll and to get the proper scroll, all we've got to do is take the
03:52value and multiply that by the Text Field's maximum vertical scroll, and that
03:59will make the Text Field scroll to the right place. Because value is going to
04:02be between 0 and 1, so it's basically a percentage. We multiply that by the
04:06maximum scroll value, and that gives us the perfect range to apply the scroll
04:12to the Text Field. So _textField, make sure to capitalize the F, .maxScrollV.
04:21So this is pretty standard for setting up a percentage being applied to
04:26something. To set the proper scroll value here, we just multiply our
04:30percentage, which is going to be between 0 and 1, by the maximum value of the
04:35scroll. Now, this is actually precisely right in theory, but in practice,
04:42scaling of the Text Field and the Scrollbar can affect this.
04:48For example, let's say for some reason value is just not quite getting to 1.
04:56Something about the scaling of the scrollbar and the Y position of the
05:01draggable element makes it so value doesn't quite get to 1. And I have seen
05:06this happened when elements are scaled to not full pixels or in other words,
05:11instead of being 200 pixels high, it's 200.3 pixels high. So you can run into
05:17these errors when that happens.
05:20Now, if value doesn't get to 1 all the way, then multiplying that by
05:25textField.maxScrollV will not return the maximum scroll. Because let's say
05:31maxScrollV is 19, and any value less than 1 multiplied by 19 is going to be
05:37something less than 19.
05:39Now scrollV, if you look it up in Flash Help, you'll that it's an integer
05:44value. What that means is that it's a whole number, and any decimal values will
05:49be cut off. So anything less than 19 would be truncated to just be 18. So we
05:56wouldn't get that last line of text.
05:58Now, to make sure that never happens what I'm going to do is just round this
06:03value up using the built-in Flash method Math.ceil. So Math.ceil, and then just
06:11wrap (value * _textField.maxScrollV) and then you won't get any problems.
06:19All right, so that should fix any current or future problems that could arise
06:26with the Text Field scrolling. So let's save this file, and then go ahead and
06:31test the movie. There is the Text Field scrolling up and down correctly. So now
06:39we've successfully connected the Text Field to our Scrollbar, and it's scrolling properly.
Collapse this transcript
Using buttons for scrolling
00:00Now that we've connected our scrollbar to a text field and the text field
00:03scrolls properly when we drag the scrollbar, we are ready to set up the buttons
00:08for the scrollbar. So let's scroll down to the bottom of your code and find the
00:12public set textField method. Right below where we set the text field we are
00:17going to add event listeners to the Up and the Down button.
00:20So up_btn.addEventListener, MouseEvent. CLICK for the event type and then we'll
00:28run a method called ClickScrollButton. Make sure to capitalize S and the B. So
00:36let's copy this line of code, paste in on the next line. We'll use a same
00:40method to handle the Click for down_btn.
00:42All right, right above this method we'll define a new private method called
00:48ClickScrollButton. So private function, ClickScrollButton and that will receive
00:54an event with a data type of MouseEvent, void because there is no return data
00:59for event handlers.
01:01And the first thing we'll do in here is create a variable called percent and
01:04we'll define that later, but that's going to represent the percentage that the
01:10text field is scrolled down.
01:11So what we'll do on the next line is actually scroll the text field. Now since
01:15we are using this event handler for two buttons, up_btn and down_btn, either
01:22one of those could be Clicked to trigger this event handler. So what we want to
01:26do is use a conditional statement to check to see which button is Clicked.
01:31So if the up button was Clicked, we scroll down, the down button was Clicked,
01:34we scroll down. Now conditional statement that would check that would require
01:38approximately eight lines of code and of course, you can write in less. But
01:43what I want to show here is a shorthand way to write a conditional statement.
01:48So _textField.scrollV += and we'll write the conditional statement. So we are
01:57not going to use the word if, but we are going to use parenthesis. So we type
02:00some parenthesis and I'll put the condition in the parenthesis. So the
02:04condition is going to be event. currentTarget. Remember currentTarget holds a
02:09reference to the object that added the event listener. So this can only be
02:13up_btn or down_btn.
02:16So == up_btn and then after the parenthesis put a question mark. The question
02:25mark is kind of like saying then. So if the event.currentTarget == up_btn, then
02:32what we are going to do here is put a value of -1. So if you remember math
02:37class back in high school adding a negative number is the same thing as
02:41subtracting a positive number. So this subtracts 1 from scrollV, if the current target is up_btn.
02:49So after that what we are going to do is we are going to say what happens if
02:53the current target is not up_btn. To define sort of an else statement shorthand
03:00use a colon. So we type : 1 and there is our shorthand conditional statement
03:08condensing what have been on eight lines into one line of code.
03:12And just to recap, all you do is you put the conditional statement in
03:15parenthesis, use a question mark and after the question mark you put what
03:20happens if the statement is true, which is this -1 and then you have for the
03:25else statement a colon and then just put the value right after that.
03:29So there is your shorthand conditional statement. So let's go to the next line
03:34and we'll set the value of percent. So type percent and we'll set that equal to
03:39the percentage of the text field to scroll down. Now to figure that out we need
03:43to take where the text field is scrolled to and divide that by the text field's maximum scroll.
03:49Now we have one problem in doing that is that the minimum scroll value is 1. So
03:56we don't have this 0 beginning index. So what we want is to offset the minimum
04:01and maximum value so that we have a value, let's say the maximum was 19, so we
04:07could scroll down 19 times. Then we want to offset that from being between 1-19
04:13to 0-18. So that way the minimum percent value is 0 and the maximum is 1.
04:21So percent = and in parenthesis we'll take _textField.scrollV as its current
04:28vertical scroll -1 and then a forward slash, because we are going to be
04:33dividing and some parenthesis after that. I'm going to divide that by
04:37textField's max scrollV. So _textField. maxScrollV. That's the maximum amount of
04:44vertical scroll that we can have here. And we subtract 1 again to offset that value.
04:50So we can start at 0. So percent gives us a value between 0-1 and then we'll
04:53set the Y position of the drag movie clip. So drag_mc.y = percent. So standard
05:04thing, we take the percent and multiply by the maximum range. So percent * max
05:08and then because the maximum range as we set earlier in our code, if you will
05:13find out where we set the value of max, is just the maximum range that
05:19draggable movie clip can scroll. It's not the maximum Y position that the movie
05:25clip can scroll down.
05:26So in order to get the maximum Y position, we are going to have to add on the Y
05:30position of the bar, which is held in the min property. So scroll back down,
05:34find out where we are saying the Y position of drag_mc and then add on min. So
05:41that will put drag_mc in Y position when we Click the scroll button.
05:45Finally, what we want to do is update that value property and dispatch the
05:50change event. So all we have to do is here run the updateValue method. We don't
05:55have to pass in any kind of event or anything as remember we set an initial
05:58value for that event parameter to null. So we can run the method without
06:03passing in any parameters.
06:04All right. So that looks good. Let's save and test. So I'll Save the file, test
06:09the movie and then what we are going is Clicking the scroll buttons to scroll
06:13the text field. So we'll see it scrolls all the way down to the last line and
06:18then all the way up to the first line. You notice that drag_mc movie clip is
06:23moving as I Clicked.
06:24Nice! So now we have successfully not only connected the text field to the
06:29scrollbar, but we can scroll the text field using our buttons.
Collapse this transcript
Controlling scroll bar scaling
00:00In this movie we are going to finish our scrollbar by adding a special feature
00:04to control the scaling of the scrollbar.
00:06So let's say this text field was way too big. Let's say I want it to be
00:11smaller. So I'm going to double-Click it and then I'm going to Click-and-drag
00:14the handle to scale the size of the text field. And I'll make it approximately
00:23half the size. That looks good. Make sure about the way that you don't use a
00:27Free Transform tool when you are doing it, but you are actually double-Clicking
00:30it and changing the size of the dynamic text field, not scaling the dynamic text field.
00:35For the scrollbar we'll use the Free Transform tool. Select the scrollbar, and
00:40Click-and-drag up, make sure to hold to Option or Alt to anchor at the top as
00:45you drag from the bottom. So you have a scale to scrollbar now. And as I test
00:49the movie, it works okay, but the scrollbar is scaled and it looks a little bit
00:57pixilated and awkward.
00:58So what I'm going to do is change the code so that the size of the scrollbar is
01:04the same, but there is none of this awkward scaling. So close the Preview
01:08window, jump over to your Scrollbar class. Here go to the constructor method,
01:13select the lines of code where we set the min and max values and cut them,
01:18Command+X or Ctrl+X.
01:20Run a method called draw. This is something that we are going to define in just
01:24a moment. Notice it's a built-in method as well, and this is actually something
01:28that components use to control the scaling of themselves. So we are basically
01:34staying consistent with Flash here. So this is an instance where I prefer to
01:39use this built-in word since it kind of keeps everything standard.
01:43So in the parenthesis here I'm going to passing the width and the height of a
01:47scrollbar via the Width and Height properties. So let's define our draw method
01:52and this is going to be public, so we can run it from anywhere. It's going to
01:57receive a parameter called w. It's a number and then h, also a number, no
02:03return data. And then inside of the method the first thing we are going to do
02:07is just paste the code that we just cut.
02:10Above that code, we are going to set the scale X and scale Y properties of the
02:14scrollbar to 1. So scaleX = scaleY = 1. So on the next line we are going to set
02:24the height of bar_mc. So type bar_mc. height. That's the bar that's kind of the
02:30background for the scrollbar and this can scale vertically without giving any
02:34awkwardness at all.
02:35So we are going to set a type = h. That's the height of the scrollbar when the
02:40code initializes. So this is the scaled height minus in the parenthesis
02:44that I'm going to type up_btn.height and from there I'm going to subtract padding.
02:50So if we where at up_btn.height, it would be a line at the bottom of the up
02:56button. We subtract the padding since the up button has a little curve on a
03:00bottom. So we want to account for that, so we subtract the padding. And finally
03:04we multiply that by 2, and so it's actually following the mathematical order of
03:10operation. So the code in the parenthesis gets multiplied by 2 and not everything.
03:16So we take that full height, we multiply (up_btn.height - padding) by 2 to
03:25account for the up button and the down button. So that gives us the height that
03:29we want the bar to be. So on the next line, we'll set the Y position of the
03:34bar. So bar_mc.y = up_btn.height - padding.
03:42Remember that up_btn has a little curve at the bottom. So we want to move it up
03:47a little bit behind that so there is not any space where that curve is. So we
03:51go to the next line, now we need to set the down button's Y position. So
03:55down_btn.y = bar_mc.height + bar_mc.Y - padding. So that puts the down button
04:06in the right place. We don't need to mess with the up button, because that's
04:08always going to be at the top. So it's going to have a value of 0, so that's okay.
04:13So we set min and max, that's correct, and finally we are going to set the Y
04:19position for the draggable element to put at the top of the beginning. So
04:23drag_mc.y = min. So that its minimum position. And that's it.
04:30Basically, we just take the width and height of the object is already and we
04:34set the scaleX and scaleY to 1. Then we use the height property here to
04:39properly scale the height of the scrollbar. So save this file, test the movie,
04:46and the scrollbar is the appropriate size, but it's not scaled awkwardly like it was before.
04:52So I can go back to my FLA file and using the Free Transform tool can hold the
04:57Option on my keyboard and Click-and- drag to scale it down whatever size I want,
05:03and the scale is going to look just right. So I can still control it just fine
05:07by Clicking-and-dragging or by Clicking the buttons.
05:11So to recap what you learned in this chapter, you learned how to drag the
05:15element in the scrollbar constraining its positions, you connected the
05:20scrollbar to the text field on the stage, added the functionality to scroll the
05:24text field, and the scrollbar with the buttons, and you can control the scaling
05:29of the scrollbar, so you can scale it to whatever height you want, and it will
05:33still render properly. Good job.
Collapse this transcript
3. Creating a Text Area
Exploring the FLA file
00:00In this chapter we are going to build the TextArea user interface element. Now
00:04we've already seen how to create a scrollbar, how to combine that scrollbar and
00:09the text field. So what's different here?
00:11What we are doing in this chapter is combining the scrollbar and the text field
00:16into one element so that we can scale and have everything rendered properly. We
00:22also want to create a quick way to load external text and to display any type
00:27of text in the text field. So in this chapter we are going to do that and build
00:31this application from scratch.
00:33So on the stage here, I have two layers one called actions and one called art.
00:38Actions has no code in it. The art layer has a movie clip instance called TA.
00:43It's an instance of the TA movie clip, short for TextArea, and if I
00:47Double-Click it on its timeline, you'll see that on the stage there is a
00:51dynamic text field with no instances name. I have it with the border around the
00:56text and I have embedded all the characters.
00:58Again, because I like the way that renders better than not embedding the
01:02characters. So you can choose to do it wherever you like. And then there is a
01:05scrollbar. Scrollbar also has no instance name. It's an instance of scrollbar,
01:11which is connected to the Scrollbar class we created in another chapter. So you
01:15can see that here in that Symbol Properties window.
01:17So if you don't have the exercise files, what your need is the scrollbar from
01:22the chapter where we created the scrollbar in this TextArea, and this text
01:26field inside of one movie clip called TA, and they don't need instance names.
01:31Once you have your FLA file setup, you are ready to get started.
Collapse this transcript
Defining the TextArea class
00:00Now before we get started writing the TextArea class, I want to make sure you
00:04have all your files and folders organized properly.
00:07So here I have the Text-Area.fla. This is where I'm working in this movie and
00:12then I have a file called external.txt. You don't need to worry about that for
00:16now, it's just external text file that contains some text. In the same folder
00:20as the Text-Area.fla file I have a folder called com. Inside to that folder
00:24there is a folder called lynda, these are all lowercase, and this will need to
00:28be lowercase as well. Inside of the lynda folder I have a folder called the ui
00:33and then inside there I have two files: Scrollbar.as and TextArea.as.
00:37The Scrollbar.as file is an ActionScript class that you created in the chapter
00:43where we created the scrollbar. So you'll need that file here. You'll also need
00:48a blank ActionScript file called TextArea.as. So once you've created these
00:52files, and they are in the right folder structure, you're ready to start
00:56working. And I'm going over to TextArea.as, and we'll start writing the code to define this class.
01:02The first thing we do is declare the package. So the package is com.lynda.ui,
01:07some curly braces below that, and it will declare the class, so public class
01:14TextArea. Notice this is blue, this is also a reserved class in Flash, but
01:20because we are declaring in our own package, then it's not going to have any
01:24conflict. You can feel free to name this file differently, if you'd like, and
01:29name the class differently. But we are using something that's kind of the same
01:33as what's build in the Flash already. So I'm basically recreating it here
01:38myself. That's why I'm okay using this reserved keyword. Again, you can use it or not, it's okay.
01:44After public class TextArea, we'll extend the Sprite class. The Sprite class is
01:50basically a one-frame movie clip. So if you are not going to have multiple
01:53frames in a timeline when you are extending some class, then you might as well
01:57use the Sprite class instead of movie clip and it's going to be less memory
02:01intensive. So curly braces below that, and I'll declare the constructor method.
02:05So public function TextArea, some parenthesis, and some curly braces.
02:10Now we'll import the classes that we'll be using in this file. So I'm going to
02:15import flash.display.Sprite. On the next line I'm going to import
02:22flash.events.Event, and we are going to use the urlRequest and urlLoader
02:30classes so import flash.net.* and then finally we'll import the textField
02:36class. So flash.text.TextField.
02:39All right, now that we've imported the proper classes, we'll start creating
02:43some properties. So first we'll create the public properties. So one public
02:48property called scrollbar. And a data type is a Scrollbar. Make sure the b is
02:54lowercase. And remember Scrollbar is the Scrollbar class we created in another
03:00chapter, and this will represent the scrollbar for our TextArea.
03:05So below that create another public property, call this one textField. Again
03:10it's a reserved keyword, but since it makes sense to use something called
03:14textField to represent a text field, then I feel good about using this reserve
03:18keyword. If you'd rather use something else, that's okay too.
03:21So data type this to your textField, and now we'll create some private
03:25properties. So private var _source, this will represent the source for external
03:32text files, we'll look at loading those later on, and then we'll have another
03:36private property called _text. That's going to be a string as well. This will
03:41be text that goes inside of textField. So that's pretty much it for our TextArea class.
03:46And the first thing I'm going to do is just use a trace statement to make sure
03:49that everything is running properly. So trace TextArea class is running, and
03:56we'll save this file, jump over to the FLA and then open up the Library,
04:04Right-Click the TA movie clip, go to Properties, check our Export for
04:09ActionScript. Make sure you are in advanced mode, or else you won't see Export
04:12for ActionScript. And for the base class .com.lynda.ui.TextArea, hit OK, you
04:22should get this message that the class definition is not found. That's okay.
04:27And it looks everything is working properly. So we'll test the movie, and sure
04:32enough I get that message in the Output window; everything is running properly.
04:35So clear the Output window, go to the class file, delete the trace statement,
04:42save the file, and our TextArea class is created and running properly.
Collapse this transcript
Initializing the text area
00:00At the beginning of this chapter, we setup our FLA file without giving instance
00:05names to the dynamic text field or the scrollbar inside of the TextArea movie
00:10clip. In this movie, I'll show you how to access those elements to initialize
00:15the TextArea. So in the constructor method let's run a method called init,
00:20which we'll define right now.
00:21We will make init a private method, no return data and this is going to
00:30basically initialize the TextArea. So part of that initialization process is
00:35going to be going through all of the elements inside of this TextArea and
00:41looking for the dynamic text field, as well as the scrollbar. So we are going
00:47to create a loop that's going to loop through all of the children inside of the
00:51TextArea. So for(var i:uint = 0; i < numChildren; i++). And inside of loop
01:05write an if statement and write inside of there, if(getChildAt(i) is Scrollbar).
01:19So if this child is a Scrollbar then scrollbar = Scrollbar(getChildAt(i)). So
01:30basically we are taking some data and we are telling Flash to treat it as a
01:33scrollbar and that's because getChildAt(i) returns a display object. So we are
01:39just basically saying that, treat this object as a scrollbar so that we won't
01:43get an error. So highlight getChildAt(i), copy it, put it inside the
01:47parenthesis for Scrollbar and that's it. So copy that if statement, paste it
01:53below, and then change Scrollbar and the if statement that you just pasted to TextField.
01:59So if this child is a TextField and then we are going to set the TextField. So
02:03textField = TextField(). This is type casting again, remember, because
02:08getChildAt(i) returned the display object. So again I'm just going to copy and
02:12paste that inside the parenthesis for the TextField type caster and that's it.
02:17So these objects don't have instance names but they are still inside of the
02:21TextArea, so we can loop through that as long as there is one TextField and one
02:25Scrollbar. No problem. Now we want to make sure that this worked properly
02:29before we move forward. So what we are going to do is throw errors if there is
02:34no Scrollbar or no TextField. We are going to check to see in this if statement
02:38is Scrollbar is undefined.
02:40You can do that by just writing an exclamation point before the property. So
02:46!scrollbar, which means if no scrollbar or in other words if scrollbar is
02:50undefined, then we are going to create an error. So throw new Error and at the
02:58parenthesis just write a string out that just basically says "No scrollbar found!".
03:04That way if you are using this code later on in a TextArea and let's say you
03:10forget to put a scrollbar in there, then you are going to get this error which
03:14will basically remind you that you need to have a scrollbar inside of the
03:18TextArea. So just copy and paste that if statement right below itself. Check to
03:25see if TextField is undefined and then change ("No Scrollbar found!") in the
03:29pasted code to ("No textField found!").
03:33That's it! So we'll save the file, File, Save. Jump over to the FLA file and
03:40just to prove that it's working properly, we'll go to our ActionScript on the
03:44Actions layer and we are just going to trace ta. That's our TextArea on the
03:48stage. Again, you could check it if you Click that in certain target path
03:51buttons, you will see ta right there.
03:53I will cancel out of that. .textField. So if this gives us any kind of value or
04:00if it says object TextField then we are in business. So it looks like we had
04:05the TextField defined. So Flash found the TextField even though it didn't have
04:08an instance name and has it inside of that TextField property. So if you ever
04:13want to not have to add an instance name to something, you can use this same
04:17form that we just did to do that.
04:19So now our TextArea is initialized and it has the Scrollbar property and the
04:24TextField property, and the last thing we'll do here is just add one line of
04:29code right below that if(!textField) statement and we are going to connect the
04:33scrollbar to the text field.
04:34So scrollbar.textField = textField. So we set the scrollbar as textField
04:44property. So now the text field should scroll when you drag the scrollbar. So
04:49Save this file, and then test it out in the Flash Player, Click-and-drag to
04:56scroll and we are good. So now our TextArea is completely initialized.
Collapse this transcript
Setting text in the text area
00:00In this movie we are going to look at making a simple way to put text in the
00:05TextArea. Right now, you have to go through the textField property, so it would
00:09be TextArea.textField.text equals whatever text you want.
00:14Now although you can do that, it's not as clean as going through a property
00:18like text. So basically it would be TextArea.text equals whatever text you
00:23want. So it cuts out that text field in the middle and makes it a little bit cleaner to use.
00:28So we already have this _text private property and what we are going to do is
00:33create a public property called text via set and get methods and whenever you
00:39set that text property, it will automatically update the text field. So it will
00:43be a bit more user friendly from the main timeline. So scroll down all the way
00:49to the bottom of the code and then right below the init method, let's define a set method.
00:55So, public function set text and and we'll receive a property called t, which
01:02will be a string. No return data. And so all we'll do is set _text =t and then
01:11set the textField.text = _text and of course, we want to have a get method as
01:18well. So public function get text(), return a string and inside of this method
01:25all we'll do is return whatever values inside the text field, so return
01:28textField.text. That's it.
01:32So we'll save the file, File, Save, and then jump over to the FLA and on the
01:38main timeline, I'll select the first keyframe of the Actions layer and open up
01:42the Actions panel and set ta, remember that's the name of our text field,
01:46ta.text = "Here's some text set with text property". So test the movie, and
01:58there is your text in the text field. Nice.
02:01So now we have successfully established a quick, easy, clean way to put text inside of the text area.
Collapse this transcript
Adding text from an external file
00:00Just like in another movie where we made a quick and easy way to set text our
00:04text area, in this movie we'll create a quick and easy way to set an external
00:09text file to load into our text area. So right now I have my file external.txt
00:16open in a text editor called TextEdit.
00:19Now for those of you folks who don't have the Exercise files, you will need to
00:22create a plain text file now and put some text inside of it, preferably enough
00:26text to scroll. You can create a plain text file using TextEdit on the Mac or
00:31Notepad on the PC. So let's jump over to our ActionScript file. So we have the
00:37private var _text to set the text value and here we have the source property
00:43that's private that we'll use to set the source of the external text. Now
00:48before we set that, I want to go to the FLA file and delete the text that's
00:53currently in the text field.
00:54So I'm going to just go inside of that movie clip, Double-Click in the text
00:57field, Command or Ctrl+A to select all that text and then delete it. So the
01:02text field is still there just not the text. So back to the class file and
01:07we'll scroll down to the bottom of the code and now add some get and set
01:11methods for that source property.
01:13So the first thing we'll do is define the set method. So public function set
01:17source and the parenthesis will receive one parameter called src:String. That
01:23will represent the path to the file, no return data so :void. And the first
01:29thing we'll do in here is create a url loader.
01:31So var loader:urlLoader = new urloader (). On the next line we'll set _source =
01:43src and then we'll add an event listener to the loader,
01:47loader.addEventListener. Remember to always add your event listeners before you
01:51load the file. So Event.COMPLETE. We'll run a function called fileLoaded, we'll
01:56define that in just a moment, and then finally we make the loader and load the files.
02:00So loader.load and we'll create the new urlRequest inline right here. So load
02:06(_source)), make sure to have two closed parenthesis there, loader.load(new
02:10urlRequest(_source)). And now let's define the get method and then we'll
02:13define file load. So public function get source():String. This is right below
02:18set source and all it's going to do is just return String. And inside of the
02:22method, we are just going to return _source.
02:26All right, above set source, define a private method called
02:31fileLoaded(event:Event):void. Now since the loader variable, was a local
02:38variable for the set source method, we can't access that variable here. But we
02:43can get the loaded data through the event parameter. So that's what we'll do.
02:47So textField.text = event.target. That's going to return a reference to the
02:53loader and then .data and that will return the loaded data. So save the file
03:00and if you go to the FLA, once you go to the main timeline and go to the first
03:06keyframe in the Actions layer, open up the Actions panel and replace the code
03:10that's currently there with the following: ta.source = "external.txt".
03:20Now for those of you who don't have the exercise files, you need to make sure
03:23that this file is in the same folder as your FLA file in order for this to
03:26work. So test the movie and there is our external text inside of our text
03:33field. All we had to do is set the source property.
03:36Now I'll Click-and-drag to scroll down, you can scroll with the buttons and it
03:40looks great. So I'll close the preview window. And now you have an unbelievably
03:44easy and intuitive way to set text from the external file in your text area.
Collapse this transcript
Capturing text input
00:00Now up to this point, we looked at putting text in our text area using the text
00:05property and the source property by applying text from an external source. In
00:10this movie, we are going to change our Dynamic text field to an Input text
00:15field, and then capture the data that's put inside the text field from the user
00:21and store that in our text property.
00:24So the first thing we'll do is just Double-Click our text area on the stage,
00:28select the text field, open up the Properties panel and change the Dynamic text
00:33field to an Input text field. It is easy enough.
00:35Let's jump over to our ActionScript file. When you are capturing text input
00:40data, there is actually an event that fires off that tells you that text has
00:44been entered into a text field. That event is called text event and its part of
00:48the events package. So all I'm going to do is just change that import statement
00:52that imports flash.events.event to flash. events.* to make sure that imports that class.
00:58Now what we do is scroll down to the bottom of init method, and then we are
01:03just going to add one line. So right below scrollbar.textField = textField, we
01:08are going to type textField. addEventListener and the event is going to be Text
01:15Event.TEXT_INPUT and we'll run a method called newText. So this is when text
01:27comes in from the user, entered into the text field.
01:31So let's create a private method right below the init method and we call this
01:36newText and we'll receive an event with the data type of TextEvent. No return
01:46data, so :void and in here the first thing we are going to do is use a trace
01:52statement to check what text has been added. The way to get that text is
01:56through the event object. It has a text property that contains the last
02:01character that was put inside of the text field.
02:03So it's just going to give one character at a time. So we are tracing
02:07event.text. Save the file and then test the movie and we can just delete all
02:15the text in here, so you can just do a Ctrl+A or Command+A, and then type a
02:19character. So I'll type a, then we see the a up here in the output window. So
02:25type anything you want and you will see that text appear there.
02:27So this is going to give us one character at a time. Now I'm just going to
02:33minimize the output window, so we have some more room. So basically all we have
02:37to do is just append that character to our _text private property. So text +=
02:43event.text. Now we'll just trace(text) to make sure it's working properly.
02:49So File, Save, test the movie, delete the text that's in there, type some text
02:57and notice that it begins with null. So the reason is that we are starting of
03:04with not defining _text. So what we are going to do is just use the simple if
03:10statement right above where we set _text, so write if some parenthesis and some
03:15curly braces and in that if statement, we'll check to see if _text is not
03:21defined. So if(!_text). Then we'll just set _text = "" and that way we won't
03:30get null at the beginning of our string.
03:32So let's test the movie after we save this file, delete all the text in there
03:37again, type and then it doesn't start with null. Now let's erase the trace
03:44statement and save the file and now we have successfully captured data from the
03:51text field using the TextEvent.TEXT_INPUT event.
Collapse this transcript
Controlling text area scaling
00:00Just like we did with the scrollbar in a previous chapter, we're going to
00:04enable this text area to support scaling. So that way, I can take this text
00:10area, and using the Free Transform tool, I could scale it down to a size, say
00:15like this, and test the movie. Instead of getting this result, I would get a
00:23resized text field that's not scaled. So I'm going to close this SWF file. I'm
00:29going to go over to TextArea.as.
00:30So the first thing I'm going to do is run a method called draw, right below the
00:35init method. Again, draw is built in and it's used with components to re-render
00:43them, just like we're doing with our text area, so I think it's appropriate to
00:46use that same method name here. So in the parenthesis, we're going to pass in width and height.
00:55Then right below the constructor method we'll define a public method called
00:59draw, receive a parameter called w that will be a number, and a parameter
01:05called h that will also be a number. It won't return anything, so :void. In the
01:12curly braces, the first thing we'll do is set the scaleX and scaleY properties
01:16equal to 1. We'll do that on the same lines, so scaleX = scaleY = 1. So we're
01:23taking away the scaling but we still have w and h for those sizes that the
01:28person wanted to scale the TextArea to.
01:30So we can use those later, but we're just making sure the TextArea is not
01:34scaled at this point. So you go to the next line and type textField.width = w.
01:39That's the full width the person can scale the TextArea to. So we're going to
01:46subtract the width of the scrollbar now. So - scrollbar.width, and that's how
01:53wide the text field should be. So we'll set the text field height,
01:58textField.height = h.
02:01Now, one thing to note about a text field is that when you set its width and
02:06height properties, it doesn't scale the text field. It just changes the size of
02:11the text field. So the Font will be of the same size but the area where the
02:15text will appear, will change.
02:17So we go to the next line and we'll set scrollbar as x, so scrollbar.x =
02:22textField.width, aligning it with the right edge of the text field and
02:30scrollbar.y = 0. So we'll align it with that top edge of the TextArea,
02:38scrollbar.draw. So we'll redraw the scrollbar, passing in w and h. Then
02:45finally, we connect the scrollbar to the text field. So scrollbar.textField =
02:51textField. And that's it.
02:56So save the file, test the movie again, and now our previously, awkwardly
03:04scaled text area now looks great. So we can see that we can scroll down and see
03:10all the text in there and the scroll buttons work as well. It's the size that
03:15we wanted, that we set using the Free Transform tool in our FLA, but it's
03:20scaled appropriately by using our draw method.
Collapse this transcript
4. Creating a Checkbox
Exploring the FLA file
00:00In this chapter we're going to make a standard user interface checkbox. So
00:05you'll be able to Click on the checkbox and the check will appear, if you Click
00:08on it again, the check will disappear. We also make it so you can control the
00:11text to the right of the checkbox using ActionScript.
00:15Before we start, let's take a look at our FLA file. This is especially
00:18important for those of you who don't have this FLA file. So on the stage here,
00:23I have a layer called actions with no code in it and a layer called checkbox
00:27with three checkboxes.
00:29So, the first one is called box1_mc, second one, box2_mc, and if you want to
00:35guess what the third one is, yes, it's box3_mc. So here, let's take a look at
00:41this movie clip, they are all instances of checkbox. So let's Double-Click that
00:46Checkbox movie clip to enter its Timeline.
00:48Top layer is text so if I show and hide that, you'll see that's just the
00:52Dynamic Text field. If you select the Dynamic text field see that it's called
00:56label_txt. So I used a font and font size and colors on this. That's
01:03not very important for this exercise but if you want to copy me, feel free to do that.
01:08Always embed fonts with Dynamic Text boxes because it always renders a little
01:12bit better. So I just embed Uppercase, Lowercase, Numerals and Punctuation. So
01:18you can do that if you want, and I have none of the options checked and have it
01:22be selectable or render as HTML or show border around the text.
01:27The main thing that you need here is to name it label_txt. So that's that, and
01:32on the next layer we have selected. If I hide that, you'll see that that's the
01:37checkbox with X on it. If I select that, you'll see it's called selected_mc. So
01:41it's just a simple bitmap graphic, stored in a movie clip with an instance name of selected_mc.
01:48Now the bottom element doesn't even have an instance name, it's just a movie
01:52clip. So, if you don't have the Exercise Files, you don't have to make these
01:56bitmaps. You just make any simple artwork, just make an X, call it selected_mc.
02:02Then you'll need the textbox called label_txt.
02:05So once everything is set up in your FLA file, you're ready to start coding your checkbox.
Collapse this transcript
Defining the Checkbox class
00:00The code for our Checkbox is going to be stored in our Checkbox class, which is
00:04going to be in the ActionScript file Checkbox.as. For those of you who don't
00:08have the Exercise Files, you will need to setup your folder structure
00:12appropriately. I'm going to tab over to Finder to show you how mine is setup.
00:16So you will have your Checkbox.fla and then a folder called com in that same
00:20folder. Inside of the com folder, create a folder called lynda. Notice these
00:23are all lowercase. Inside of the lynda folder you have a folder called ui.
00:27Inside of the folder called ui, create a file called Checkbox.as.
00:32Back to Flash. All right in Checkbox.as, the first thing we are going to do is
00:36declare the package. Just like the other classes that we have created, the
00:39package is going to be com.lynda.ui. Type some curly braces and we'll import
00:46some classes that we'll be using. So we'll import first flash.display.* and
00:52then we'll import flash.events.* and then finally we are going to import flash.text.*.
01:02Then we'll declare a class, public class Checkbox, we are going to extend the
01:13Sprite class, because we had essentially one frame movie clip. So we can
01:17optimize our application by using the Sprite class instead of movie clip. So
01:21right below that some curly braces, we'll declare some private properties.
01:25So the first one is going to be _ label that will be a String that will
01:29represent the label that's on the Checkbox or in other words the text that's to
01:33the right of the Checkbox. And then another private property called _isSelected
01:39and the data type is Boolean, and we'll just initialize that to false. And I'm
01:44just doing this here just so I know for sure that it's initialized to false.
01:48Boolean properties are actually false by default anyway, but here I just want
01:53to be able to look at my code and see that I initialize it to false. So it's
01:58not 100% necessary to have this. I just have it as a quick reference for
02:01myself. So below this we'll create a public function called Checkbox, this is
02:07our constructor method, make sure to have a lower case b, curly braces below that.
02:11We are going to set a few initial properties. So the first one is buttonMode.
02:15So buttonMode = true. buttonMode is what shows the hand cursor when you have
02:22Clickable elements. So the Checkbox is going to be Clickable, we are making
02:25this whole movie clip Clickable and we are going to make the hand cursor show
02:29when you roll over that.
02:30Now by default text fields don't show the hand cursor even when you set
02:35buttonMode to true in their parent movie clip. That's because text fields show
02:40the cursor when you hover over them. To disable that functionality what you do
02:46is you set that text field's mouseEnabled property to false. So that's
02:50label_txt as a reference for a text field. .mouseEnabled = false.
02:59So that way when the whole Checkbox is Clickable. Then you will see the hand
03:03cursor when you roll over the text field and last we are going to set
03:07selected_mc. Remember that's the check for our Checkbox or the x in this case.
03:12.visible = _isSelected. So that _ isSelected is false. So we are hiding that
03:20selected movie clip at the beginning, so the Checkbox will not be selected.
03:24Okay, so everything looks good. Let's save this file, File, Save. Jump over to
03:30the FLA and then we'll connect the Checkbox to our checkbox class, test the
03:35movie to make sure we are not getting any errors. So I'm going to go to the
03:37Library. I'm going to Right-Click or Ctrl-Click Checkbox, go to Properties and
03:44then check Export for ActionScript. Make sure you are in the Advanced mode.
03:49Leave the Class at Checkbox. That's fine. That can actually be pretty much
03:52anything you want at this point. So that's not important at all really, what's
03:57important is the Base class. We want to set the base class to com.lynda.
04:02Remember this is the fully qualified class name which includes the package .ui.Checkbox.
04:09Again, the fact that the Class is as same as the Base class isn't really
04:13significant. So you just need to have the Base class right. So this could be
04:16something totally unrelated. I just happened to have the same name here. So
04:20make sure you have the Base class correct and Click OK. And you should get this
04:24warning message, unless you check Don't Show Again. Basically it says Flash is
04:27creating the Checkbox class for you, OK and that's the Checkbox class, not the Base class.
04:32You define the Base class that absolutely needs to be defined by you when you
04:37do this. The regular class does not have to be defined by you. It can be
04:41created by Flash. I just want to test the movie to make sure I'm not getting
04:44any errors and I'm not, it's not really Clickable yet, but I can hover over the
04:51Checkbox and the text and I see that hand cursor. Nice, so I'll close that
04:56window, and now our Checkbox class is defined and running properly.
Collapse this transcript
Selecting a checkbox
00:00In this movie, we'll add the interactivity to select and deselect the Checkbox
00:05with and without ActionScript. So the first thing we'll do is add and event
00:09listener to the Checkbox at the bottom of the constructor method. So
00:12addEventListener, and it will be MouseEvent.CLICK, and we'll run a method
00:19called updateSelected.
00:22So that's going to be private method. So private function updateSelected, all
00:28one word, capital S, receive an event with the data type of MouseEvent. No
00:33return data, so :void, add some curly braces and then what we are going to do
00:39is set a property that we haven't defined yet called isSelected.
00:42Notice that we defined _isSelected as a private property. We are going to use
00:46public get and set methods for a property called isSelected. So that's without
00:50the underscore, because we wanted to do certain things when the person from the
00:55FLA file selects the Checkbox using ActionScript. So what we'll do is we'll set
01:01isSelected = !isSelected.
01:09So remember the exclamation point is operator that means not, so it's going to
01:14be whatever it's not. So basically if it's selected and I Click on it, it will
01:18be deselected and if it's deselected and I Click on it, it will be selected. So
01:22let's define the public get and set method for isSelected. So public function
01:28set isSelected, and then we'll pass in a property called S that will be
01:37Boolean. No return data for set methods. And then here the first thing we'll do
01:42is we'll set _isSelected = s, so it's whatever is passed in there. Now we'll
01:49set selected_mc's visible property equal to _isSelected. So if it's selected,
01:57then we'll see the check or the x in the box. If it's not selected then we don't.
02:02So finally, we'll dispatch a change event to broadcast so the Checkbox has been
02:08updated. So dispatchEvent, some parenthesis, and in the parenthesis we'll
02:13create a new event right there. For the parenthesis for the event constructor
02:19we are going to pass an Event.CHANGE.
02:21So we are just broadcasting that the Checkbox's state changed. So now you see
02:27the effectiveness here of using the public isSelected property versus the
02:32private _isSelected property. By using the public set method here, when we set
02:37the value, we are actually taking a shortcut to writing three lines of code and
02:43condensing that down to one line of code.
02:45So when we use without the underscore, we are actually running this set method.
02:50So that saves you some code writing and keeps your code a little more
02:53efficient. And below the set method, let's define the get method. So public
02:57function get isSelected and we'll just return _isSelected. It's going to return
03:03a Boolean value and inside of the method it will just return _isSelected.
03:10Looks good, let's save the file and then test the application. You should be
03:15able to Click the Checkbox or the Checkbox Text to select and deselect the
03:21Checkbox. Looking good. All right, the next thing to do is to see if we can set
03:29the Checkbox's state using ActionScript with our public set method.
03:35So let's go to the FLA file, and then we'll go to the Actions panel and open it
03:41up. We'll set box1_mc, again, if you are not sure of the instance name, just
03:46Click that Insert Target Path button to confirm the instance name, and cancel
03:50out. So box1_mc.isSelected = true, so that should be selected. We are going to
03:59test the movie. And sure enough it is. Nice.
04:02Finally what we want to do is check to see if we can listen for that change
04:08event. So box1_mc.addEventListener and it's going to be event.CHANGE and then
04:14I'm just going to run a method called stateChanged. So run a function called
04:19stateChanged. So let's define that now. So function stateChanged and we'll
04:24receive an event with a data type of Event. :void for a no return data. And in
04:31here we'll just simply trace(box1_mc.isSelected).
04:38Test the movie. Whenever you Click on that Checkbox, you will see true when
04:43it's selected and false when it's deselected. Nice, so with that I'm just going
04:49to delete the code that we have in here, and close the Actions panel. Now
04:53return to the Timeline and with that we have a Checkbox that can be controlled,
04:59with or without ActionScript.
05:00So we can set it by Clicking on it or we can use ActionScript to set it if we
05:04want to. So we now have a Checkbox that works with mouse Clicks and
05:08ActionScript, so you can set it whichever way you like.
Collapse this transcript
Setting a label
00:00Now that we set up the functionality for selecting and deselecting the
00:03Checkbox, we are going to look at controlling the label text for our Checkbox.
00:08So the first thing we are going to do is set the label property which is going
00:12to be the public version of that private property equal to nothing. So right
00:17above the addEventListener line of code in the Constructor method, set label = "".
00:23And again we haven't set label yet. We are going to do that now.
00:26So we have our private version, which is _label, and the public version, which
00:30is label with no underscore. So we'll define that right below the get
00:34isSelected public method. So public function set label, and in the parenthesis
00:43we'll receive a value called l, be a String, void for no return data, and we'll set _label = l.
00:54On the next line, we'll take that label and put it inside of the text field. So
00:58label_txt.text = _label. Now we'll define the get method for the label. So
01:07public function get label, no parameters returns a String and I'll just return
01:15_label. That's it. So basically at the beginning we are running that label
01:22method passing in a value of nothing to initialize the Checkbox to not have any kind of value.
01:29So let's just save the file, jump over to the FLA. Now we see Checkbox Text,
01:35which is the default text for the Checkbox. Test the movie. You should see no
01:38text there. What we are going to do is set these values on the main timeline.
01:42So open up the Timeline go to the first keyframe of the Actions layer, and get
01:47the Actions panel up and now we'll set the labels for the three Checkboxes.
01:52Again, if you need help remembering the instance names, just Click the Insert
01:56Target Path button, you could see box1_ mc, box2_mc and box3_mc right there. So
02:02box1_mc.label equals, make sure you use quotes because we are working with a
02:09string, Label for box 1, I'm just going to copy and paste this and change box1, box2 and box3.
02:20So Label for box 2 and Label for box 3. Test the movie and we see the labels
02:28and the checkboxes and we can check them. Let's say you wanted to check when a
02:35checkbox is checked or what box it is or what label it is and what the value
02:40is? What you could to is add event listeners to all the boxes. So
02:45box1_mc.addEventListener(event.CHANGE, stateChanged) and then I'm going to copy
02:55and paste this line twice, for box2_mc and box3_mc, and then we'll define that
03:03stateChanged function.
03:04So function stateChanged receives an event with a data type of Event. No return
03:12data so :void and what we'll do is we'll run a simple trace statement. And what
03:18I'm going to is above the trace statement is create box variable and I'll data
03:23type it to a Checkbox. Make sure that the b is lowercase. The uppercase b is
03:28used in Flash's ui checkbox. We'll set that equal to Checkbox, lower case b in
03:34parenthesis. Basically we are type casting here. event.target.
03:38So we are just taking that target whatever box was Clicked. We are telling
03:42Flash to treat it as a Checkbox, and then hold it in that box variable. So
03:47we'll trace(box.label +, and then a colon and some quotes and a space after the
03:55colon, after the quote, it's + box. isSelected). So now we can test the movie
04:02and we Click the box and then we'll see in the Output window, the value of the box.
04:09So Label for box 1: true, and so we can Click the various checkboxes, and see
04:16them as they get updated and get all that information there. So now we can set
04:23the text for the checkbox and the Checkbox is Clickable and we can set values
04:28for it. Our checkbox is complete.
04:31So just to recap what you learned in this chapter, we looked at showing and
04:34hiding movie clips for the Checkbox. We set public and private properties using
04:40get and set methods and we successfully created a way to control the text for
04:45the Checkbox using ActionScript as well as User Interactivity.
Collapse this transcript
5. Creating a Radio Button
Exploring the FLA file
00:00In this chapter, we are going to be creating Radio Buttons. I'm going to start
00:04by showing you how I set up my FLA file. If you don't have access to this FLA
00:09file, this is especially important for you, so that you can know how to setup
00:12your files to follow along. So on this stage here I have two layers, actions
00:17and radio. The actions layer has nothing in it, not on the timeline, not in the Actions panel.
00:22The radio layer has one movie clip on the stage. It's called rg_mc and it's an
00:29instance of the movie clip called Group with the capital G. If I Double-Click
00:32that movie clip, I can enter its timeline. Inside of the Group movie clip,
00:36there is a text field called title_txt. Note that it's a Dynamic Text field
00:41with the following settings, Myriad Pro, Regular, 16 point, etcetera.
00:47Again, whenever I use a Dynamic Text field, I always embed the font, so might
00:50want to do that as well. I just find that embedded fonts give a lot more
00:55control over anti-aliasing. So then I have three Radio Button movie clips. So
01:02we have r1_mc, r2_mc and of course, r3_mc.
01:10Now before we go inside of the radio movie clip's timeline, I want to point out
01:15that there is also an actions layer inside of this movie clip and that has no
01:18code in it as well. So these are all instances of a movie clip called Radio and
01:24if you look in the Library, you will see that the Radio movie clip is Exported
01:29for ActionScript. So open up its Properties, and you could see its base class
01:33is com.lynda.ui.RadioButton. So the class is actually already defined.
01:39For those of you who don't have the Exercise Files, I'll show you how the class
01:43is setup, so you could set up yours in the same way. Note that it needs to be
01:47in this package and folder structure com. Inside of the com folder should be
01:51the lynda folder. Inside of that folder should be the ui folder. Inside of that
01:55folder, should be the RadioButton class.
01:58If don't have the access to the Exercise Files, I would do this after defining
02:02the RadioButton class which we'll look at in another movie. Just don't forget
02:05to do this. So with that I'll just Click OK and Double-Click Radio to enter its
02:10timeline. So there is a text field that says Radio Button Label. That's a
02:15Dynamic text field that's called label_txt. You can copy my settings if you
02:19would like. It's not necessary. Again I embedded the font, just a matter of
02:23personal preference, and then the top movie clip is called selected_mc. If you
02:28watched the movies about creating a Checkbox, then this is very familiar
02:31because we did the exact same thing.
02:33So if I hide that selected movie clip, then you will see the unselected movie
02:38clip which has no instance name. All right that's how our FLA file is setup. So
02:44once you have this setup in the same way you are ready to start writing the
02:47code for your Radio Buttons.
Collapse this transcript
Viewing the RadioButton class
00:00Now we'll take a look at the RadioButton class. Remember that if you don't have
00:04exercise files, this class will need to be saved in the ui folder. So inside of
00:10the same folder as your fla, you should have a folder called com, in there a
00:13folder called lynda, in there a folder called ui and in there a file called
00:17RadioButton.as. Don't worry about RadioGroup.as right now. That's just a blank text file.
00:24To make sure that these are all lower case and they are spelled just like mine
00:27are here. So back to Flash and now you might be wondering why on earth I'm not
00:32showing you how to write all the code for this. It's actually because the
00:37RadioButton class is pretty much the exact same thing as the CheckBox class
00:41which we did in another chapter.
00:43So if you have any questions about this file, I'll recommend watching the
00:48chapter on creating the check box, because a radio button is basically a check
00:52box, except for a radio button is part of a group and only one can be selected
00:57at a time. So that's really the only difference here.
01:00So I'm going to scroll down here down here and you can copy down the code or if
01:04you don't have the exercise files, I'll recommend copying the check box code
01:07and just modifying it slightly for this. So here we have our package
01:11declaration, import statements. RadioButton class extends the Sprite class
01:16because we are using a one-frame movie clip. Two private properties, _label and _ isSelected.
01:23And then in the constructor we are setting that mouseEnabled property of the
01:26text field equal to false, and that will make it so you will see the hand
01:30cursor when you roll over it, setting the selected movie clip to hide or to
01:36have it's visible properties set to false, setting buttonMode to true, label= "",
01:40so basically setting label to nothing, and adding the event listener to the
01:44RadioButton that listened for a Mouse Click to run updateSelected.
01:46Now here is the difference between this class and the check box class. In the
01:51check box class whenever you Clicked on the check box, if it was selected, it
01:54will be deselected, and vise versa. In other words if it was deselected you
01:57Click it, it will be selected. Here we're only setting isSelected = true, if
02:03the RadioButton is not selected.
02:05So you don't toggle a radio button, you just select it when it's deselected. So
02:09if it is selected, we are not doing anything. So that's the main difference
02:13here. So if you look at the set isSelected method, you will see the same thing.
02:17Whenever we run isSelected, we check to see if isSelected is true. If it's true
02:23then we'll dispatch that change event; if not then there is no need to do it.
02:28So we only want to announce when this RadioButton has been selected and not
02:32when it's been deselected. So we have the get isSelected method that returns
02:38_isSelected. That's the private version of the public property isSelected and
02:42we have the set and get label and that's pretty much it.
02:46So once you have your radio button set up, test the movie and you should see no
02:51text for the ration buttons, since we set the label property = "" inside of the
02:57RadioButton class, and you should be able to Click on the radio buttons and
03:01have them select. They shouldn't do anything when you Click them the second
03:03time and so each one should select but not deselect.
03:07All right, so that's working properly. So that's a look at the RadioButton
03:10class. Once yours is set up properly, you are ready to start defining the Radio Group.
Collapse this transcript
Defining the RadioGroup class
00:00Now we'll create the RadioGroup class that will manage a set of radio
00:04buttons. If you don't have the exercise files, make sure to create this class
00:09file in the same directory as your RadioButton class, so that it can be in the
00:13com.lynda.ui package. So let's declare that package now. It's package com.lynda.ui.
00:22Just to import the movie clip class, so import.flash.display.MovieClip and on
00:29the next line we are going to import the event class. So
00:32import.flash.events.Event. Now we'll define our RadioGroup class, so public
00:40class RadioGroup, make sure that's the same name as your files, same casing and
00:46everything, extends MovieClip. Some curly braces below that and then we are
00:53going to create the constructor method, so public function RadioGroup().
00:59Again, same spelling, casing as your class and as your file name and above the
01:06constructor method, we are going to create some properties. So first we'll do
01:11the private property, so private var allButtons. This is going to represent all
01:16of the radio buttons. That's going to be an array, and I'll just initialize it
01:20right here to an empty array. That's short hand notation open square bracket
01:24and closed square bracket.
01:25So we can go private property, called _titles, it's going to represent the
01:30title of the radio button group that will display in the title text field above
01:35all of the radio buttons. A few lines down and now we'll create some public
01:39property. So public var label:String. This will represent the label of the
01:45radio button that is currently selected.
01:47So on the next line, public var selectedIndex:uint or a positive integer.
01:55That's going to represent the current index of allButtons that is selected.
01:58Basically this will allow us to access all the radio buttons directly through
02:02this RadioGroup. So if we want to access them from the main timeline for some
02:06reason, we can do that.
02:07All right, that's it for initializing this class. So let's save the file and
02:12before testing the movie jump over to the FLA file, connect the Group Movie
02:17Clip to the RadioGroup. So you go to the Library, right Click that Group Movie
02:21Clip or Ctrl-Click. Choose Properties. Make sure you are in Advanced mode.
02:26Check Export for ActionScript, you will see that Class name is Group just like
02:30the name of your symbol. Just leave it like that and we are just going to
02:33modify the Base class here. So we are going to set it to our RadioGroup class.
02:37Again, you need to make sure that you are referencing the fully qualified class
02:41name, which includes the package name. So com.lynda.ui.RadioGroup, so Click OK.
02:49Flash should tell you that it's creating in class. That's good. Click OK.
02:55If you don't get any problems then it means that class is connected to your
02:58movie clip successfully.
03:00So test the movie and if you don't get any errors, then everything is working
03:04properly. Nice, so now we have successfully defined the RadioGroup class and
03:11we are ready to start adding some functionality to it.
Collapse this transcript
Initializing the RadioGroup class
00:00In this movie, we are going to look at initializing our RadioGroup class and
00:04basically making it work with all of the radio buttons together. So inside of
00:09our constructor method, let's add an event listener and what we want to do is
00:15listen for the event.activate event. And what that is it's going to trigger
00:20once all of the RadioGroup's child elements are ready to manipulate using ActionScript.
00:26So we want to make sure that we can work with them and they are ready to
00:30perform tasks and everything before we do anything with the RadioGroup class.
00:35So if the event is going to be Event. ACTIVATE, we'll run a method called init.
00:41We will define init as a public function init, receives an event with a data
00:47type of Event, void for no return data.
00:52Inside of the init method, we are going to loop to all the children inside of
00:56RadioGroup. So create a for loop and inside of the for loop type (var i:uint =
01:010; i < numChildren; i++) so this is going to run for all the children that are
01:09inside of the RadioGroup.
01:10So the first thing that we are going to do is create a conditional statement
01:17here. So write an if statement, and inside of the parenthesis of the if
01:21statement check to see if (getChildAt(i) is RadioButton), we are going to see
01:31if this child is a radio button.
01:38Here I use the same RadioButton class name as the class name in Flash and Flash
01:44knows the difference because I'm talking about RadioButton inside of the
01:47com.lynda.ui package. So by putting it under different package, I'm allowed to
01:51use the same class, same and that's okay.
01:52Then what we are going to do is create a variable to hold it. So var, I'm just
01:56going to call this radio, data type it to RadioButton and I'll set it equal to
02:03RadioButton. So basically we are type casting and we are saying
02:09(getChildAt(i)), which returns the display object, telling Flash treat it as a
02:13radio button and holding it in that radio variable.
02:16So go to the next line and we'll put that RadioButton inside of the allButtons
02:25array. So allButtons.push (radio) and then right below that,
02:34radio.addEventListener (Event.CHANGE, handleAllClicks).
02:40We will define that method in just a moment. Right below the for loop, I'm
02:49going to set the first radio button to be selected. So allButtons, index 0, .isSelected = true.
02:59So that first radio button will then be selected. Now we'll define the
03:03handleAllClicks method that will handle all of the changes for the radio
03:08buttons. So whenever you Click on a radio button and change the current
03:12selected radio button this handleAllClicks method is going to run.
03:16So we'll create a private function handleAllClicks(event:Event):void and in
03:30here we are going to loop through the allButtons array. So create a for (var i:
03:37uint = 0; I < allButtons.length; i++) and inside of the loop we are going to
03:48check to see if the (event.target) is allButtons index i.
03:52So if(event.target), see if event.target, which refers to whatever radio button
04:00fires that CHANGE event, is equal to allButtons index i.
04:07Notice we are not using the word is here, just like we did earlier. Is if you
04:12want to check something is class. So if getChildAt(i) is an instance of or is
04:18RadioButton. Two equal signs is saying is equal to. So is only for classes.
04:25So if event.target == allButtons, index i, then we'll set selectedIndex = i and then
04:35we'll set label = allButtons, index i, .label.
04:41Then what we'll do is have an else statement right below the if statement. So
04:45if event.target == allButtons index i, when that's the selectedIndex, right,
04:50because that means if I Clicked on that one last and it's newly selected and if
04:55not we don't want that one to selected. So we'll deselect that radio button.
04:59So in the else statement allButtons index i .isSelected = false. So we make sure that
05:08they are all deselected. Finally, we are going to dispatch the change event. So
05:14right below this for loop, type dispatchEvent(new Event(Event.CHANGE)). So when
05:29the RadioGroup changes that means a new radio button is selected. So we should
05:34then be able to access this selectedIndex and the label of the button.
05:39So let's save the file, File, Save and jump over to the FLA file. So in our fla
05:46file, we want to go inside of that RadioGroup movie clip, go to the first
05:52keyframe of the Actions layer, open up the Actions panel. Go in to Click the
05:56Insert Target Path button so I can get the instance names of my radio buttons.
06:00So if I expand rg_mc, I'll see r1_mc, r2_mc, r3_mc.
06:04We are going to set the labels for these movie clips. So I'll cancel out of
06:09that. So we have got r1_mc.label = "Radio label 1". I'll just copy and paste
06:18this twice more and then change the 1 to 2 and then to 3 on the third line and
06:27on the main timeline I'm going to listen for that CHANGE event. I'm going to
06:31check to see what the label of the RadioGroup is and it should be the appropriate label.
06:36So go to main timeline, first keyframe of the Actions layer. We go to that
06:42RadioGroup. If you check Insert Target Path, it's rg_mc. So
06:45rg_mc.addEventListener(event.CHANGE, radioChanged). To find that function now,
06:59radioChange(event:Event):void and here we'll just trace (rg_mc.label).
07:11That should display the label of the currently selected radio button. So we'll
07:16test the movie. So right now we get the first thing as Radio label 1, so we get
07:20that and Click Radio label 2 and it displays that, Radio label 3.
07:23So now it's firing off that event and I can react whenever the button is
07:28selected. So the basic functionality of our radio button is complete. We can
07:34select one at a time and we have an event that handles when the radio changes.
07:39Now whenever the current selected radio button changes, we just respond to that
07:43CHANGE event and we can get the current label or the current selectedIndex in that radio button group.
Collapse this transcript
Adding a title
00:00To finish off our Radio Buttons, we'll create a way to quickly and easily set a
00:05title for the RadioGroup using ActionScript or the main Timeline.
00:10The first thing we'll do is go inside of the constructor method and right above
00:15the addEventListener line of code, we're going to check the titles text values.
00:19So we are going to create an if statement and in there, we are going to check
00:23to see if title_txt.text-- remember that's the title of the RadioButton Groups.
00:28If you go to the FLA file, that's this text right here, Radio Group Title.
00:33So if it's defined, or in other words if it's not empty, then title =
00:40title_txt.text. Now, we have defined the private property title but we haven't
00:45defined the public version of that private property, the one without the
00:49underscore. So let's define it now.
00:50Scroll down to the bottom of your code and right below the handleAllClicks
00:55method, define public function set title. It will receive one parameter called
01:03t that's a String, no return data, so : void and in the set title method, we'll
01:10set _title = t, so whatever is passed into this method, and then we'll set the
01:16text field. So title_txt.text = _title.
01:22Now, the convenience of this is that we don't have to go through title_txt
01:26outside of the Radio Group. We just type RadioGroup.title = whatever title and
01:33all the rest of the magic happens inside of this class. So it saves you some time.
01:37So another public method, this is going to be the get method for title. So
01:41public function get title, no parameters, turns a string, and in the curly
01:48braces, just return_title. That's it. Save the file and if you go to the main
01:58Timeline in your FLA file, open up the Actions panel, delete the code that's
02:04there and we'll just trace rg_mc. Remember that's the RadioGroup on the stage.title.
02:12Test the movie, and it says Radio Group Title, just like it's reflected on the
02:17stage. So if you wanted to update that on the Timeline, you can just
02:20Double-Click the Radio Group, select the Text Field, I'll just type New Radio
02:26Title right in there. So test the movie. Now, it says New Radio Title.
02:32So if I wanted to override that by setting it with ActionScript, I can just go
02:36to the main Timeline, open up the Actions panel and right above where we set
02:41the title, type rg_mc.title = "Title set using ActionScript" and there it is.
02:56So now that we can control the title of the RadioGroup, a RadioButton group is complete, ready to go.
Collapse this transcript
6. Creating a Button
Exploring the FLA file
00:00In this chapter, we are going to build a simple button. We'll start by looking
00:04at how this FLA file is set up. This is particularly for those of you who don't
00:09have the Exercise Files.
00:11So on the Stage here, I have two layers: Actions, which neither has code nor
00:16art, and the Art layer that just has a button. The button's instance name is
00:20btn and the symbol name is Button.
00:24If I Double-Click that Button symbol in the Library, you can see that it has
00:29four layers. So there is Label layer, which is a text field, and if you Click
00:32on the text field, you can see that its instance name is label_text and it's a
00:37Dynamic text field.
00:38You can set your Font settings like mine if you'd like. Just make sure to
00:42Format your Paragraph alignment to the center. So you're always going to have
00:45the text aligned in the center of the button.
00:48So then, we have the Normal layer, which contains the up state of the button.
00:53That's a movie clip called Normal and the instance name is up_mc. Below that,
00:59we have the Over layer, with an instance name over_mc. It's an instance of the Over symbol.
01:06Finally, we have the Down layer. down_mc for the instance name and the symbol
01:12name is Down. I'll show all these layers. I'm going to Double-Click the Normal
01:17layer to enter its Timeline.
01:19I want to show you how this symbol is set up. Notice inside of here I have a
01:24layer folder with three layers. Now, inside of the layers, if I zoom in a
01:29little bit, you can see it's a bitmap graphic because it certainly gets a
01:34little bit pixilated if I zoom in, and if I hide the top one, you will see that
01:39left portion of the button disappears. If I hide the second layer, you will see
01:45that the right portion of the button disappears. So we have Normal Left, Normal
01:50Right, and then the Button layer.
01:52So these layers contain the sides of the button. Now, what we are going to do is
01:57make this button so you can scale it horizontally as big as you want and that
02:01center portion is going to scale while the end caps do not scale. That's why I
02:07set it up like this.
02:09So if you don't have the Exercise Files, just make sure to set up your files in
02:13same way. You don't necessarily have to use a bitmap graphic. You can even just
02:16use a rounded rectangle and just cut it up so that you have three different
02:20layers, and convert one of those into a movie clip. So we have left_mc here,
02:25main_mc, and then right_mc.
02:28So you just need to do that for each of the states of the button. So each of the
02:34three button states, Normal, Over, and Down, have those three instances inside
02:39of them. That's pretty much it for the FLA file. So once you have everything
02:44set up, you are ready to start writing the code for a button.
Collapse this transcript
Creating the BasicButton class
00:00In this movie, we're going to create the class file for our button. When you're
00:04creating this class file, make sure that you have the com folder in same
00:09directory as your FLA file. And in that com folder you have a folder called
00:12lynda and then ui and then the BasicButton.as file in there.
00:18In BasicButton.as, we'll start by declaring the package. So it's of course
00:22com.lynda.ui. Inside of there, we'll declare the class. So public class
00:33BasicButton extends, and we're going to extend the Sprite class. That's just
00:39because we have a one frame movie clip, so we're going to optimize the
00:42application by using Sprite as opposed to MovieClip. Remember that a MovieClip
00:47takes a little bit more memory than a Sprite, so always use the Sprite whenever you can.
00:51Let's import some classes, so import flash.display.* and then we'll import
00:59flash.events.*, and finally we'll import flash.text.*. Inside of the class,
01:10we'll declare the constructor method. So public function BasicButton, and some
01:18Curly Braces. Right above the constructor decoration, we'll declare some
01:22private properties. So private var _ label, this will represent the label of the
01:27button. It's going to be a String. Another private property, so private var
01:32allButtons. This is going to be an array of all of the buttons. So we have
01:39_label that's a String and allButtons that is an array.
01:42Now let's add some values inside of the BasicButton constructor method. So
01:47first thing we'll do is set buttonMode to true. Remember, this is going to show
01:51the hand cursor when we add an event listener to the button. There we're going
01:56to disable mouse interactivity for the label text field, so
01:59label_txt.mouseEnabled = false.
02:04If you don't do this, then you won't see the hand cursor when you roll over the
02:08text field. You don't want that because it looks kind of weird, it's awkward
02:12for a user to see the hand cursor for a second and for the hand cursor to
02:16disappear when they roll over the text on the button. So always do this,
02:19whenever you want to make a text roll over, show a hand cursor.
02:23Let's save this file and then jump over to the FLA and connect our Button
02:27symbol to this class and test the movie to make sure it's working properly.
02:32Open up the Library, right-Click or Ctrl-Click the Button symbol, choose
02:36Properties, check Export for ActionScript, and again you'll need to be in
02:40Advanced mode to see this.
02:42For the base class, we'll set that to our fully qualified class name, which is
02:46com.lynda.ui.BasicButton, Click OK. You should get one warning message that
02:55says the class could not be found. That's correct. Everything is running okay
03:00so far. Let's test the movie, and no errors. Also, you see that hand cursor
03:06when you roll over the button. Nice!
03:09So we've successfully created our BasicButton class and it is now connected to
03:14our Button symbol.
Collapse this transcript
Defining button states
00:00Now look at displaying the various button states like UP, OVER and DOWN, based
00:05on user interactivity. So we'll start by adding some event listeners in the
00:10constructor method. So right below label _txt.mouseEnabled = false, let's add an
00:15event listener. Let's pour in a note. This is going to the whole button and not
00:19to the individual states of the button. That way every mouse Click, MOUSE_OVER,
00:24MOUSE_DOWN, gets captured.
00:26So addEventListener and in the parenthesis we'll pass in MouseEvent.MOUSE_UP,
00:36and run a method called handleMouse, we'll define that method in just a minute.
00:41What we're going to do is just copy and paste this a three more times. We'll
00:47change the event each time, so we have MOUSE_UP and then we want to change _UP
00:52to _DOWN. It should still be blue and then we want ROLL_OVER and then ROLL_OUT.
01:04All right, now let's define the handleMouse method. We'll define that right
01:10below the constructor, private function handleMouse, receive an event with a
01:17datatype of MouseEvent, :void for no return data. In the Curly Braces, we're
01:23going to use a Switch Case statement instead of an If Else statement to handle
01:28what event happened.
01:29Remember the Switch Case statements are better when you have one data source,
01:35in this case the event type, and you want to do a different thing based on that
01:40one data value. So let's use a Switch Case statement. So type the word switch,
01:46some parenthesis and some curly braces. In the parenthesis we're going to put
01:50the value that we're checking, which is event.type. So this is either going to
01:53be MouseEvent.MOUSE_UP, MouseEvent.MOUSE_DOWN, etc.
01:57So in there, we're going to use Case statements to check to see the value. So
02:03case MouseEvent.ROLL_OVER, do that one first, and a colon. Now in the next line
02:12we'll define what happens when we roll over the button. Now what we're going to
02:18is actually run a method called hideAllExcept. Now we haven't defined this yet, but we'll in just a minute.
02:25In the parenthesis we're going to pass in over_mc. We're basically going to do
02:32this for each button. So on the next line, type break;. So that just means to
02:40stop processing the Switch Case statement, once this Case has been met.
02:45So let's copy those three lines we just wrote and then paste them and change
02:50the event. So ROLL_OUT for the next one and we'll change over_mc and
02:56hideAllExcept to up_mc. That's the UP state for the button. Go down below the
03:05break statement, paste the code again, change the event type to MOUSE_DOWN,
03:10hideAllExcept(down_mc), and we'll paste one last time. Change the event to
03:19MOUSE_UP. Make sure to capitalize the event name. We'll have
03:23hideAllExcept(over_mc).
03:28So when you roll over, we'll show over_ mc. When you roll out, we'll show up_mc.
03:34When you put your mouse down, we'll show down_mc, and when you put your mouse
03:39up and are still over the button, we'll show over_mc. Now we'll define the
03:44method that controls the visibility in just a minute.
03:47The last thing we need to do in the Switch Case statement is to find what
03:50happens as a default. So type default. This is basically if none of these cases
03:56are true if it's some other event, which it won't be, but we just put this as a
04:00matter of best practice. So default :. In the next line type break;. So we're
04:05not going to put anything there and that's it for that method.
04:08Now we'll define the hideAllExcept method. So we'll do that right below the
04:12handleMouse method, private function hideAllExcept. In the parenthesis we'll
04:21receive one parameter called except. That's going to be a MovieClip, :void, no
04:27return data here, and in the curly braces, we're just going to loop through all of the buttons.
04:34Now in order to loop through all the buttons, we're going to need to put all of
04:36the buttons in an Array. So I'm going to do that right above where we add the
04:41event listeners in the constructor method. So I'll type allButtons =, and then
04:47some Square Brackets. In the square brackets, we'll pass in all of the buttons.
04:52So we have up_mc, down_mc and finally over_mc.
04:59All right, let's scroll down to hideAllExcept and we'll loop through all the
05:03buttons. So for, some parenthesis and some curly braces, standard loop, loop
05:08into the Array, var i:uint = 0 for i < allButtons.length, i++. Inside of
05:18the loop, we'll set allButtons, index i, .alpha, based on a shorthand conditional step.
05:30So some parenthesis to write the condition, we're going to check to see if
05:33allButtons index i is equal to except. If so, so question mark and we'll set the
05:40alpha to 1. If not, so :, alpha will be 0.
05:45The last thing that we're going to do is scroll up to the top of the code and
05:52right below we set the allButtons Array, we'll hide everything except
05:58up_mc. So we'll have that be the initial state of the button that shows.
06:03Save the file, test the movie, test the button, and there we go.
06:09So now we've successfully set up the difference states of the button using mouse interactivity.
Collapse this transcript
Setting a label for the button
00:00In this movie, we're going to look at setting a label for the button. So scroll
00:04down to the bottom of your code in your BasicButton file and then we'll go down
00:09a few lines and create some public Set and Get methods to access that private
00:14label property as a public property.
00:17So public function set label will receive one parameter called l. That will be
00:22a String, :void for no return data. Inside of our set method, all we are going
00:30to do is set the value of _label = l, and then label_txt.text = _label, and that's it.
00:41Then right below this, we'll create the Get method. So public function get
00:45label. It's not going to receive any parameters. It's going to return a String.
00:50All we'll do in here is just return _label. That's it.
00:54So we'll save this file and then jump over to the FLA to see what we have. So
01:00go to the FLA, open up the Timeline. Go to the first keyframe in the actions
01:05layer and open up the Actions panel. In there you can check the Insert Target
01:09Path button to confirm the instance name of the button. So mine is btn. Cancel out of there.
01:13So we'll type btn.label = "Button", make sure to put the String value in
01:22quotes, and then test the movie. And there is our label on the button.
01:27So now we have successfully created a way to easily and quickly set a label for
01:35our button, using ActionScript.
Collapse this transcript
Controlling the button's scale
00:00Up to this point our button is basically the same as a button symbol that you
00:05can just create in Flash. What we're going to do now is add a special unique
00:09feature to control the scaling of the button.
00:12So now let's say I use the Free Transform tool and select the button and scale
00:18it out to, say, maybe this big. Now when I test the movie, it has a pretty
00:23awkward scale. So what we're going to do is make the button that wide when you
00:29scale it this big, but not scale the button. So in other words, keep the size
00:34of the button preserved by only scaling the middle of the button.
00:38So let's jump over to our ActionScript file. I'll minimize the Timeline to get
00:43some more room and right above the addEventListener lines of code in the
00:47constructor method, go in to run a method called draw, which we'll define in a moment.
00:52So in the draw method, I'll pass in width and height. Now the draw method is
00:57something that's built into Flash components and it's used to re-render
01:03components based on scaling.
01:05So I'm using the same name as a built-in method here for my own custom method,
01:10because it's intuitive for me to do that. If you want to use a different name
01:13here, you can feel free to do that. But since I'm doing this exact same thing
01:18that the built-in method does for components, I think, it's appropriate to use
01:22the same name here.
01:24So right below the constructor method, just declare a public method called
01:29draw. So public function draw, receive two parameters, both numbers, we'll call
01:35them w and h. No return data so :void. The first thing we'll do is set scaleX
01:43and scaleY to 1. We'll do that on one line just like this, ScaleX = ScaleY = 1.
01:51So we reset any scaling. But we still have the scaling values preserved in
01:58those w and h parameters.
02:00Now we'll loop through all of the buttons, in a Buttons array and scale them.
02:05So for, and some parenthesis and some curly braces, so basic for loop, looping
02:11through the allButtons array for our i: uint = 0; i < allButtons.length; i++.
02:22First thing we'll do is create a variable to hold the button. So var btn,
02:27datatype it to a MovieClip. We'll set it equal to allButtons index i. So remember,
02:34allButtons has all of those movie clips inside of it. So we have that btn
02:39variable, so you can easily access it.
02:43In here, we're going to set the width of the middle of each button. Remember,
02:48the buttons are broken up in the left and the right and the middle. So the left
02:53side of the buttons are the left corners, the right side has the right corner
02:57and the middle is the scalable portion of the button. So that's the portion
03:00that we want to scale. The amount that we want to scale it is we want it to be
03:04as wide as the value passed into the draw method.
03:07So the width of the middle part of the button should be w minus the size of the
03:14left and right caps. So, let's write that formula here. So, it's btn.main_mc.
03:22Remember that's the middle movie clip inside of each button, .width = w, so
03:26we're going to start there and we're going to subtract the end cap width.
03:34So, what we're going to do is just take btn.right_mc.width and multiply that by
03:412, since the left and the right end caps are of the same size. So that gives us
03:46the right scaling for the middle part of the button. And now let's just control
03:51the placement for the other parts of the button.
03:53So btn.main_mc.x = btn.left_mc, that's the left cap of the button, .x +
04:05btn.left_mc.width. So this will align the left edge of the main_mc movie clip
04:17with the right edge of the left movie clip. So basically, you'll have the left
04:23cap on the left and then immediately after that on the right is the main_mc
04:29movie clip or the middle of the button.
04:31Finally, we'll place the right end cap of the button. So btn.right_mc.x =
04:35btn.main_mc.x + btn.main_mc.width. So we're aligning the left edge of right_mc
04:52with the right edge of that main_mc or that middle part of the button. So we're
04:59doing this for each button state. That's why we have in the for loop, and that's it.
05:04So save the file, and at this point you should be able to just test the movie.
05:10To do that, we'll go back to FLA, look at the scaling of the button here, so we
05:14want to have the same width but we don't want to have that awkward scaling that we have now.
05:18So test the movie, and there we have it. Nice! So the button is just as wide as
05:23we want it to be and it scales properly. So only the middle of the button
05:27scales and the end caps stay the same size as they are originally.
Collapse this transcript
Auto-sizing the button's label
00:00Now we'll finish our button user interface element by controlling the size of
00:06the text field based on the amount of text inside of it. The first thing we'll
00:11do is go down to the very bottom and find set label method. At the bottom of
00:16the set label method run the draw method, width and height. So whenever we
00:20change the label, then we're going to run that draw method again and re-render the button.
00:28The reason why we're doing that is because we're going to set the text field to
00:32autoSize. So it will grow as it receives more text. We'll set it to autoSize in
00:38the constructor method. So find the constructor and right below where we set
00:41mouseEnabled = false, go to the next line and type label_txt.autoSize =
00:47TextFieldAutoSize.CENTER.
01:00So this will grow from the center as it receives more text. Now you could
01:05anchor it to the left to the right by changing the TextFieldAutoSize, if you
01:09want to. So for now we'll leave that TextFieldAutoSize.CENTER.
01:13Now we'll adjust the draw method. So scroll down to the draw method, and then
01:17right below our variable inside of the loop, so right below that btn variable,
01:23create an If statement. What we're going to do is check the size of the label.
01:29So if label is auto-sized to where it would be bigger than the width of the
01:33middle of the button, then we're going to change the size of the middle of the
01:38button. If not, we'll just take that size that's set on the Timeline by scaling
01:42the button, like we did before.
01:44So I'm going to wrap this next line, that btn.main_mc.width = w -
01:49btn.right_mc.width * 2, I'm going to wrap that line in some curly braces below
01:55the else statement. So I'll just tab it in and then put a Closed Curly Brace below it.
02:01So that's going to be kind of the fallback. If we don't need to adjust the text
02:04field based on the size of the label, then we'll just adjust it based on the
02:08width and the height. So we're only going to do this if the label is so big or
02:13it has so much text that it's bigger than what the middle of the button would
02:17be. So to figure that out, inside of the if statement we'll start with W. So if
02:22W < label_txt.width + btn.right_mc.width * 2.
02:35So basically, this is going to run if the label text field is bigger than what
02:40the middle of the button will be, and that's how we find it out.
02:43So if so, what we're going to do is set the width of the middle of the button
02:47equal to the width of the label. So btn.main_mc.width = label_txt.width. So
02:58we're basically getting the width from the size of the label instead of the
03:04size of w minus the end cap size, which would do if w is greater than the width
03:11of the label plus end caps.
03:13Now all we have to do is set the x position of the label. We'll do that on line
03:1842, but above the current line 42 in my code, so right after we set the x
03:23position for main_mc. So right here, we type label_txt.x =. We're going to
03:32center it with the button.
03:33Now remember the formula for centering is just taking the width of the object
03:38you want to center to, so btn.width, you divide that by 2. That aligns the left
03:43edge of the object with the center of the object you are aligning to. Then from
03:48there we just subtract half the width of the object that you're centering.
03:52So, - label_txt.width / 2, and that will center the label. That's it. So save
04:02the file. Let's go over to the FLA and then I'm going to scale the button down
04:09a little bit, so right about there. Then I'm going to write a whole bunch of
04:12text inside of the label to see if it scales properly. So "Here's a bunch of
04:20text". So test the movie and the button scales appropriately, nice!
04:25But let's say we wanted the button to be a little bit bigger than that. So if I
04:29scale it bigger on the Timeline, like yay big, then the button scales according
04:36to what I did on the Timeline and not according to the text. So we have a good
04:41amount of control over the auto-sizing of the button and changing the scale of
04:45the button however we want to on the Timeline.
04:48And with that, our button is complete.
Collapse this transcript
7. Creating a List Menu
Exploring the FLA file
00:00In this chapter we are going to build a list that is scrollable
00:03and has various items that you can select as you see here on my screen.
00:08To start out I want to walk you through my FLA file to show you how it's setup.
00:12This is especially useful for those of you who do not have access to their
00:16Exercise Files for this title. So on the stage here I have a bunch of different
00:20list items. And now if you check the Properties Inspector, when you have one
00:25selected, you could see that they're instances of a symbol called Listitem.
00:28Note that they don't have any instance names.
00:31So I'm going to go to the Library and we'll take a look at that Listitem
00:35symbol. Note that it's green indicating that it's been exported for
00:39ActionScript. If you right-Click or Ctrl-Click that symbol and choose
00:43Properties, you could see that it's connected to the com.lynda.ui.ListItem
00:49class. We'll walk through that class in a later movie.
00:53For those of you who don't have the Exercise Files, just make a mental note to
00:57make sure to connect this movie clip to the ListItem class once you create it.
01:03Inside of the Listitem movie clip we have several different layers. There is
01:08the label layer that obviously has the label. And the label is simply a dynamic
01:14text field. I call that label_txt. It has some standard font settings. As usual
01:21with a dynamic field I embedded the font, single line, Behavior, Format, Left
01:27Aligned, and that's pretty much it.
01:30On the next layer I have the over layer. This is the over state for the
01:35Listitem. It's called over_mc. It's an instance of a movie clip called Rect.
01:41And if I double-Click that, you will see it's just a shape. I'll zoom in really
01:44quick and you will see that I have a really simple shape with a white fill on
01:51the top, and then a gray in the middle or darker gray on the bottom. These are
01:54all these vector shapes and back to the Listitem movie clip. And I have the
02:00Brightness, Color Effect applied at 0. So when you rollover, there is basically
02:05no color effect. I'll hide that layer and show the up layer.
02:12Up layer has some brightness applied, 64 %. It's an instance of Rect as well and
02:17its name is up_mc. And if I hide the up layer then you will see down_mc.
02:23Brightness at -9; this is basically so that the movie clip shade is when you
02:28rollover. So I just have the same movie clip and its brightness changes whether
02:32you are not rolling over it or your mouse is over or your mouse is down. Again,
02:37all that interactivity is applied in the ListItem class, which we'll look at in a later movie.
02:43So that's how the FLA file is setup. It's pretty straightforward and I also
02:47have a movie clip called List. And if you double-Click that symbol, you can
02:51enter its timeline and in there, it's pretty simple and we'll go over in more
02:56detail in the later movie. But I have an actions layer with no code. A
03:01placeholder layer, which is just a vector shape just so we can see a
03:05placeholder graphic for the list, and then a layer called scrollbar that
03:08contains a scrollbar instance called scrollbar_mc.
03:11Let's connect it to the Scrollbar class. Now the Scrollbar class is something
03:15that we created in a previous chapter, so I recommend watching that chapter if
03:21you don't have a Scrollbar class already created. And the scrollbar symbols
03:25folder in the Library simply has all the symbols that are related to that
03:28scrollbar movie clip.
03:31So that's our FLA file. So with that FLA setup properly, you are ready to start
03:36building the List classes.
Collapse this transcript
Viewing the ListItem class
00:00In this movie we're going to walk through the ListItem class that I've already created.
00:05Now for those of you who do not have access to the Exercise Files, they'll need
00:09to create this class from scratch, but don't worry it's not as daunting as it
00:14may seem. You can actually just copy the Button class that we created in a
00:19previous chapter and modify the code to fit the ListItem class, because
00:25basically this class is the same thing as the Button class with the few additional tweaks.
00:30So I would start from there, if I didn't have the Exercise Files. Let's take a
00:34look at this. So it's part of the com.lynda.ui package. So obviously this file
00:39needs to reside in that folder. It's called ListItem, same as the file name and
00:45everything. So I'm importing all the display events and text classes.
00:51And ListItem class extends the Sprite class. So here I have one private
00:56property and four public properties. First private property is _label. That
01:02represents the label on the list item. I have a public property called
01:07allButtons. That's an array of all the different button states for this
01:12ListItem and three public properties to hold the different movie clips that
01:17represent the button states. Let's look at the constructor.
01:22So I have set buttonMode = true, mouseEnabled =false. So the label text doesn't
01:28capture any mouse interactivity. Setting the values of up, over and down
01:32properties, placing all the button states inside of the allButtons array,
01:38running a method called hideAllExcept(up _mc). We'll walk through that in just a
01:42minute, running the draw method to control scaling and adding an event listener
01:48to run once this is activated initializing this class.
01:52So we'll go to the init method since it's next down the line. So the init
01:57method just adds event listeners to this whole ListItems. So remember they're
02:02not on each of the individual button states. They are on the entire ListItems.
02:07So MOUSE_UP, MOUSE_DOWN, ROLL_OVER, ROLL_OUT. They are all running a method
02:11called handleMouse, which again we'll look out in minute.
02:15So I have a disable method that removes all the event listeners from this
02:19object, the draw method controls to scaling. It simply sets scaleX and scaleY
02:24to 1, loops through all of the children, and sets each of the child objects
02:31with x and y positions. handleMouse uses a switch case statement to check the
02:38type of event. So is it MouseEvent, ROLL _OVER, ROLL_OUT, etcetera, and based on
02:44the event that occurs a different movie clip is shown.
02:48So hideAllExcept, Over_mc, if it's ROLL OVER, up_mc, if it's ROLL_OUT and
02:54etcetera. So this may look very familiar, if you already created the Button
02:59class. So hideAllExcept does exactly what you think it would do. It loops
03:05through all of the buttons and hides everything unless the button passed in as
03:11the except parameter is equal to allButtons in xi. And again hiding is done
03:17using the alpha property and not the visible property.
03:21By setting the alpha property you don't take away any mouse interactivity
03:26setting the visible property of an object defaults disables its mouse
03:30interactivity, which we don't want to do in this case. So that way we use alpha
03:33to ensure that; that never happens.
03:36Finally, we have this set and get methods for the label property. Remember that
03:41the _label property is the private version and the regular label property is
03:47the public version that we controlling using the set and get methods.
03:50So the set methods simply stores the value passed in that private property and
03:56then sets the text in the label text field and the get method simply returns a
04:00value of the private property _label.
04:02So that's pretty much it for the ListItem class and now again if you don't have
04:06the Exercise Files, make sure to connect it to your ListItem movie clip and
04:10then I'm going to test the movie here to show you what this looks like. So I
04:13have the different labels and there is all my list items. I can roll through
04:17them, and just Click on them and you can see the various states as I Click on them.
04:25So that's a look at how the ListItem class is set up and once you have your set
04:30up properly, you are ready to move on.
Collapse this transcript
Creating the List class
00:00Now that we have our ListItem class setup, we'll look at creating the List
00:05class. The first thing I'm going to do is delete all my list items off the
00:08stage. We are going to use the List class to create list items dynamically.
00:14So what I'm going to do is open up the Library and drop an instance of my List
00:18movie clip on to the stage, you don't need to give an instance name, what you do
00:23need to export it for ActionScript, if you already have the List class setup.
00:27So we'll do that in just a minute.
00:29Let's go to the List.as file to take a look at our List class. So if you don't
00:35have the Exercise Files, the main thing that you should want to make sure to do
00:38is take this file, save it into the com.lynda.ui package. Then just define the
00:44basic scale to the class called List that extends the movie clip class.
00:50It's significant that I'm extending the MovieClip class rather than the Sprite class.
00:54One benefit that you get when you extend the MovieClip class is that you can
00:59add ActionScript to a frame inside of a movie clip, but not a Sprite. So we are
01:06going to add frame scripts into our List to create the various list items. So I
01:13definitely want to extend MovieClip here and not Sprite. So make sure to import
01:18the display and events classes, and you are ready to go.
01:22Above the constructor, I'm going to define my properties. So I'll start with a
01:26few public properties, Public var ItemClass, datatype it to a class. Now this
01:35is going to represent the Class that we are using for our list items. That way
01:40you can use whatever class you want for your list items and everything could
01:44still work properly.
01:46So let's say you wanted to create your customized class for your list items
01:50that's different from what we created with this list. You could do that using
01:54this time class property. So in the next line, I'll create another public
01:59property called items. This is going to be in an Array and just initialize it
02:05to an empty array by setting it = open square bracket, closed square bracket. We'll create another public property
02:11called currentValue, so it will represent the current label value the selected
02:17item in the list. So datatype that to a String.
02:21I will create another public property called the scrollbar. This will represent
02:25the scrollbar that controls this List. So datatype that to a Scrollbar,
02:30lowercase b. Capital B is the one that's built-in the Flash, and then we'll
02:35create some private properties.
02:36So let's go to the next line, and create a private property called _rowHeight,
02:43datatype it to a positive integer, so uints = 4, that will be the default. This
02:48represents how many rows in the list will be visible. So if you adjust this
02:53property, you can have more or less rows visible in your List.
02:59On the next line create another private property called listItemContainer and
03:06datatype it to Sprite, and this is going to be a Sprite that contains all of
03:11the list items so that we can mask them in order to control the scrolling. And
03:16speaking of mask, we'll create one more private property liMask. This will
03:22represents a mask that we'll use to mask the list items. That way we can scroll
03:27them, datatype this to Sprite and there we go.
03:32So in the constructor, I'm going to write some of our initialization code. The
03:36first thing I'll do is create a new Sprite using our liMask property. So liMask
03:42= new Sprite, don't forget the parenthesis and then we'll just add that to the
03:47stage using the addChild(liMask), and then we'll create a new Sprite for the
03:52listItemContainer, so listItemContainer = new Sprite, and we'll add that to the stage as well.
03:59So addChild(listItemContainer), and then we are going to loop through all the
04:06children. Remember that when we looked at our List movie clip that we had that
04:11shape on the stage. What we want to do is use ActionScript to remove it. That's
04:14just placeholder so that looks a little bit more pretty on the stage, and we
04:18can see what it's going to look like when we scale it, and we move it, and everything.
04:22So we are going to loop through all the children, so (var i:uint = 0; I <
04:26numChildren; i++) and in the loop, we are going to check to see if the child
04:38that i is a shape. So if(getchildAt(i) is Shape). \Then we are going to remove
04:50it. So removeChild (getChildAt(i)). So if the child is a Shape, it will remove
05:05it. That way this will get rid of any extra shapes that happened to be inside of the list.
05:10With that, our list is initialized. So we should be able to save the file. Go
05:15back to the FLA, in the Library, right- Click or Ctrl-Click the List movie clip,
05:21go to Properties, Export for ActionScript, set the base class to
05:26com.lynda.ui.List and then you should be able to test the movie and not get any
05:35errors, and also that rectangle as the placeholder should disappears. So you
05:41should just see a scrollbar. If you do, then everything is working properly. So
05:46now the basic outline of our List class is set up and running properly.
Collapse this transcript
Defining the addItem Method
00:00Now that we have defined the constructor method for our List class, we are
00:04going to add a method called AddItem. This will allow us to add list items to our list.
00:11So scroll down below the constructor, and right below the constructor we are
00:15going to declare a public method called AddItem. And we'll see one property
00:22called l, short for label. That will be a string. So no return data, so we
00:27call void, curly braces and in the method, I'm going to create a variable
00:33called item, data type it to a ListItem, and we'll set it equal to a new
00:41instance of ItemClass. Now we have to define the ItemClass in our FLA file.
00:47So we'll do that a little bit later on in this movie. So then we are going to
00:51set the Y position. So item.y = items. length, remember items is that array of
00:58all of the list items. So right now it's 0 until we add any items times
01:04item.height, and then we are going to put a label on it. So item.label = l, and
01:13then listItemContainer.addChild and passing the item. So put that item inside
01:20of the listItemContainer and finally we'll put the item inside of the items
01:25array. So items.push(item). So basically the Y position is going to update
01:31based on the length of the item.
01:32So for the first item that length is going to be 0 and that's going to be
01:37multiplied by item.height. So the Y position of the first ListItem is going to
01:40be 0 and then once length is = 1, the Y position is equal to the height, and so
01:46basically they are going to stack below each other.
01:48So that's how that works. Let's save the file and to test it, we'll go over to
01:54our FLA file, Double-Click the List on the stage to enter its timeline. So you
01:59should be at the timeline of the List movie clip, and go ahead and open up the
02:04Actions panel on frame one and the Actions layer, and in Actions panel I'm
02:10going to type ItemClass. Remember that's property that is now attached to the
02:17List, because this List is connected to our List class. Set it equal to ListItem.
02:24That's the class that we exported in our Library. So you definitely need to
02:28make sure that's exported for ActionScript and it spelled just like this. And
02:33then on the next line, I'm going to type AddItem. Remember that's our custom
02:38method that we built in our List class and I'm just going to write Item 1. Now
02:45we should be able to test the movie and then see Item 1 list item there. I can
02:50Click it just like any other list item.
02:54So that looks that it's working okay, and if you want to add a couple more, you
02:58could just copy and paste this. So Item 1, Item 2, Item 3, Item 4, test the
03:06movie, and there they are. Nice! So our list is now coming together. With our
03:13AddItem method now we can add as many items as we want to the list.
Collapse this transcript
Controlling list menu scaling
00:00Now that we can add different items to our list, we are going to
00:04look at controlling the appearance of the list including scaling using a method called draw.
00:10So we'll start in our AddItem method. So right below items.push(item) we are
00:18going to run a method called draw passing in width and height. So every time we
00:23add a list item, we are going to re- render the list to make sure that it works
00:28properly. So below that I'm going to check to see whether or not we should show
00:33the scrollbar. So if, and in the parenthesis I'm going to check to see if
00:39items.length > _rowHeight.
00:45So if the number of ListItems is greater than rowHeight, and remember that
00:49rowHeight is equal to 4. So only 4 list items are going to show at a time then
00:54we are going to show the scrollbar or else we are going to not show the
00:58scrollbar. So scrollbar.visible = true and then else scrollbar.visible = false.
01:13And if you just wanted to write the shorthand, you could take this conditional
01:17statement in the parenthesis here you are just going to copy the statement
01:19including the parenthesis and paste that over true. So if it's true, visible
01:24equals true. If it's false, then it'll be false.
01:27So actually I'm just going to delete the if and else statement and we'll keep
01:33this really condensed. Again, if this is confusing for you to look at, you can
01:38always use the if/else statement. I just prefer the condensed shorthand notation here.
01:43So let's define the draw method. I'm going to define that right about the
01:46AddItem and right below the list constructor. So public function draw and
01:52I'll have w as a Number and then h as a Number as well, :void and I'll return here.
02:01And for the draw method we'll pretty had the standard method that we have done
02:04in previous chapters. So scaleX = scaleY= 1, scrollbar = scrollbar_mc.
02:16scrollbar_mc by the way is the MovieClip name for the scrollbar in the list
02:21MovieClip name for the scrollbar in the List MovieClip in the FLA file.
02:22We will set the Y position of the scrollbar equal to 0. scrollbar.y = 0.
02:28scrollbar.x = w. That's the width of the list. So if somebody stretches it out,
02:36that's what w is going to be. Then we are going to subtract the width of the
02:40scrollbar. That's where the scrollbar should be. So w - width of the scrollbar
02:45and that will place the scrollbar exactly where it should be. At the right edge
02:49of the list items. So scrollbar. height = items index 0 .height * _rowHeight.
03:02So if rowHeight is equal to 4 and items index 0 .height equals 20, the scrollbar will
03:09be 80 high. So that will match the height of the visible rows. Moving on we'll
03:15run the scrollbar draw method. So scrollbar.draw passing in w and scrollbar.height.
03:22So remember the scrollbar has its own draw method to render appropriately. So
03:26we could stretch it out however we want. Then we are going to run that draw
03:29method, everything fixes, then we'll add the scrollbar to the state. So
03:33addChildscrollbar. This is just to make sure that the scrollbar is in front of the list items.
03:39And then we are going to draw the mask. So liMask.graphics. That's the property
03:46that controls graphics drawn dynamically using ActionScript. And the first thing
03:51you need to do whenever you are drawing a graphic is run the beginFill
03:54method that's part of the graphics property. So beginFill and in the
03:58parenthesis we just need to pass in the color of the fill which is going to be
04:02the ActionScript X, 0x000000. And then comma and then we'll pass in the alpha,
04:11which is going to be 1.
04:12So we want to have black rectangle here. The reason why I wanted to have an
04:16alpha of one, so that I can see it if the mask isn't rendering properly. Down
04:21the next line we'll draw the rectangle. So liMask.graphics.drawRect and in the
04:29parenthesis I'm going to pass in the X and Y coordinates for the rectangle so
04:34(0,0) and then the width and height. So w for the width, comma, and for the
04:41heights it's going to be items index 0 .height * _rowHeight. So as same thing as what
04:50we did for the scrollbar.
04:51And as the best practice after you draw shape you always run endFill method. So
04:58liMask.graphics.endFill, no parameters. So whenever you drawing the shape
05:07you've got to run the beginFill method to define the fill for the shape, you
05:10run that appropriate draw method and then you run the endFill method.
05:14Then we are going to have a loop to run the draw method for each of the list
05:20items. So create the basic skeleton of a loop. So it's going to be for var
05:29i:uint = 0 i < itmes.length; i++.
05:34So in the loop all we'll do is run the draw method for each item. So type
05:39items index i .draw (w - scrollbar.width, h). That will run the draw method for each
05:54list item in the items array.
05:56Finally, below the loop set the mask for listItemContainer equal to
06:00liMask. So listItemContainer.mask = liMask. And that's it. So save the file and
06:13at this point you should be able to test the movie and you should see no
06:17scrollbar and you should just see items 1 through 4.
06:22Now that's not that exciting, because we only had four items anyway. But if you
06:27add more items. So I'm going to double-Click the list in the FLA, go to my
06:31ActionScript in there, and I'm just going to add Items 5 and 6. So copy and
06:37paste item 4 twice and then item 5 and 6. So just change that code a little
06:44bit, test the movie. And now I see the scrollbar, I see items 1 through 4 only. Nice!
06:52So we now have our mask setup properly and if you go to the main time line and
06:59you use the Free Transform tool to Click-and-drag to scale the list, then you
07:05will see that; that's affected properly when you test the movie.
07:08So now I have successfully controlled the scaling of our list. So it scales
07:14properly when you scale it horizontally and we've set the appearance of the
07:19mask to render properly.
Collapse this transcript
Selecting list items
00:00In this movie we'll take a look at selecting different items in
00:03the list in firing off an event once an item is selected. So scroll down in
00:09your code and find the AddItem method.
00:12In the AddItem method, right below listItemContainer.addChild, add a new line
00:17of code. And in this line we are going to add an event listener to the item. So
00:22item.addEventListener and event is going to be MouseEvent.CLICK and we'll run
00:32the method called handleClick.
00:36The handleClick method will be a private method and we'll define it right below
00:40AddItem. So private function handleClick the event with the datatype of
00:47MouseEvent:void for no return data and inside of the handleClick method create
00:52a variable called item. And so var item data type it to a listItem. And then we
00:59are going to create a for loop. It's going to loop through all the items and
01:04it's going to make each item initialize adding all of the event listeners and
01:10then after the loop we are going to disable the list item that was Clicked.
01:15So in the loop (var i: uint = 0; i < items.length, i++) and then inside of
01:26the loop we'll set item = items index i and then we'll run the item init method.
01:33So item.init and remember all the init method does is just add all the
01:38MouseListeners so that the ListItem will react to rollover and roll out and
01:43mouse down and everything.
01:45And on the next line, we'll run the items hideAllExcept method. So
01:49item.hideAllExcept and in the parenthesis for that method pass in item.up.
01:57That's the property that holds the up state of that ListItem. So we are
02:01basically resetting all of them and below the loop we are going to disable the
02:06item that was Clicked on.
02:07So below that type item = datatype a ListItem. So ListItem
02:19(event.currentTarget). Remember that currentTarget retains a reference to the
02:25item that added the event listener. So it's going to be one of the list items.
02:31And the reason why we were doing this typecasting thing here is because the
02:34data type returned by event.currentTarget is not ListItem. It's an interactive object data type.
02:42So Flash doesn't necessarily recognize properties and methods that are unique
02:46to ListItem. So what we were doing here just telling Flash to treat it as a
02:50ListItem. On the next line we are going to run items.disable method. This is
02:54going to remove all the event listeners. That's again defined in our ListItem
02:59class. Then we'll run item. hideAllExcept passing in item.down. That will be the
03:07selected state for the ListItem.
03:08We will set currentValue = item.label and then we'll dispatch an event and the
03:17event will be a new instance of the event class and inside the new event
03:23constructor we'll pass in Event.CHANGE.
03:27And that's it for List class for now. So save the file and then test the movie.
03:34So at this point you should be able to Click on the various list items. They
03:37should stay selected when you Click them. Nice! The last thing that we want to
03:42do is check to see whether we can capture the CHANGE event data when this item
03:48changes and that currentValue.
03:52So go to the FLA and double-Click the list on the stage to enter its timeline
03:58inside of the timeline of the List movie clip select the first keyframe of the
04:02actions layer and open up the Actions panel. And in here I'm going to add an
04:07event listener and the event is going to be event.CHANGE and then we'll run a
04:12method called listChanged.
04:13So let's define that function real quick. So function listChanged event with
04:20the data type of Event:void, no return data. And then what we are going to do
04:25is just trace(currentValue). Remember we don't have to type
04:31list.addEventListener or list. currentValue, because those properties are part
04:36of the movie clip that we are inside of right now. So we can just run the
04:40AddItem method. We can just set the Item class property, etcetera.
04:44So test the movie and you should be able to Click on these and see the label
04:50for that list item when you Click on it. So I Click on that, I get Item 1,
04:54Click on Item 4, I get Item 4, Item 3 and Item 2. Nice! So now our ListItems
05:00are working properly and I'm going to comment out this trace statement so that
05:05we don't see any messages in the output window unless we want to later on.
05:09So our list now supports selecting individual items. And changing events so we
05:16can capture whenever the list currentValue changes.
Collapse this transcript
Adding scrolling to the list
00:00In this movie we'll add the functionality to scroll the list. So,
00:04let's scroll down to the draw method and then right below where we run the
00:07scrollbar's draw method I'm going to add an event listener to the scrollbar. So
00:11scrollbar.addEventListener and the event is going to be event.CHANGE. That's
00:16the event that's fire off when the scrollbar's values changes. And we'll run a
00:21method called updateListScroll. We'll define that right below the handleClick method.
00:29This is going to be a private method. So a private function updateListScroll,
00:39event with the datatype of Event:void, no return data, some curly braces and
00:46inside here we are going to set the Y position of the listItemContainer. So
00:49listItemContainer.y = scrollbar.value. That value is going to be 0 and 1.
00:59That's the percentage that the scrollbar is scrolled down * times and then
01:04in parenthesis we'll multiply this by the Y position of the listItemContainer
01:10that is at the maximum value.
01:12So in other words we are going to start with height of the list item mask. So
01:17liMask.height and we are going to subtract the height of the listItemContainer.
01:24So minus listItemContainer.height.
01:28So if you picture that in your head, height of the mask is the same as the
01:32height of the scrollbar. And from there when you subtract the height of a
01:35listItemContainer, that aligns the bottom of the listItemContainer with the
01:40bottom of the scrollbar.
01:41We set the Y position there for the listItemContainer and then on the next
01:46line. We are going to reset the mask and this is one of the gotchas of using
01:50masks with ActionScript. You actually have to reset the mask every time a mask
01:55element moves. So just make sure to do that and you will not have any problems.
01:59I am going to do that from here so listItemContainer.mask = liMask, and that's
02:07it. So save file and then test the movie and you should be able to scroll down
02:16and see all of the list items. Nice! So now our list is scrolling just like it should.
Collapse this transcript
Connecting scroll buttons
00:00The last thing that we have to do to complete the list is adding functionality
00:04to our Scroll buttons. In order to do that we're going to need to modify our
00:09Scrollbar class and add some functionality. If you don't have the Scrollbar
00:14class I recommend watching the chapter where we created the Scrollbar class so
00:19that you can see how to build this class from scratch.
00:22Here I'm going to add a private property above the other private properties.
00:27This one is going to be called _ scrollAmt, capital A, short for scroll amount.
00:34Data type that to a Number. This is going to represent what percentage the
00:40value of the scrollbar will change when you Click the Up or Down button. So
00:45we'll allow you to set that somewhere else.
00:49Now we'll create public versions of that private property. The get and set
00:54methods that we're going to use for scrollAmt are pretty much identical to
00:58those used for textField. So we'll just copy and paste the set and get methods
01:02for get textField, and just change the name to scrollAmt. Make sure to
01:08capitalize the A. So do that in both the pasted set and get method, and for the
01:14get method I'll make sure to return _scrollAmt and then in the set method I'm
01:22going to replace a line of code that sets _textField to set _scrollAmt = amt.
01:28And of course, I'm going to have to change that tf parameter and its data type
01:37to amt, short for amount, and the data type to Number with capital N.
01:42So now we set the Scroll Amount, we set that private property and the event
01:47listeners to the Up and Down buttons. And the last thing we need to do for our
01:51get and set methods is just change the return textField data to returning a
01:57Number in the get method.
01:58So now scroll up in your code, find the ClickScrollButton method, and I'm going
02:04to wrap the second and the third lines in an if statement. So right below
02:09percent, there's _textField.scrollV and then +=. So I'm typing if statement and
02:15check to see if there's a value for _textField. Wrap those two lines with the
02:21curly braces and select them both and indent them using the Tab key.
02:25So if the textField is set, that's how we're going to control the scrolling,
02:31else if and we're going to check to see if _scrollAmt is defined. And within
02:40the else if statements curly braces we're going to do basically the same thing
02:44we did for the textField, but we're going to do it specifically for the scroll amount.
02:49So here we'll set percent =, some parenthesis and then we'll type (drag_mc.y -
02:56min) / max, so I'll get the percentage that the scrollbar is down based on the
03:06draggable elements position. And then we're going to add on to it based on that
03:11scrollAmt property. So percent += and then we're going to use a shorthand
03:16conditional statement to add on.
03:18So we want to see, if we Click the Up button we want to go in a negative
03:22direction and if we Click the Down button we want to go in a positive
03:25direction. So the first thing we'll do is check to see if (event.currentTarget
03:31== up_btn) add a question mark after the parenthesis. It will be negative
03:37_scrollAmt, else, so colon, then the value will be _scrollAmt.
03:48So we're going to change the percent in the amount of that scrollAmt property.
03:53And then just to be safe, we want to keep the percentage in bounds. So we're
03:57going to have a conditional statement to check to see, if the percentage is
04:01less than zero. So if(percent < 0) then percent = 0 and we'll do an else if
04:10statement to check to see if it's greater than 1. So if(percent > 1), percent = 1.
04:21So that keeps the percentage in bounds and everything else is good for the
04:26Scrollbar class. So what you want to do is Save this file and then go over to
04:31the List Class. In the List class all we're going to do is set the Scroll
04:36Amount. So find the draw method and right below the scrollbar.addEventListener
04:42line of code, inside of the draw method, go ahead and set the Scroll Amount for
04:45the scrollbar. So scrollbar.scrollAmt = and then what I want to do for the
04:53Scroll Amount is I want to go one list item at a time. So why don't you just
04:57scroll in the Amount of a list item.
05:00Now how on earth do you get that number? All you do is you take one and you
05:05divide it. So I'm going to take 1 and we are going to divide it by a number in
05:09parenthesis. So type some parenthesis and I'm going to divide it by the number
05:12of total items, so items.length - _rowHeight.
05:18So the scrollAmt is going to be 1 divided by the number of rows that are
05:27hidden. So we start with total list items that we have, and we subtract the
05:32visible rows then we get the number of rows that are hidden. So by dividing one
05:37by that number, the Scroll Amount will make the list items scroll. When you
05:42Click the Scroll buttons reveal one item at a time.
05:45So save the file, File, Save and then test the movie and Click the Scroll
05:52buttons, to confirm that it works. So we see one new list item at a time, so
05:57here we reveal five and six as we Click down and then two and then one as we
06:02Click the Up button. Nice, so now our list is complete, good job.
Collapse this transcript
8. Building a Dropdown Menu
Exploring the FLA file
00:01In this chapter, we're going to build the dropdown menu. Dropdown menu is
00:05basically a list that's connected to a button. To start out, I want to show you
00:09how I set up my FLA file. So on the stage here I have my Dropdown movie clip
00:16and if you Double-Click it, you'll see that inside of there you have a list box
00:21with an instance name of li and this is the list that we've build in a previous
00:26chapter. So of course, it's connected via Library to the List and ListItem
00:32classes and then we have a button that's connected to our BasicButton class
00:38that we have created in another chapter.
00:40Apart from that, the only thing here is the arrow movie clip and if I zoom in,
00:46you can see it a little bit better. So I'm going to use the Magnifier tool,
00:50keyboard shortcut M and zoom in a little bit. And see this little arrow out
00:54here, it has an instance name of arrow _mc and if I Double-Click you can see
00:58it's just a simple shape. So I have this clear rectangle on the background and
01:03then I have some gray semitransparent shapes here. So it's pretty straightforward.
01:09In the ActionScript I only have one little line of code that says
01:14arrow_mc.mouseEnabled = false. Basically that just means that this arrow won't
01:20capture any mouse interactivity, so when you roll over it, it won't steal any
01:26thunder from the button that's behind it. So we essentially disabling all of
01:30its mouse interactivity. And that's really all there is to it. At this point I
01:35don't have the dropdown menu connected to any class via ActionScript, so we'll do that later.
01:41So once you have your FLA file set up like mine, you are ready to start writing
01:45the code for your dropdown menu.
Collapse this transcript
Creating the Dropdown class
00:00We'll start creating our dropdown menu by defining our Dropdown class. So here
00:06I'm in a file called Dropdown.as. So if you don't have the Exercise Files, make
00:11sure to create that file, capitalizing D at the beginning of Dropdown and make
00:17sure to save this file in the com.lynda.ui package.
00:22So the first thing we'll do here is declare the package, package com.lynda.ui,
00:27some curly braces. Import the classes we're going to be using. So,
00:32flash.display.*. We'll import flash. events.* and flash.text.*. We are also
00:44going to import the classes not built into Flash. That's a third party class
00:50called TweenLite. So import gs.*.
00:55Now for those of you who don't have access to the Exercise Files, you will need
00:59to download TweenLite in order to use this exercise, because we're using
01:04TweenLite for the animation of our dropdown menu.
01:07You can download TweenLite at tweenlite.com. And when you download TweenLite
01:16you're going to see a folder called gs. I want you to copy the gs folder into
01:20the same directory as your FLA file. For more details about TweenLite, you can
01:25see the other titles where I've shown how to use it in detail, like the Object
01:30Oriented Programming title and the Creating Games for the Wii title.
01:34So below that I'm going to declare that my class, so public class Dropdown
01:41extends MovieClip and again I'm extending the MovieClip class because I'm going
01:46to be using Frame Scripts. So since I'm going to be putting code on the
01:50timeline for my dropdown menu, I need to extend MovieClip class and not the
01:55Sprite class, because the Sprite class does not support that feature.
01:58And now the Constructor method, public function Dropdown(), curly braces, and
02:05for now we won't put anything in that. Now let's declare a property. So a
02:08public property called list, datatype that to List, capital L, public property
02:15called button, all lowercase, datatype that to of BasicButton.
02:20So you'll definitely need the BasicButton class in your com.lynda.ui package
02:26for this project as well as the List class of course and the ListItem class for
02:31that matter. For more information about those classes you can see the previous
02:35chapter, where we created the List and List items.
02:38Other public property called buttonSpace, capital S, datatype it to a Number
02:43and set it equal to 3. This represents some spacing that we'll use for the
02:48button. We'll talk about more at this later. And some private properties. So
02:52private var _currentValue, capital V, datatype that to a String. Private
03:01property isDown, capital D, datatype that to Boolean. And finally a private
03:08property called listMask, capital M, datatype that to a Sprite. This is going
03:14to be the mask object that masks the entire list.
03:20That's it for creating our class and our properties. So let's save this file
03:24and connect it to our dropdown menu in FLA. So go to Library in FLA,
03:30Right-Click or Ctrl-Click the Dropdown movie clip. Choose Properties, check the
03:36Export for ActionScript box and for base class we'll connect it to
03:40com.lynda.ui.Dropdown.
03:45Always remember that you need to use the fully qualified class name, which
03:48includes the package here. So we have that Click OK, and the definition for the
03:53class cannot be found. That's okay. Test the movie and we don't get any errors,
04:02so everything is working properly. Nice, so now our Dropdown class is created
04:08and successfully connected to the movie clip on the stage in our FLA file.
Collapse this transcript
Initializing the Dropdown class
00:00Now we'll look at initializing our dropdown menu. The first thing I'm going to
00:04do is adjust the text alignment for our button. When we initially created our
00:10basic button in a previous chapter, we made it so that the button aligns to center.
00:17Here what we are going to do is change the basic button class to support any
00:21type of alignment. The first thing I'm going to do is make sure that my text
00:26field on my button is aligned to left. So I'm inside of my Button movie clip
00:30that's inside of the dropdown and I'm going to select that label field and to
00:35the Properties Inspector I'm just going to confirm that my paragraph alignment
00:39is left. Then go to the BasicButton class.
00:43In the BasicButton class go down to the draw method. In the draw method find
00:50the part where the label text field's X position is set and here half of the
00:56width of the button minus half the width of the field.
00:59What we are going to do is delete that set value and change it to be
01:04btn.main_mc.x. So it's going to align with the left edge of that main movie
01:11clip. And then what we are going to do is make the label text field the full
01:17width of the button minus the end caps. So right below that type
01:20label_txt.width = btn.main_mc.width.
01:28And that's it. So we'll save the file and then move over to Dropdown.as. In the
01:35Dropdown file, go to the constructor and add an event listener to listen for
01:41the event.ACTIVATE event. So addEventListener and the event is going to be
01:47event.ACTIVATE, and that occurs when all of the elements inside of this movie
01:52clip are drawn and ready to access using ActionScript or run a method called init.
01:58Now we'll define the init method as public. So public function init and for the
02:07parameter it's going to receive an event with the data type of event and in
02:10case you want to run this anywhere else, put this initialize that to be null.
02:14So that you can run this as a regular method without an event needing to take
02:18place, :void, so no return data.
02:22And inside of the init method loop through all through all the children and set
02:27the list item and button properties. So create a for loop, for(var i:uint = 0;
02:34i < numChildren; i++). As the first order of business in the loop, create the
02:47skeleton of an if statement and check to see if(getChildAt(i) is list).
02:57So if it's a list then what we'll do is just set the value for that list
03:01property. So list = List(getChildAt(i)).
03:11Remember getChildAt returns a display object and this right here, List,
03:18basically tells Flash to treat that display object as a list so that the Flash
03:22doesn't throw any errors. So right below that if statement, create another if
03:28statement. And we want to see if(getChildAt(i)) is a BasicButton.
03:33So if(getChildAt(i) is BasicButton). Then we are going to set the value of
03:41button. So button = and of course we have type cast again, because getChildAt
03:46returns a display object. So button = BasicButton(getChildAt(i)). Then we'll
03:57set the auto size property for the button.
03:59So button.label_txt. That's instance name of the label inside of the button,
04:04button.label_txt.autoSize = TextFieldAutoSize.NONE. So no auto-sizing for the
04:18textField. This is always going to be the width of the button. Now we'll set
04:21the label for the button.
04:23So button.label = "Choose..." to indicate that the person should choose an
04:33option from the dropdown menu and then arrow_mc. Remember that's the name of
04:39the arrow for the dropdown menu, so we are just going to position it to the
04:42right edge of the button.
04:43So arrow_mc.x = button.width - arrow_mc. width and then below our loop add just
04:56a little bit more code to this method. And all we are going to do is set up the
04:59mask. So list.y = button.height - list. height, so we are aligning the bottom
05:09edge of the list with the bottom edge of the button.
05:11Then we are going to subtract buttonSpace. Now this buttonSpace property will
05:17allow us to control any spacing that we may want in between the list and the
05:25button. The reason why I added this is because when I created this I had a few
05:30spacing issues with the button and the list and so by adding this property I
05:36could control that as much as I wanted.
05:38So here you may or may not need to set a value for buttonSpace. You may just
05:45have buttonSpace to be zero. But by having it here we can control space between
05:49the list and a button. Next we'll set listMask = new Sprite and add it to the
05:57stage. So addChild(listMask).
06:01Next, we'll draw a rectangle. So listMask.graphics.beginFill, black, so
06:11(0x000000,1). It's a black rectangle so we could see it, in case it doesn't
06:17work. Then listMask.graphics.drawRect and in the parenthesis we would pass in
06:26the x and y position which is going to be 0 for the x and the y position is
06:31going to be button.height - buttonSpace. So it's going to align with a bottom of the button.
06:39Again, buttonSpace is just buffer. Then the width is going to be list.width and
06:45the height of the rectangle is going to be list.scrollbar.height.
06:52Finally, we'll end that fill. So listMask.graphics.endFill and then set the
06:59mask. So list.mask = listMask.
07:04All right, so that's kind of a lot but we'll save the file and test it out just
07:10to make sure everything is working properly. So you should see the text aligned
07:15to the left on the button, which it is. Great. The arrows on the right. Nothing
07:20happens when I Click the button, which is okay. And the list is hidden by the mask.
07:25So now we have successfully initialized our dropdown menu.
Collapse this transcript
Finishing the Dropdown class
00:00Now we'll finish our dropdown menu by adding the animation of the list coming
00:05down when you Click the button and by firing off a CHANGE event when you select
00:10a different item in the list.
00:11So the first thing we'll do is scroll down into the init method and for the
00:18list, right inside of that if statement where we check to see if getChildAt(i)
00:22is a list and we set the value of the list. We go right below there and add an
00:26event listener to the list.
00:27So list.addEventListener and event is going to Event.CHANGE so that whenever a
00:32different item in the list is selected, we'll run a method called
00:36toggleDropdown. This is going to, basically, signal that the dropdown menu
00:43should go up. So after the list comes down, you Click an item in the list and
00:47should go back up. So we're going to run toggleDropdown and the other time we
00:50want to run toggleDropdown is when we Click the Dropdown button.
00:54So, add an event listener to the button in that if statement. We check to see
01:00if getChildAt(i) is a BasicButton. So at the very bottom of that if statement,
01:04go in to add an event listener to the button. So button.addEventListener and
01:09the event is going to be MouseEvent. Click, make sure to capitalize Click, and
01:14again we'll run toggleDropdown.
01:21So, right below the init method, we'll define the toggleDropdown method. This
01:26will be a private method. So, private function toggleDropdown, receive an event
01:34with a datatype of Event. And in case we want to run it another time without
01:41needing an Event, we'll set it equal to null.
01:44So return datatype to void, some curly braces, and now notice that the datatype
01:49here for the event is Event, and I'm using it for a MouseEvent and Event
01:56datatype as well. Now the reason why that works is because the MouseEvent class
02:00extends the Event class. So, because MouseEvent is subclass of Event, I can use
02:06the generic Event datatype here.
02:10So all Event classes that are not Event like TimerEvent, MouseEvent etc,
02:15are all subclasses of the Event class. So, pretty much in every Event handle
02:20that you can create, you can basically have the Event datatype set to Event and
02:25will still work properly.
02:26So in our toggleDropdown, the first thing we're going to do is bring the
02:29dropdown menu to the front, because you definitely want that list coming down
02:34to be in front of every other object. So, just to be sure, we're going to run
02:39parent.addChild, passing in this. So I'll bring that list in front of
02:45everything by bringing this whole dropdown menu in front of everything.
02:49Next, we'll have an if statement. We're going to check to see if the current
02:53value has changed. If so, we're going to fire off the CHANGE Event. So,
02:59list.currentValue. So if there is a current value for list and if the current
03:05value of list, so list.currentValue is not equal to the value of _currentValue.
03:13So if there is a value for list and that value is a new value, then we're going
03:18to do as in the curly braces here.
03:20So first we'll set the button label. So button.label = list.currentValue. Then
03:27_currentValue = list.currentValue. Then we'll dispatch that CHANGE event. So,
03:35dispatchEvent, create a new Event object, and inside of the new event
03:41constructor pass in Event.CHANGE.
03:45Below the if statement, we're going to make isDown. That's that property that
03:50will identify whether or not the list is down. We're going to make it equal to
03:54its opposite. So isDown = !isDown. So it's going to equal whatever it is not.
04:04So if it's false, it will be equal to true and if it's true, it will be equal to false.
04:09Then what we'll do is check to see if it is down or not. So if and in the
04:14parenthesis, check to see if isDown is true. If so, we're going to use
04:19TweenLite to tween the list and here is the syntax for that, TweenLite.to, and
04:27in the parenthesis the first parameter is the object to tween. So that's list.
04:31Next is the time to tween, which we'll put in, .25. That's in seconds. Then
04:36next is an object, so some curly braces, for shorthand notation.
04:41This has a bunch of different properties that we can pass in. The first one is
04:45any tween-able property that we want. So I want y to end up at button.height -
04:54buttonSpace. So y:button.height - buttonSpace. The next property I'm going to
05:02put in is what to do or what function to run when the tween updates.
05:08So, for that you just set the onUpdate property. So, onUpdate : resetMask. So
05:15as the tween is updated, we're going to run a method called resetMask, which
05:18we'll define later. And again, remember that whenever you're working with an
05:21animated mask or you move a mask around, you need to reset the mask. So that's
05:28why we're doing that here.
05:29I'm just going to copy and paste this if statement for the else statement and
05:34change the if to an else, so no parenthesis after that. Then change the value
05:41of the y property to button.height - list.scrollbar.height. So here, if isDown,
05:48then the TweenLite is going to tween the list all the way down to where it
05:53should be. So it's going to reveal the list. And if not, this is going to hide
05:57the list, it's going to make it go up. So the button height minus the height of
06:01the scrollbar of the list will make the list slide back up into its original position.
06:06So, now let's define resetMask. So we'll do that right below the method we just
06:13defined. So private function resetMask, no parameters, :void, and all we're
06:20going to do in here is just reset the mask. So list.mask = listMask.
06:29Then one last thing we've got to do is create the public version of our
06:33currentValue property. So, all you do to create a public version of that is
06:40create the Get method. We're not going to create a Set method because we only
06:43want it set by selecting an item in the list. So, public function get
06:48currentValue returns a String, some curly braces, and we're just going to
06:55return _currentValue.
06:58So that's it for this class, we're just going to need to add a little bit of
07:02code inside of the FLA file, and we're ready to test. So save the file, go
07:10inside of your dropdown menu. In the FLA, select the first keyframe in the
07:15actions layer and open up the Actions panel and we'll put in addEventListener
07:21and the Event will be Event.CHANGE. Then I'll run a method called checkChange.
07:28Define that function here, function checkChange, receives an event with a
07:33datatype of Event. And in here, we'll simply trace currentValue.
07:40So we'll test the movie, and notice that the Choose is in the right place,
07:45arrow is in the right place. I Click the dropdown menu. The list slides down.
07:49And then I can Click the arrows to navigate to the list items. If I select one
07:54of the list items, the label on the button updates as well as the Output window.
07:58You will also notice that I have this little bit of hanging down of the
08:04scrollbar below the button. Now that problem is coming because of the
08:08buttonSpace. Like I mentioned in an earlier movie, that buttonSpace may or may
08:13not be necessary. It just gives you the option to add padding for the button.
08:18So let's go back to Dropdown.as and change that buttonSpace Number to 0. So
08:24change that, save the file and then test the movie. Click to bring the dropdown
08:32down and then up again and the list is showing and hiding properly. So again
08:37that buttonSpace property is there for you to use or to not use. You can
08:42control the space between the list and the button.
08:45With that our dropdown menu is complete. So to recap, we're created a dropdown
08:51menu, which is basically a button that controls showing and hiding of a list.
08:57To show and hide the list, we used a mask and TweenLite for the animation.
Collapse this transcript
9. Using UI Elements
Viewing a simple form application
00:00In this chapter we're going to look at some examples of using the user
00:03interface elements that we built throughout this title. The first thing I want
00:07to show you is a form that I created using a good amount of these elements. On
00:12the stage here I have a form. It's all in a movie clip called Form and if I
00:17Double-Click that movie clip you'll see that it has encapsulated code and artwork.
00:22So I have Name and Email, Static text fields and then Input text fields called
00:28name_txt and email_txt to accept that information. I have a Static text field
00:34called Contact Options and then I have some Checkboxes, cb1_mc and cb2_mc.
00:42These label values are set with codes. So we'll look at them in just a minute.
00:46And we have a RadioGroup. If I double -Click that element we can enter its
00:49timeline to see what's in there. So there is a dynamic text field called
00:53title_txt and there is a bunch of Radio buttons r1_mc, r2_mc, r3_mc and r4_mc.
01:01And if you look at the code here, pretty straightforward. I'm just setting
01:08labels for all the RadioButtons. Back to the form, because we have a text field
01:12says Message. It's a Static text field, TextArea element called message_txt and
01:18finally a button called submit_txt. This is the same button that we created in this title.
01:24Let's go to the first keyframe of the Actions layer and open up the Actions
01:28panel and the first thing you'll see is the sendForm function. Let's scroll
01:32down to the bottom though and look at some initial values that are set. So we
01:36are setting labels for the checkboxes and for the submit button.
01:40So if you test movie you can see what those look like. So you have the checkbox
01:43labels, the radio labels and the Submit button. Pretty straightforward and then
01:49we have addEventListener line of code, addEventListener to the submit button,
01:55running a function called the sendForm.
01:56So now just see the sendForm function. Now this may look pretty intimidating
02:01but I promise it's not as crazy as it looks. So the first thing here we have a
02:06variable called request. It's holding an urlRequest and to just do a simple
02:10example I have this fake email address here and I'm using the mailto send the
02:15email. So this will just open up the person's default email program when they
02:19Click the send button.
02:20Of course you could have this to be some sort of url where you can send all the
02:25form data to and you could process the form and send an email without having to
02:31open the person's email program. If you want to know more information about
02:34that you're going to have to use a different programming language something like PHP.
02:39So if you're going to use PHP I recommend looking up the PHP mail function
02:45there you can find information about how to send an email with PHP. Other than
02:50that, the concept is to me pretty much the same here, sending data to another url.
02:55So on the next line we have a variable called vars to new instance of the
02:59urlVariables class and these are form variables. So you can add different
03:03values at the end of url and by using urlVariable class the data automatically gets url encoded.
03:09So you don't have to do that manually. So that's pretty convenient. So here we
03:13have the body property of vars being set. Now the body property is significant.
03:19It's part of the mailto protocol. When we use the body property, what happens
03:24is the text that you put in there goes into the body of the email. So here is
03:29the body of email basically. Everything is connected to vars.body.
03:32It says "Here is a form you received from", and then it appends name_txt.text,
03:38< sign, person's email,, sign, and then a colon, some new lines. So that's
03:46what \n, it's a new line operator, then adding on a whole bunch of little
03:51hyphens and some new lines. This is basically a separator. This is just all
03:56arbitrary stuff that I did just for this particular email.
04:00So the thing that you really need to do is just to define a body. Then we have
04:04appended on the message from the textField that's the TextArea and then
04:09another dividing line and then the checkbox label and checkboxes isSelected. So
04:15we'll say Contact me by phone : true or Contact me by email : false or
04:21whatever. And then for the subject to the email, see that subject property on vars.
04:27When you add the subject to urlVariables, it's a part of the mailto protocol
04:32and that sets whatever you put in there as the subject to the email. So that's
04:35going to be whatever of the RadioGroup label is. And then here we have req.data
04:42= vars. So we're connecting the urlRequest and the urlVariables there and
04:48navigate to url _self.
04:50So this won't send the email but it will open up the person's default email
04:54program and the body of the email will be whatever we put in the body which is
04:59the code I have highlighted here. Subject to the email will be what we have
05:03here highlighted, the vars subject property, and the email address it's going
05:09to as whatever we have after mailto when we define the urlRequest.
05:13So there is a simple example of a form that you could create using the user
05:17interface elements that we created in this title.
Collapse this transcript
Skinning UI elements
00:00Now that you have created the interactivity for these user interface elements,
00:05remember that you could always swap out the artwork to make them look
00:09different. So you can not only change fonts and font sizes but you can actually
00:13change the look of all of the elements very easily.
00:17In this movie we'll take a look at how to customize the checkbox element that
00:21we created. So the first thing I'm going to do is zoom in on one of the
00:25checkboxes. And I'm going to Double- Click that checkbox to enter its timeline.
00:31So remember we have to the selected checkbox and the unselected checkbox.
00:35What I'm going to do is hide the selected checkbox and then Double-Click the
00:39unselected box to enter its timeline. The first thing I'm going to do inside of
00:43here is I'm going to set this layer to be a guide.
00:46So I'm going to Right-Click that layer and choose Guide. That way we won't see
00:50it when we test the movie, but we can use it as a guide when we are developing
00:55our new box. So create a new layer and in that new layer draw some artwork. So
01:01I'm just going to draw a new box. So I have a light gray background like so.
01:10Maybe that Stroke is a little big, so I select the Stroke and bring it down to 1.
01:13Looks a little bit better, and then I'm going to jump back to the Checkbox
01:21movie clip. Hide the unselected box, show the selected box.
01:26I will Double-Click the selected box to enter its timeline, again, I'll turn
01:30layer 1 into a Guide, create a new layer on the new layer. I'll draw another
01:37box and now I'll use the Pencil tool with this slightly larger Stroke. Let's go
01:46for around 3 or 4 and then I'll draw an x inside of the box. Be kind of rough
01:53there and then I'll test the movie and we have the new checkboxes.
02:01Note that the label for the checkboxes has disappeared because that's a default
02:05behavior for the label if you don't define a label. So I have easily customized
02:13our checkboxes and you can do that with all of the user interface elements that
02:18we created just swap out the movie clip symbols and you have a completely new
02:23skin while preserving the same interactivity.
Collapse this transcript
Conclusion
Goodbye
00:00Well, my friend, we have reached the end of this title. Just to recap on what
00:05you learned we created a scrollbar, we connected that to our own custom text
00:10box that had powerful scaling capabilities, we looked at creating user
00:15interface checkboxes, radio buttons, all from scratch.
00:20Then we looked at an example 4 application that you can create using those
00:25exact same user interface elements. We also looked at how you can skin those
00:30elements to customize their look and feel. So I hope you had a fun time in this
00:34title and learned a lot because I sure did. I'll see you next time.
Collapse this transcript


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 100,984 instructional videos.

start free trial learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 1,945 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.


site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked