ActionScript 3.0 in Flash Professional CS5 Essential Training

ActionScript 3.0 in Flash Professional CS5 Essential Training

with Todd Perkins

 


In ActionScript 3.0 in Flash Professional CS5 Essential Training, Todd Perkins shows Flash designers how to incorporate ActionScript code into their projects and create interactive presentations and applications. The course includes a review of ActionScript language basics and the object-oriented programming (OOP) methodology, a tour of those Flash Professional CS5 features designed for developers, such as code hinting and the Code Snippets panel, and instructions on interacting with objects in the Library and placing code on the Timeline. Exercise files are included with the course.
Topics include:
  • Understanding the building blocks of ActionScript
  • Working with the display list
  • Using dot syntax
  • Navigating the Timeline
  • Creating document classes
  • Linking classes to Library objects
  • Adding drag/drop functionality to objects
  • Creating a slide show
  • Loading and running code in an external SWF
  • Working with text
  • Accessing XML data
  • Playing audio and video with ActionScript

show more

author
Todd Perkins
subject
Developer, Web, Programming Languages
software
ActionScript 3.0, Flash Professional CS5, CS5.5
level
Beginner
duration
7h 8m
released
Oct 21, 2010
updated
May 23, 2012

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:04Hi! I'm Todd Perkins.
00:06Welcome to ActionScript 3.0 in Flash CS5 Professional Essential Training.
00:11If you're familiar with Flash, but want to take your ActionScript skills to
00:15the next level to add more complex interactivity, then this course is exactly
00:19what you're looking for.
00:20Throughout this course, you'll see all the features of Flash Pro that you can
00:24utilize to make writing ActionScript as smooth as possible.
00:29We'll start at the beginning,discussing how to control the colors and fonts of
00:33ActionScript in Flash.
00:35From there, we'll get experience working in the Actions panel and see what each button does.
00:41Then we'll begin writing our own code, starting with the basics, like modifying
00:45properties of movie clips and controlling timelines.
00:47Once you're used to writing basic ActionScript on your own, you'll use that
00:52knowledge to build real-world apps.
00:54We'll start with simple games, like a skateboarding game and a drag-and-drop game.
00:59Then we'll make more complex projects, like a slideshow viewer that loads
01:03external images, a SWF Player for playing back animation, and a video player.
01:10By the time we're finished with this course, you'll have seen just about
01:13everything Flash has to offer to help write ActionScript, and we'll have built
01:17many powerful, real-world applications as well.
01:21So, good luck and have a great time!
01:23
Collapse this transcript
Using the exercise files
00:00If you have purchased this title on a disc, or if you are a premium subscriber to
00:05lynda.com, then you have access to the exercise files for this course.
00:09Simply download the files and place them on your desktop for easy access.
00:14Exercises in this file are organized by chapters.
00:18Each chapter has the files we'll be working with throughout that chapter.
00:23Some chapters have folders called assets that hold the additional assets that
00:27we'll be working with.
00:32Some chapters are organized into folders instead of files.
00:36In those cases, click on the appropriate folder, and then you can find the start
00:41state in the start folder and the final state in the final folder.
00:49Whenever an exercise file is available for a video, you'll see a yellow overlay
00:54at the bottom of the screen.
00:55This will indicate the name and location of the file that we'll be working with
01:00in that particular exercise.
01:02If you don't have access to the exercise files, don't worry.
01:05You can still follow along.
01:07As we go throughout each movie, I'll show you how to set up your files in the
01:11same way so that you can follow along and have the same learning experience.
01:17
Collapse this transcript
Using the function keys
00:00Throughout this course, we're going to be using a lot of keyboard shortcuts,
00:04particularly with the function keys.
00:07If you're working on the Mac, your computer may be programmed to have those keys
00:12perform other functions.
00:14To make the F1 and other function keys function as regular keys, go to Apple >
00:21System Preferences, and in System Preferences, click Keyboard.
00:27Under Keyboard, make sure the box that says, Use all F1, F2, etc.
00:32keys as standard function keys, is checked.
00:35From there, you can simply close System Preferences, and you can use the F1 keys
00:40as we use them throughout the title.
00:42
Collapse this transcript
1. Understanding ActionScript 3.0 in Flash Professional
Adjusting preferences for ActionScript fonts, colors, and formatting
00:00Controlling the appearance of your code is an essential tool for veteran
00:03programmers and newbies alike.
00:05Modifying ActionScript fonts, colors, and formatting allows you to make code
00:10easiest for you to read and write in Flash Pro.
00:12In the Actions panel here, I have some code.
00:15Right now, I don't want you to have to understand any of what this code means
00:19or what it's doing.
00:20I just want you to look at the appearance of it.
00:23Is it easy to read? Is it hard to read?
00:25Are the colors too bright or too little contrast or too much contrast?
00:30If you want to change those things, you can change them in Flash Preferences.
00:34For example, I like to have larger fonts on my screen when I record these movies
00:40so that the code is easier for you to see.
00:43So, I'll go to Flash > Preferences.
00:46Note on the PC that's under Edit > Preferences.
00:50To modify your ActionScript settings, click the ActionScript Category on the
00:54left side of the window.
00:57Here you'll see various options for modifying your ActionScript code.
01:01You can choose how Flash handles curly braces and spacing when you create new lines.
01:07This will be more applicable once you learn more about creating your own code.
01:11Just make a mental note now that this setting is here if you ever want to change it.
01:16You can control whether to display code hinting and the delay in showing the code hints.
01:22Again, you'll learn more about this later on;
01:24just make a mental note that it's here, and you can change the Font.
01:28Now I like to use Monaco for my ActionScript Font.
01:32This is the default font on a Mac when you're working in Flash,
01:37but on the PC, it's a different font. If you really like Monaco, like what you
01:41see on my screen, you can find it on the web for the PC.
01:44Now as I said earlier in this movie, I like having a larger font size, so it's
01:50easier for you to read.
01:51So I'll change my font size to 18.
01:55You can change your font size if you want or keep it the same - your preference.
01:59Notice other dropdown menus, like Open/Import.
02:02This shows how Flash should encode the text inside of your files that you're using.
02:08I just like to leave the default, UTF-8.
02:12When you modify an open file outside of Flash, you can control what Flash does,
02:16whether you get prompted, or it reloads, or it doesn't reload.
02:21You can use a different editor for your code files, whether you want to use Flash
02:25Professional or Flash Builder, or you can have Flash ask you.
02:29You can control the color for your code.
02:31Let's say you wanted the foreground color to be a bright green, the
02:36background color to be black.
02:41So, I'll click OK, and there are my new code settings.
02:45Notice the font is bigger, and there is green for the main text and then
02:50black for the background.
02:52This is a little bit hard for me to read,
02:53so I'm going to change it back, but you can change the settings however you want.
02:57Go back to Flash > Preferences, click ActionScript, change Foreground back to
03:02black and Background back to white.
03:05These two options at the bottom are advanced settings that we'll talk about
03:09later on in the title.
03:10So, I'll click OK, and I get my default colors with my large font.
03:16So, by using the ActionScript Preferences window, you can easily tweak
03:19ActionScript fonts, colors, and formatting to best suit your individual needs.
03:25
Collapse this transcript
Changing Flash Player and ActionScript versions in the Publish settings
00:00Some projects you work on will require specific Flash Player or ActionScript
00:04versions, or you may be adding ActionScript 3 to a project that was created
00:09targeting Flash Player 8 or below.
00:12Whatever the specific task, it's important to be able to modify the target Flash
00:17Player or ActionScript version for your project.
00:21You can get to the appropriate menu in multiple ways.
00:25If you have nothing selected and look on the right side of the screen in the
00:29Properties panel, you'll see the Profile area, under the Publish option.
00:34Note the Publish option shows the Flash Player version, which here is Flash
00:37Player 10, and it shows the script version, which here is ActionScript 3.0.
00:42Let's say you're doing a project where you weren't using any features that were
00:47Flash Player 10-specific.
00:49So just to hit more of an audience, you wanted to publish to Flash Player 9.
00:53So to do that, I can click the Edit button next to Profile.
00:58Note this is the same menu as you can get by going to File and then Publish
01:02Settings, and you just have to click the Flash tab.
01:05In here, I can change the Flash Player version.
01:08So I can change it to Flash Player 9 or any other one I want.
01:12Note also you can change the ActionScript version.
01:15Though Flash Player 8 and below don't support ActionScript 3, Flash Player 9 and
01:2110 support ActionScript 1 through ActionScript 3.
01:26So, using Flash's Publish Settings, you can go to the Flash tab and adjust Flash
01:31Player or ActionScript settings for a particular project.
01:35
Collapse this transcript
Reading and solving errors through the Compiler Errors window
00:00Even the most seasoned programmers make mistakes, but the difference between the
00:04pros and amateurs is that the pros know how to read error messages.
00:09Flash has two windows that help you find errors in your code,
00:13one of which we'll focus on in this movie.
00:15If I test the movie here with Command+ Return or Ctrl+Enter on the PC, you'll see
00:21an error in the Compiler Errors window.
00:25In the Location section, Flash tells me where the offending code is:
00:29in Scene 1, layer 'actions', Line 8.
00:34The description says, Syntax error:
00:36expecting rightparen before semicolon.
00:39So note that when you get an error, make sure to read the error message.
00:42I know that's pretty obvious to say, but read it and really think about what's wrong.
00:48There is a missing rightparen, according to Flash.
00:51So I am going to double-click this error message, and Flash will actually open
00:55up the Actions panel and highlight the offending code for me.
00:59Now at this point if you don't understand this code, I don't want you to worry about that.
01:03I just want you to understand how the Compiler Errors window works right now.
01:09The offending code is highlighted.
01:11I can see at the end that there is only one right parenthesis, but at the
01:15beginning of the code there are two left parentheses.
01:18So when you are writing ActionScript, they actually need to be balanced out.
01:22You need to have - if you have two left parentheses you need to have two right parentheses.
01:26One of these on the left is actually a mistake, so if I delete that, I can test
01:30the movie again, and Flash has actually given me another error.
01:37Note that when you get into Compiler Error, Flash actually stops processing the code.
01:41That's why we didn't see these two errors at once.
01:44So if we look at this, we have Scene 1, layer 'actions', Frame 1, Line 23.
01:49Access of undefined property SoundMixe.
01:52So if I double-click that, I can see that this is undefined, SoundMixe.
02:01Now being used to writing code all the time, I know that this is probably
02:05supposed to be SoundMixer, with an r on the end, and if you are new and you
02:10didn't see that right-away, I promise as you go through this title,
02:13as you get more experience with ActionScript, errors like this will become
02:16more obvious to you.
02:18So I make the fix, and I test the movie one more time, and the code is
02:26processed, and you can tell that by hearing the background music.
02:30When you see errors in the Output and Compiler Errors windows, don't panic.
02:35Remember that those windows give you important messages about fixing your
02:39problems. By paying attention to those messages, you will be able to fix your
02:44errors and more quickly transition into being an ActionScript master.
02:49
Collapse this transcript
Using the Actions panel buttons to add and remove comments
00:00Comments allow you to organize your code, temporarily disable code, and to write
00:04messages for yourself and for other programmers.
00:07Flash Pro lets you insert and remove comments with the click of a button.
00:12I will show you how that works in the Actions panel.
00:14I click the first keyframe in the actions layer and press option F9 on the Mac
00:20or F9 on the PC to open the Actions panel.
00:23Let's say I wanted to make a comment to tell another developer where the code is
00:27that starts the game.
00:29So I can click on line 6 in my code, type two forward slashes, and then I can
00:35type, "click or press a key to start the game."
00:44So line 7 and 8 are the code that does that.
00:46They set up the code to click or to press a key to start the game.
00:51Again, you don't have to know what this code means right now; just understand
00:55the part about the comment -
00:56this is just a simple note to somebody.
00:58Notice that this line of code is gray, and if I delete those two forward
01:04slashes, then some of these words turn blue. That means they are reserved
01:09keywords in Flash, so this is actually going to be processed as code if I were
01:13to test moving right now.
01:15The easy way and the fast way to add a comment is to simply click the buttons at
01:19the top of the Actions panel.
01:21So there is the multi-line comment, which is the forward slash and the asterisk,
01:24and there is the single-line comment that is the two forward slashes.
01:28So I can simply click the apply line comment to add two forward slashes.
01:31I can comment that line of code.
01:33So this line of code is gray, which means it will not be processed by the Flash Player.
01:39If you want to remove a comment, you can click the remove comment button.
01:42That's a speech bubble with x on it.
01:43So I can add a comment or remove comment.
01:47I will delete this line altogether, and let's say I wanted to disable this
01:53interactivity. I want to make it so you cannot click or press the button on the
01:57keyboard to start the game.
01:59I can highlight these two lines of code, and I can click Apply block comment -
02:03that's the forward slash and asterisk, and Flash will wrap that code in forward
02:08slash asterisk and end the wrapping in asterisk forward slash.
02:12That creates a multiple-line comment and notice all this code is gray, so it
02:18will not be processed by the Flash player.
02:21Again, I can highlight that code, click the remove comment button, and it will go back to normal.
02:26So using the comment buttons in the Actions panel, you can quickly insert and
02:31remove comments in your code.
Collapse this transcript
Using the Actions panel to format code
00:00Whether you are cleaning up your own code or reformatting code in a file you
00:04have received from someone else, Flash makes it easy to format your code
00:07exactly how you want it.
00:10Before you have Flash automatically format your code, however, you need to make
00:14sure there are no errors.
00:16You can find out that there are no errors in your code by testing a movie.
00:23If you don't see any syntax errors in the compiler errors window, you should be
00:27okay to autoformat your code.
00:30Code formatting is controlled through the Auto Format settings in Flash preferences.
00:36So if you go to Flash preferences or Edit > Preferences on the PC and click the
00:41Auto Format category, you can control the settings here.
00:45So let's say I unchecked all these boxes, and I get a preview of what my
00:48code will look like.
00:49Notice the curly braces, they are on the same line as the function name.
00:52I click OK, and my code looks the same as it did before, until I clicked the
00:58Auto Format button.
00:59So we click that button, which looks like an indented paragraph, Flash will
01:04format your code based on those settings.
01:07Now personally, I like to have my curly braces on separate lines and have the
01:12settings as I had them before. So I am going to change it back, but if you prefer this way, by all means, keep
01:18your code like that.
01:23So I click OK and to get my code back to how I had it, all I have to do is click
01:28the Auto Format button again.
01:29And Flash will format the code based on my settings.
01:33Again, this works really well if you get code from someone else and you prefer
01:37to read code in a different way.
01:40So whenever you want to reformat your code, simply check that there are no
01:44errors and then click the Auto Format button and let Flash do all the work for you.
Collapse this transcript
Using the Actions panel Toolbox
00:00If you want to get the most out of the Actions panel, you should be familiar
00:04with some of the code helping tools.
00:06One of them is the toolbox.
00:08If you click the Show/Hide toolbox button, that's the last button on the right
00:13at the top of the Actions panel,
00:15you click that button, and then it opens up the toolbox. And now I can click and
00:20drag where these rectangles are and reveal the whole toolbox.
00:24So there's a section at the top that gives you language elements and then a
00:28section at the bottom that enables you to navigate through your code.
00:31Let's look at the navigation section first.
00:34This tells you where all the ActionScript is inside of your FLA file.
00:37It enables you to click on different frames, so that you can navigate around
00:41quickly to all the different code.
00:44I usually keep all my code on one frame, but when my code is scattered across
00:50the whole FLA file, this section becomes very useful.
00:55I'll minimize it now by clicking the rectangle with the arrow icon.
01:01The other section of the toolbox has many code snippets that you can put inside
01:06of your code, and by many I mean smaller than what you find in a Code Snippets
01:09panel, like one line of code or one simple command.
01:13You can also find these same commands through the Plus icon at the top-left of
01:18the Actions panel, and you can access them through the Script Assist menu, which
01:24is the magic wand at the very top-right of the Actions panel.
01:30You can add code in the Script Assist by clicking the Plus icon and selecting the
01:34code that you want to add.
01:39For these features, you already have to be familiar with ActionScript 3.
01:46Personally, they aren't a part of my workflow since I prefer to type the code by hand.
01:52But for you, you may find them very useful once you become more familiar with the language.
01:58So by using these extra tools, you can look at improving your workflow when
02:02you're working with ActionScript.
Collapse this transcript
Seldom-used but helpful functions of the Actions panel
00:00Not all the features of the Actions panel are commonly used by people that use Flash,
00:06but there are some that are particularly useful to you.
00:10For instance, there is the Find/Replace command.
00:14You can click the Find button to open it up; that's the magnifying glass with an
00:19A on it. Or you can highlight a piece of code by double-clicking, so I
00:26highlighted one word, which is loader here, the lowercase L, and I press
00:30Command+F or Ctrl+F on the keyboard, open up Find/Replace, and it will already
00:35have the Find what section populated.
00:38Here I have some code called loader, and let's say I wanted to make it something
00:43more descriptive, like gameLoader or something.
00:46So I can replace that with gameLoader, capital L, and then I'm going to check
00:51Match Case, so it doesn't override the loader with a capital L, which is
00:55something different in the code.
00:56Now, I'll just click Replace All.
00:59So Flash tells me it found and replaced 5 items.
01:03So now I have a more descriptive name: gameLoader.
01:07Notice the word Loader, with capital L, is still intact.
01:12Another useful feature of the Actions panel is the Insert Target Path button.
01:16If you click this button, this basically gives you a map of your FLA file.
01:22I only have one object on the stage, so you don't see the whole structure here.
01:26But if you have more objects on the stage, you can click them and then click OK,
01:31and then Flash will insert the code that you need to communicate with that
01:34object through ActionScript.
01:36You'll see the usefulness of that later on in this title.
01:41There is the Check syntax button, which checks to see if you have any syntax errors.
01:45I can click that button now. I don't have any errors, so I don't have any problems.
01:52There is the Show code hint button.
01:53We'll talk more about code hinting in another movie.
01:57There's Debug options, which you can use to help debug your code when you're
02:02testing it in Flash player.
02:03Next code is Expanding and Collapsing.
02:08So let's say if I scroll down here in this block of code that says startGame, I
02:12can click right after that curly brace and then click Collapse between braces.
02:16So I have that block of code organized like that.
02:19I can collapse a section of code by highlighting it, then clicking the Collapse button.
02:26I can expand that by clicking the Expand button.
02:29In addition to these features of the Actions panel, you also have the
02:32Options area, which is that dropdown menu you can get at by clicking the top-
02:36right of the Actions panel.
02:38That's that little arrow that's pointing down and the few lines next to it.
02:42Here you have some other options, like you can pin a script somewhere while you
02:47move around with the rest of your code.
02:49You can go to a particular line, you can import and export Scripts, you can
02:54print code, you can show or hide line numbers, and you can control word wrap.
03:00Note that you can also get straight to your ActionScript Preferences through
03:02this window, like so.
03:07So by using these features of the Actions panel, you have more tools to work
03:12efficiently in Flash.
Collapse this transcript
Understanding code hinting
00:00Code hints can save you large amounts of time when writing code and
00:03simultaneously help you learn ActionScript faster.
00:07Code hints appear when you type certain special characters in Flash.
00:12Right now, I don't want you to type the code that I'm typing;
00:14I just want you to watch my screen to see how it works.
00:16You'll learn about how to type all this code later on in the title.
00:20So I'm going to set the word 'var' and then a space and then 'gameLoader' with a
00:25capital L, and then I'm going to type what are the special characters that
00:28brings up code hinting. That's a colon.
00:30So I type that, and I get this menu.
00:32I can navigate with my keyboard or with my mouse.
00:39There are a ton of things to choose from, but if I start to type in what I want,
00:43which is Loader with an L, I'll type that colon again, Loader, then Flash will
00:50eventually highlight that code in the Code Hinting window.
00:53Now, I can simply press Return on my keyboard and Flash will write Loader for me.
00:59Also, notice that Flash added this import line of code.
01:03For now, don't worry about what that means; just know that Flash is helping you
01:07out by writing a lot of code for you.
01:12So I can type some more stuff, so Loader = new Loader, and I'll type a
01:17parenthesis, and then I see code hinting again.
01:21Another way to access code hinting is to type a dot.
01:24So I can type 'gameLoader.Load' and then press Enter on my keyboard, and here you
01:32will see load, and then it says request:URLRequest, Context: LoaderContext.
01:39Now, this might you look like a bunch of garbage to you right now, but this is
01:43actually very useful information when you're learning ActionsScript.
01:47Flash is telling you what type of data to put inside of the parentheses.
01:52So as you go throughout this title, I want you to pay attention to code
01:55hinting and use it to your advantage to help you learn ActionScript more
01:59quickly, and to write code faster.
Collapse this transcript
Reviewing the Code Snippets panel
00:00Code snippets give you an excellent starting point when writing code, by giving
00:04you access to dozens of useful, prewritten blocks of ActionScript.
00:08To access the Code Snippets panel, you can click the Code Snippets button in the
00:13top-right of the Actions panel, or go to Window > Code Snippets.
00:16Now, you're probably already familiar with the Code Snippets panel, so I'm just
00:22going to do a quick review.
00:24The snippets are organized in categories:
00:27regular actions, actions for timeline navigation, for animation, loading and
00:33unloading files, et cetera.
00:37Let's say I want to stop the Timeline.
00:38I could expand the Timeline Navigation, and double-click stop at this frame.
00:43In the Actions panel, you'll see the stop action is added. It says 'stop,' and there's
00:48parentheses, and then as semicolon, and there is a comment.
00:53You can tell it's a comment because it's gray.
00:55Remember that multiline comments start with a forward slash and an asterisk
00:59and then end with asterisk forward slash.
01:02This comment explains what the code does.
01:05Now, you've probably already done this before, if you've ever used the
01:08Code Snippets panel.
01:10But another thing you can do with the Code Snippets panel, if you're an advanced
01:14user, is create your own code snippets.
01:17Let's go back to the Code Snippets panel and see how to do this.
01:22To create your own snippet, click the Gear icon at the top-right of the Code
01:27Snippets panel. Then choose Create New Code Snippet. Give your code snippet a
01:35Title. Give your code snippet a tooltip.
01:43You can autofill from code that's already selected in the Actions panel if you
01:47want, or you can simply write your code in the Code area.
01:59This code doesn't really do anything,
02:00so you don't need to copy it down if you don't want to.
02:04Note that you can also check this box to automatically replace
02:07instance_name_here when you're applying the code snippet.
02:11You may have seen an example of that when you, say select a button, and then use
02:16a code snippet to make that link to a web site.
02:19That's what you're essentially doing here.
02:21To create the code snippet that does that, you simply type instance_name_here,
02:26and Flash will automatically replace that code with the correct instance name
02:30when you select it on the stage and apply the code snippet.
02:34Keep in mind that if you didn't understand that, it's no big deal.
02:37That's something that you're going to understand more about when you learn
02:40more about ActionScript.
02:41So I'll click OK to create the code snippet, and then you can see my example
02:46snippet in the Custom section.
02:47So, I'm going to apply it into my code by double-clicking it, and there it is right there.
02:53So if you ever find yourself constantly writing the exact same block of code,
02:58see if you can find it prewritten in the Code Snippets panel.
03:01If it's not there, you can always add it yourself, so you can save time when
03:05creating your projects.
Collapse this transcript
Using help
00:00If you ever find yourself stuck, or want to learn more information about a
00:05particular topic in ActionScript, you can actually highlight just about any blue
00:11keyword, and press F1 on your keyboard to launch Flash Help.
00:17Here is the Flash Help menu.
00:19If you look in this part of the window, which is on the right side, you can
00:23scroll down and find information about the code that you highlighted.
00:28Some of the explanations are in actual sentences, and not just in code, so you
00:34can have a better understanding of how the ActionScript classes work.
00:39You can find additional properties and methods that you can run here as well,
00:44along with explanations and examples.
00:49So whenever you get stuck when you're working with ActionScript, highlight any
00:53blue word that may be giving you trouble and press F1 on the keyboard to
00:57launch Flash Help.
Collapse this transcript
2. Becoming Familiar with ActionScript 3.0
Understanding how ActionScript 3.0 code is processed in the Flash Player
00:00Before you start writing your own code, you should be familiar with how that
00:03code is processed, so that you have an idea of how your applications will react
00:07when you start writing ActionScript.
00:10Remember that ActionScript runs in the Flash player, so your movie actually has
00:14to be playing in order for code to be processed.
00:17In Flash Pro, ActionScript can be placed in external files, which we'll talk about
00:22later, or in the Timeline in keyframes.
00:25You can tell where ActionScript is in the Timeline by looking for the lowercase
00:29a instead of a keyframe.
00:33So I'll click the first keyframe of layer 1, and open the Actions panel with
00:37Option+F9 on the Mac or F9 on the PC.
00:41ActionScript code runs one line at a time, in small fractions of a second.
00:48In fact, many lines are processed in one frame, which could be 1/60th of a
00:54second, or 1/24th of a second.
00:57So the code is processed very quickly.
00:58Again, the code runs one line at a time,
01:02but sometimes, a process will take a little bit longer than another one;
01:06for example, loading a PNG file like I'm doing on line 3.
01:13When this happens, instead of waiting for that whole process to be finished,
01:17Flash just moves on to the next line and the file loads in the background.
01:22This is called asynchronous code.
01:26Another thing I want to point out when you're working with ActionScript is some
01:29code runs at a specified time, or when a specified event occurs.
01:34We're going to be talking about this in more detail later, but for now I want
01:39you to understand that some code doesn't run line-by-line.
01:43For example, before the file is loaded, I connect the loading file to a block of code.
01:52I tell Flash, when the file is done loading, then I want you to run this block
01:57of code called the fileLoaded.
02:00That block of code will cause a message to appear in the output window.
02:05That's what a trace statement does.
02:07It simply allows you to create a note for yourself that will pop up in the
02:12Output window while your movie is playing.
02:13It can be used to fix problems in your code.
02:17So before I test the movie, I just want to point out that I have these trace
02:21statements here to demonstrate in what order the code is processed.
02:25So I have the file loading on line 3, and then the trace statement that says
02:30started loading file on line 4,
02:32so that's after the file starts loading.
02:34Once the file is done loading, then you'll see file loaded in the Output window.
02:40So I'll test the movie.
02:42It says started loading file, and then file loaded after that in the Output window.
02:48So you can see that the file starts loading, and then Flash just moves on to the
02:54next line, and when the file is complete, this block of code runs.
02:58So what I want you take out of this is I want you to remember that
03:04ActionScript code is processing the Flash Player is asynchronous, so commands
03:09that take more time to complete, like a loading file, may still be running while
03:13Flash moves on to other lines of code, and that some lines of code will be
03:19executed when an event occurs.
Collapse this transcript
Understanding variables
00:00Variables are the building blocks of many a programming language,
00:03including ActionScript 3.
00:06Knowing what variables are and how to create and modify them is essential for
00:10building any application.
00:13I'm going to open up the Actions panel by selecting the first keyframe of layer
00:171 and pressing Option+F9 on the Mac, F9 on the PC, or by using the menu command Window > Actions.
00:27I'm going to create a variable by typing the word 'var.'
00:30That's var, short for variable, all lowercase.
00:35If you typed it correctly, it should be blue.
00:40In Flash, reserved keywords will turn blue when you type them.
00:44That's all based on your ActionScript settings that you can set through Flash Preferences.
00:50After var, type a space and then type the name of the variable.
00:55So, just type 'my' and then Name with a capital N, no space.
01:01When you're naming a variable, you have to follow special rules.
01:06Don't start with a number, but you can use numbers, as long as it doesn't
01:10start with a number.
01:12Then don't use any spaces or special characters, like an exclamation point or at symbol.
01:19After you type the name of the variable, you can type a colon to declare
01:24the variable datatype.
01:27Think of a variable like a noun.
01:30So a noun could be a person, place, thing, or idea.
01:35When I tell the datatype of a variable, it's the same thing as saying the type of noun.
01:40It's like putting it in a category.
01:42So, one category of variable is called String, which can hold text.
01:47So your name would be text.
01:50So you can put that in a String type variable.
01:56Again, I'm using code hinting.
01:58I just have to type the first few letters of string, and then I can select
02:01that from the window.
02:02Again, capital S, and it should all be blue.
02:06When you're declaring a datatype, just about every single datatype is going to
02:11start with a capital letter.
02:12There are about three or four that are going to be starting with lowercase letters.
02:17So myName, it's a String, which is going to hold the text datatype.
02:22Then if I want to set a value, I type space, equals, and then space.
02:28Then for a text value, you put that in quotes, which is different from a number,
02:32which doesn't need quotes.
02:33So I'm going to type open and close quotes.
02:36Those are going to be green.
02:38Again, this comes from your Flash ActionScript preferences, and then a semicolon.
02:43Now, I'm going to move my cursor back
02:47in between the quotes, and I'm going to type my name.
02:50You can type your name.
02:53The semicolon at the end is just like a period in a sentence.
02:57It ends a statement.
02:58Now if you want to see if this code is working properly, you can use a trace statement.
03:05Remember, a trace statement makes a message appear in the Output window.
03:09Again, it's usually used for testing your code.
03:13So we'll go to the next line by pressing Enter or Return, and then type the word
03:18'trace,' all lowercase, and it should be blue when you type it.
03:22Again, that's a reserved keyword in Flash.
03:24Type some parentheses.
03:27You'll notice that I do things like typing open and close parentheses, and
03:31then typing open and close quotes at the same time, instead of typing one, and
03:35then writing the code in the middle, and then typing the other, I do that so I
03:38don't forget to balance out the quotes or braces, because you always need to
03:42opening and close quotes.
03:44You're always going to need opening and close parentheses, curly braces, et cetera.
03:49So I like to make sure I don't forget to write the closed one by writing it at first;
03:54same thing with the semicolon.
03:55I'll move my cursor back in between the parentheses by using my arrow keys.
04:01Then inside of the trace area, which is the parentheses, I'm going to type
04:06myName, just as I typed it above.
04:08So now I'm going to test the movie and see what appears in the Output window.
04:20Notice that it says Todd, and it doesn't say myName.
04:23So the value of the variable is showing up in the output window, and not the variable name.
04:30So I put the name Todd inside of the myName container.
04:36There are many different types of variables that you'll be working with
04:38throughout the rest of this title.
04:40You'll learn more about them as you get more practice using them.
04:44But for now, the main thing I want you to remember is that variables are
04:48containers that hold data, are comparable to nouns, and are the basic building
04:53blocks of ActionScript 3.
Collapse this transcript
Understanding functions
00:00Functions are the secondary building block in programming, and allow you to
00:04store related tasks in named groups, so they can be reused in the most
00:09efficient way possible.
00:11In ActionScript, functions are actions, comparable to verbs, so they do things.
00:19Now I want you to think of riding a bicycle.
00:23You think of riding a bicycle as one task,
00:27but it's really a group of related tasks.
00:31So you have sitting on the bike, pedaling, balancing, et cetera.
00:38So just like that, a function takes a group of tasks and names in them.
00:44They usually have active names, like one of the functions you're familiar with
00:49is probably Stop().
00:52Stop is a built-in function. Remember, it's blue, and that stops the timeline.
00:58Notice that it's sort of an action word.
01:03Another function is gotoAndPlay().
01:09It looks pretty similar to stop, in that there is a blue phrase and some
01:15parentheses and a semicolon, except for in the parentheses you place which
01:19frame you want to go to.
01:20So gotoAndPlay groups related tasks.
01:25It takes the playhead, moves it to a particular frame, in this case 3, and plays from there.
01:35I can go on, but I think you get the point.
01:37Now, there is a difference between running a function and creating a function.
01:44Running a function is as simple as typing the function name in parentheses.
01:48Creating the function is a little bit more complicated.
01:51To create a function in Flash, type the word 'function.'
01:56Remember, it should be all lowercase, and it should be blue after you type it,
02:01showing that it's a keyword reserved for Flash.
02:05Type a space, and then type the name of the function.
02:10Let's say I wanted to create a function that added numbers,
02:14so I'll call this addNumbers.
02:19Usually, a function name starts with some type of action word.
02:23Here my action word is add.
02:25So it describes what the function does.
02:28I also like to write in camel-case, meaning each new word starts with a capital letter.
02:35This is useful because you can't use spaces in function names.
02:41After the name of the function, type some parentheses.
02:45After the parentheses, you specify what type of data the function will return.
02:51We'll look at this in more detail later on,
02:55but for now, just type a colon, and then type void.
02:59This is going to be one of the very few datatypes that starts with a lowercase letter,
03:03so v-o-i-d, with a lowercase v.
03:08Go to the next line, type an open curly brace, that's Shift+Square Bracket, and
03:16then press Enter or Return.
03:18Depending on your ActionScript settings, Flash may or may not write a
03:21curly brace for you,
03:23so you might have to add that close curly brace.
03:26Inside of the curly braces, I can write my tasks that the function will do.
03:31This is usually a set of commands, one on each line.
03:37So let's say I wanted to just add a simple trace statement here.
03:40So I'll type trace -
03:41remember that puts a message in the output window, some parentheses, and a semicolon.
03:47In the parentheses, I'm not going to use quotes;
03:50I'm just going to type 1 + 2.
03:56So I'm going to trace 1 + 2.
03:59Now if I were to test the movie, then nothing would happen at this point,
04:03because remember, I said that there is a difference between declaring a
04:06function and running a function.
04:09So this block of code I have highlighted here is simply declaring a function.
04:13It's saying that it exists, and you can run it whenever you want.
04:17In order to run the function, go below the curly braces, or of course, you can
04:24go above the word 'function.'
04:26It doesn't matter; as long as it's in your code, Flash will find where the function is.
04:33Then type the name of the function.
04:34So it's addNumbers and some parentheses, and a semicolon.
04:40So notice that it looks kind of like the stop function.
04:44It has the command and then the parentheses and a semicolon.
04:48But it's not blue, because this is a custom function that we just created. So test the movie.
04:53There, you can see 3 in the output window.
05:00To make a function more reusable, you can add something called parameters.
05:05Remember, when you ran the gotoAndPlay function that you passed in a number that
05:10tells Flash what frame to go to and play from.
05:13So we'll look at doing that here with our custom function.
05:18So let's say when you addNumbers, you're going to put the numbers that you want
05:22to add inside of the function.
05:24So every time you run the addNumbers function, you can use different numbers,
05:28and basically you have the function do something unique each time you run it.
05:33Let's look at how that works.
05:35Put your cursor in the parentheses, at the top of your code, right after the open
05:40parenthesis after addNumbers; Type num1.
05:45So all lowercase, num, n-u-m, and then a 1.
05:50This is the first parameter, and I am going to declare its datatype.
05:55So I'm going to type a colon, and then I'm going to type the word 'Number' with
05:58the capital N. Once I see it's highlighted in the Code Hinting window, I'm just
06:01going to press Return, and have Flash complete that for me.
06:06Then I'm going to type a comma, and then I'm going to type a space, and the I'm
06:11going to type num2:Number, just like I did for num1 function
06:17Most of the time spaces don't matter, so I can either have a space or not have a space here,
06:21but I prefer to put spaces because it helps me to read the code a little easier.
06:29So we have num1 and num2.
06:31Now I'm going to replace these numbers inside of the function with num1 and num2.
06:39So instead of adding 1 + 2, it's going to add num1 + num2.
06:44Now these numbers don't have any values yet.
06:47They won't have any values until we run the function and tell Flash which
06:52values those should be.
06:55So go down to the bottom of your code, when you're running the
06:58addNumbers function.
07:00Right after the open parenthesis, put one number -
07:04I'll put 5, put a comma, and notice the Code Hinting window comes up.
07:09It says addNumbers, which is the name of our function.
07:12It says there are two values. There is num1 -
07:15that is a number,
07:16and then there is num2, that is also a number.
07:20So for num2, I'll put in 10.
07:22So now I'll test the movie, and look in the Output window. So now I have 15.
07:30So the magic of the function is that I can reuse this as many times as I want.
07:34I can highlight the addNumbers line of code on line 6, copy it with Command+C or
07:39Ctrl+C, and then go down and paste it, and repeat that, paste in a few more
07:45times, and I can change the numbers that I'm adding to whatever I want.
08:01So now I can test the movie again.
08:05For some reason, the Output window scrolled down, and I don't see anything, but
08:09there actually is something there.
08:10So if I close that window and close the Actions panel and scroll up, you'll
08:16see the numbers that were added for me.
08:18So you've seen that I can reuse this function as many times as I want.
08:22Right now, it's only just one line of code, but imagine if you wrote 20 lines
08:27of code inside the function, and instead of having to rewrite 20 lines of code
08:32four times, you simply put it into a function, and you can just call the
08:36function or run the function four times, passing in different values like we're doing here.
08:41As we go throughout the rest of this title, you'll see more uses of functions
08:47and get experience learning how effective they can be.
08:50What I want you to remember now is that functions are groups of commands that
08:54are named and can be reused to organize and optimize your applications.
Collapse this transcript
Understanding events and event listeners
00:00When building applications in Flash Pro, one of the programming elements you
00:04will work with most is called an event.
00:07Events are things that happen, like a mouse click, keypress, a video is loaded, et cetera.
00:12And event handlers are functions that run when a particular event occurs.
00:19Let's take this application I have here.
00:21Now, if you don't have the exercise files, all you need to do is create a movie
00:25clip and give it an instance name of boarder_mc.
00:30So create a movie clip, select it on the main Timeline, go to the Properties
00:36panel, and in the Instance Name field type 'boarder_mc,' just as I have it here.
00:42We are going to talk a lot more about instance names and about manipulating
00:47movie clips later on, but for now this is all you need.
00:50Select the first keyframe of the actions layer, or create an actions layer, if
00:56you don't already have one, and open up the Actions panel.
01:01Now, if you've used code snippets, you may have seen events in action, when
01:04you've applied a mouse click event handler to an object.
01:08So, you'll select an object and apply the mouse click through the code snippets.
01:13Here, we are going to add keyboard control to the character in the game.
01:17Now, the first thing to do to be able to respond to an event is to add something
01:23called an EventListener;
01:26that tells an object to wait for a particular event to happen.
01:30Now, if we are going to connect the boarder to the keyboard, you would think
01:35that we directly connect the boarder to listen for the keyboard press, but
01:41that's not how it works.
01:43Keypresses are best controlled when connected to the Stage.
01:48So we're going to type the word Stage, and then I'm going to type dot.
01:56We'll talk more about what dots do in another movie.
01:59We'll type 'addEvent,' capital E, and you'll see addEventListener selected in the
02:06Code Hinting window before long, so you can just press Return, and Flash will
02:11write the rest of the code for you.
02:13So, eventually I have a capital E, else Listener, capital L, and then the event
02:19that we want to listen to for is called KeyboardEvent.KEY_DOWN.
02:23So I just type a capital K. And Flash highlights that code for me.
02:27So I can press Return. Flash will type the import line of code, which makes
02:32sure that I can access a keyboard event through my code.
02:38We'll talk more about importing code later on, but for right now just ignore that.
02:42After KEY_DOWN, type a space and then type 'keyPressed' and a close
02:55parenthesis and a semicolon.
02:57So, remember an EventListener, connects an event, and the event here is
03:02KeyboardEvent.KEY_DOWN, which represents when a key is pressed, to a function,
03:09which here is called keyPressed.
03:11Now, we haven't defined that function yet, but we'll define it in just a second.
03:15So, if connected pressing a key down to the stage.
03:18Keep in mind that an event can occur without anything happening, because you can
03:23press a key without a key to an EventListener.
03:27But when you add a listener, then you're able to respond to that keypress.
03:32So, I'll go down a few lines, and I'll create a function. Type the word function
03:37then a space, and then keyPressed just as I typed it above.
03:41So, keyPressed, just likes that lowercase k, capital P, one word.
03:46Open parenthesis, and then I am going to type 'evt.'
03:51This is a parameter that represents the event that occurred.
03:55This is data that is going to be sent to the function when a key is pressed,
03:59and it's going to have information about the event that occur, which is going to be a keypress.
04:03So type a colon and type Keyboard Event, capital K, capital E. Once it
04:11highlighted in the Code Hinting window, you can just press Enter or Return, close out
04:16the parentheses, type colon and then void, all lowercase.
04:21Go to the next line, create an open curly brace, add a return and depending on
04:28your settings, Flash will or will not create a close curly brace.
04:31If it's not there, make sure to add it yourself. And then inside of the curly
04:35braces, we are going to what happens when a key gets pressed.
04:40The thing that we want to do is move the skateboarder.
04:43So, I am going to type boarder, all lowercase, _mc, also lowercase, dot x, plus equals five.
05:00That's going to move the boarder five pixels to the right.
05:04Well talk more about this code later on. For now you can just copy what I
05:07have on the screen.
05:10So, test the movie, and then press any key on the keyboard and the boarder
05:14will move to the right.
05:15So here we have created a simple EventListener, and connected an event to a
05:28function, which is called an event handler, and inside of that function, we
05:32have moved the boarder.
05:34This is a pretty simple example of an event, but events are the backbone of your
05:39applications, and by applying EventListeners to objects, you can control what
05:44code is executed when an event occurs.
Collapse this transcript
Working with conditional statements
00:00Have you ever wondered how applications can think and make decisions?
00:04Conditional statements allow you to specify a block of code that runs only if a
00:09particular condition is met, and power many different parts of code, including
00:13artificial intelligence and games.
00:16In this application I have here, I can test the movie and press a key to make
00:21the boarder move to the right.
00:23Now, it doesn't matter which key I press.
00:25I am pressing the up arrow now and the Spacebar now; the boarder always moves to the right.
00:30What if we wanted to only move him to the right when we press the right key and
00:34move him to the left when we press the left key?
00:38Let's look at how to do that.
00:39I'll close the window, select the first keyframe of the actions layer and open
00:44up the Actions panel.
00:46So, here I have some code that adds an EventListener to the stage, listening for
00:50the KeyboardEvent.KEY_DOWN event, and then it runs a function call keyPressed.
00:56Instead of the keyPressed function, I can use something called the conditional
01:01statement to check which key was pressed.
01:04To do that, I'll put my cursor above the boarder_mc line of code, then press Return
01:12so I can get a space above that code.
01:15Here I'll type the word 'if' lowercase, some parentheses, and then I am going to
01:22wrap that code that says boarder_mc.x inside of curly braces.
01:32Just for presentation's sake, I can tab the boarder line of code in, just to clean
01:39up the code a little bit.
01:40In the parentheses after if, I'm going to type the condition that needs to be
01:45true in order for the boarder to move to the right.
01:51So, the condition is going to be if the key that was pressed is the right key on the keyboard.
01:58I mean right as in the direction right, and also right as a correct.
02:02So, if evt, and that's the event that gets received,
02:08That's an object that actually comes in to the function whenever a key is
02:12pressed, and that contains information like what key was pressed and other
02:16information like that.
02:19So, evt.keyCode. As soon as I type a K you'll see keycode highlighted in my Code
02:27Hinting window, so I'll just press Return and then a space and then two Equal signs.
02:33That's different than one Equal sign.
02:35One Equal sign is used to set a value like when you're setting the value for a
02:39variable, but two Equal signs is a comparison operator.
02:44Instead of being 'is,' like one Equal means, two Equal signs means 'is equal to.'
02:52So we use two Equal signs in conditional statements.
02:56So, event.keyCode is equal to, and then a space, and then I am going to type the
03:02right key command, which is keyboard.RIGHT, all caps.
03:11So, if the keycode of the event is the right key, or in other words is equal
03:17to the keycode of the right key on the keyboard, then move the boarder to the right.
03:23So let's test the movie now and see what happens.
03:26I am pressing the Spacebar.
03:27I am pressing the Return key.
03:29I am pressing up arrow and down arrow and left arrow; he doesn't move.
03:33I press the right arrow and the boarder moves.
03:41So naturally, if we move to the boarder to the right, we should also move him to the left.
03:45Let's just copy and paste this whole block so from the closed curly brace of the if statement up to
03:50the word if, copy that with Command or Ctrl+C, and then go down right below that
03:56curly brace, make sure you are not messing with curly brace for the function, but the
03:59curly brace for the if statement, and then paste the code.
04:03So, I have two if statements.
04:05Now, I am going to change the keyboard.RIGHT to keyboard.LEFT in the pasted code.
04:11Think about what you might change in the boarder_mc line to make the
04:15boarder move backwards.
04:19Simply change the Plus sign to a Minus sign.
04:22We added to its x position to move him to the right; we subtract from the x
04:26position to move him to the left.
04:28Test the movie, and he moves to right and left.
04:42Now, if the person playing the game presses the right and the left key at the
04:46same time, then there's going to be a conflict.
04:50So, what we want to do is make it so only one of these blocks of code runs.
04:56So, we only move the boarder to the left if the person playing the game is not
05:01pressing the Right key.
05:03To specify that, click before the second if statement and type the word 'else' and then a space.
05:11So, what this means is, first, we check to see if the Right key was pressed, and
05:20if not, or in another words 'else,' check to see if the Left key was pressed.
05:27In that case, move the boarder to the left.
05:31Now, you can keep having if statements and else if statements as many times as
05:35you want, and you'll have like a hierarchy with the else if statements.
05:39Finally, you can end a statement with an else statement.
05:42You can specify what you want to have if none of the conditions are met, so
05:46I'll type the word 'else' and then some curly braces, and inside of the curly braces, I'll type 'trace'
05:57and then in quotes, in the trace statement, I'll just type, "you didn't press left or right."
06:08So I'll test the movie and left or right moved the boarder, but if I push the up
06:12arrow then I get that you didn't press left or right message.
06:16So of course you can run whatever code you want in there, or not have the else
06:21statement altogether.
06:26Let's look at a different type of conditional statement.
06:28I am going to delete the code that I have here inside of the function, making
06:34sure to preserve the curly braces of the function.
06:37Then instead of using if and else, I'm going to use a conditional
06:41statement called switch case.
06:42I am going to type the word switch, and in parentheses I am going to type in 'evt.keyCode.'
06:49So that's the keycode of the event and some curly braces.
06:55The switch statement allows us to check one bit of data, in this case
07:00event.keyCode, and do different things based on the value of that
07:05particular piece of data.
07:06So when you're working with a keyPressed, this is more effective and requires
07:11less code than using an if statement.
07:15So with the switch statement, I type the word 'case,' and then I type the value in
07:22for the first example, so that's the Keyboard.RIGHT.
07:27So this is a different way of writing that if statement.
07:31This is basically saying the same thing: if event.keyCode is equal to
07:36Keyboard.RIGHT. Then after that, type a colon. Then go to the next line and then
07:44tell Flash what you want to do if the right key was pressed.
07:48So, that's boarder_mc.x += 5 and then a semicolon.
07:57Then to optimize a switch case statement, you tell the statement to stop looking
08:02for different options by going to the next line and typing the word 'break.'
08:08So this is like the first if statement that we created, case it's Keyboard.RIGHT,
08:13then move the boarder to the right five pixels.
08:15So, let's test this out and see how it works.
08:18It works exactly the same as the if statement.
08:20So, let's take those three lines, select them all, copy: Command+ C or Ctrl+C, go
08:29to the next line, paste the code.
08:32Now you can change Keyboard.RIGHT to Keyboard.LEFT, change the Plus to a Minus and
08:38test the movie again.
08:41So we can move the boarder right and left now, but the other keys also don't work.
08:49If you wanted to do a final else statement, like we did with the other one where
08:54none of the conditions are met, with a switch case statement, you use the default
08:58command, so we type 'default' and then a colon, and then you can type in a
09:05trace statement if you want, or you can type in nothing; it doesn't matter.
09:09So, in the trace statement, I'll put a different key was pressed, and then we end
09:19with a break statement.
09:21So you can see that this code is a little bit more concise than what we did
09:27with the if statement.
09:33So sometimes you are going to want to use the if statement, but for something
09:36like a keyPressed, the switch case statement is the better way to go.
09:39So, using conditional statements, you can add logic to your code and decide which
09:45code to run based on any criteria you choose.
Collapse this transcript
Creating arrays and vectors
00:00When you're working with large amounts of data, it is crucial to stay organized
00:04so that data can easily be accessed and utilized.
00:08Using arrays and vectors, it is easy to organize large amounts of data.
00:14Let's start by looking at an array.
00:17Let's say you wanted to load 50 different thumbnail images into a slideshow.
00:24Now it'd be nice to have some organized way to store all those image names, right?
00:30Well, with an array, you can do that.
00:34To create an array, type var, then a space, then name your array.
00:40I usually name my array something plural, so thumbnails, for example.
00:44I'd say these represent thumbnail images that you would load into Flash. So thumbnails:
00:53and now remember, you're telling Flash what type of data the variable is, and
00:58an array is an array.
00:59So type capital A and begin to type Array, and you'll see it selected in the
01:04code hinting area, and you could just double-click that or press Enter on your
01:07keyboard, and Flash will finish it for you.
01:10And then type space = space, and there are actually several different ways
01:15that you can create an array.
01:17I can type new Array, capital A, and Flash will create the array in memory.
01:26And then I can go to the next line, and every time I want to add something to
01:30the array, I can type thumbnails.push. This adds an item into the array, so I'll
01:38just double-click push.
01:40Notice it's an action word, so it's like a command, like a function.
01:44And inside the parentheses, I put whatever object I want to add.
01:48So in quotes, I would put something like image1.jpg.
01:53This isn't actually representing a real image.
01:56I'm just giving an example.
01:58So I'll close out the parentheses and type a semicolon.
02:01So if I wanted to add more images, I can just select this line of code, copy it
02:05with Command or Ctrl+C, go to the next line, paste with Command or Ctrl+V, and
02:10then repeat, and then change that to image1, image2, image3, image4.
02:16That's one way to create an array.
02:18I'm going to delete all these lines at the bottom now, and look at a
02:21shorthand way to do all this.
02:24Inside of the parentheses, when you create the array, that's the new Array parentheses,
02:29you can type comma-separated values.
02:32Type image1.jpg in quotes, and then a comma, a space, and then in quotes you can
02:37type image2.jpg, and so on.
02:43The last way to create an array is a really shorthand way, which just
02:47involves square brackets.
02:49So I can replace new Array and the open parenthesis with an open square bracket.
02:57That's right under the curly brace, so if you don't hold Shift and you press
03:01Curly Brace key then you'll get square bracket.
03:03Now I'll move to the right parenthesis and replace that with a right square bracket.
03:07So I've created an array, and now I have an organized way to store my data.
03:13Now I'm not going to type in all 50 images here, but you get the point.
03:16Now let's say you wanted to access one of the elements in the array.
03:21So I store all my data in this array, a big list, and then I want to pull out
03:26one of the elements in it.
03:28To do that, we're going to use something called array access notation, which
03:33sounds a lot more complicated than it is.
03:35All it is is square brackets.
03:37So go to the next line, type a trace statement, and in the parentheses type
03:45'thumbnails' and then some square brackets, and inside of the square brackets, type
03:52the number of the thumbnail you want to access.
03:56Now, arrays start at number zero, so image1.jpg would be at index zero of the
04:03array; image2.jpg would be at index 1;
04:07the next one would be at index 2; and then 3, and 4, and so on.
04:11So to access the first piece of data in the array I type a zero inside of
04:16the square brackets,
04:17and then I test the movie, and you can see I have access to that data.
04:24So right now that may not seem extremely useful, but you can imagine how much
04:29easier it would be to stay organized when you have large amounts of data.
04:32You put them into an array, and you can access the data using a simple
04:36array access notation.
04:38Another way to hold this of data is called a vector.
04:43Because vectors are Flash Player 10 only, and the syntax is drastically
04:49different from an array and anything else we'll be doing in this title, I'm
04:52going to focus on arrays for this title.
04:55But typically if you're using Flash Player 10, a vector is the more optimized option.
05:03Here is how to create a vector.
05:05Type the word 'var' and then a space and the name of the vector.
05:11I'm just going to call this 'v' type a colon to declare the data type and set it
05:17as a vector with capital V. And one of the differences between a vector and an
05:21array is that a vector can only hold one type of data that you specify. An array
05:27can hold numbers and strings at the same time, and a vector can only hold all
05:31strings, or all numbers, or all any other data type, but they all have to be the
05:37same data type in a vector.
05:39That's part of what keeps them optimized.
05:41So we're going to tell Flash that this vector will only contain strings.
05:45To do that, type a dot after Vector and then a less than sign, and in there, type a
05:52capital S, for String.
05:55So start to type String. Once it's highlighted, press Return and then close out with
05:59a greater than sign.
06:00This syntax here, the dot, less than, greater than sign, is something we're really
06:07not going to be working with in this title, which is, again, why I'm going to work
06:10with array in this title.
06:12So after that, type space, then a new, space, Vector, capital V, and you'll see that
06:20Vector.<String> is highlighted, so I'm just going to double-click that and then
06:24some parentheses and a semicolon.
06:27So that's how to create a vector that'll hold a string.
06:30Again, we're not going to be diving too deep into vectors in this title, but it
06:34is more optimized, and it's Flash Player 10 only, so if you want to use a Vector,
06:38you can look up the vector class in Flash help.
06:42The main thing I want you to take away is that arrays and vectors allow you to
06:46organize data for easy access.
Collapse this transcript
Using the For command to create a loop
00:00Sometimes you will want to run the same block of code many times, with little variation.
00:05For example, let's say you have a slideshow with a hundred images, and you need
00:10to load and place all of the thumbnails.
00:14It would be very tedious to copy and paste the loading and placing code 100 times.
00:20So to save time, and to be more organized, you can use a loop and write the code
00:26only once, and have it repeat itself a hundred times.
00:29What we're going to do here is use a loop to place the trashcans on the stage.
00:35I'm going to align the trashcans to the Y position of the second trashcan.
00:41If you don't have access to the exercise files, just create nine instances
00:45of any movie clip and name them can1_mc, can2 _mc, et cetera, all the way through can9_mc.
00:53Let's go to the first keyframe of the actions layer and open the Actions panel.
01:00Here I've added all these cans to a trashCans array.
01:06Note that these values are not in quotes, because they are referring to
01:11instances on the stage.
01:12Now let's take a look at writing a loop to modify the position of each of these objects.
01:19Type the word 'for' some parentheses and some curly braces.
01:23Notice the parentheses and curly braces are becoming common.
01:27We use them for functions, for the if statements, switch/case statements, and
01:33for this for loop here.
01:34So inside of the parentheses, type var space i, lowercase, colon, uint, u-i-n-t.
01:43This is another data type that starts with a lowercase letter. Set that = 0.
01:51By the way, uint is a positive integer, so a whole number, 0 and above.
01:59Semicolon after that 0; and i, less than.
02:05This is going to run nine times, once for each of the trashcans, a
02:11semicolon, and then i++.
02:14Let's walk through what this line of code does.
02:17These three different sections of the loop specify different things.
02:22The first part is called the iterator variable.
02:27This is going to be a variable that we'll use inside of the loop.
02:31So we declare the variable, just like any other variable. We call it i for
02:36iterator, but it could be anything you want.
02:41Declare the datatype as a positive integer and set the value = 0.
02:46That's a starting value for the loop.
02:48You can set it to any value you want, but most of the time it's going to be zero.
02:53The next part refers to how many times the loop will run.
02:57The loop will run as long as i < 9.
03:02Finally, you explain what will happen after each time the loop runs. i++ means to
03:09increment I by 1 or add 1 to it each time the loop runs.
03:13So just to see the loop in action, type 'trace' inside of the loop and in the
03:20parentheses for the trace statement, just type i. Make sure not to put it in quotes.
03:25So if you test the movie and take a look in the Output window, you'll see the
03:31numbers 0 through 8.
03:34Let's go back to the code, and that corresponds to an index of the array, or at
03:44least we can tell Flash to make that correspond to an index of the array.
03:47So what we'll do is erase the trace statement and place the trashcans.
03:55To do that, we'll reference the trashcans through the array.
04:00So type trashCans, index i, which means you put i inside of square brackets, so
04:08that's referring to a trashcan each time, and then type .y. That refers to the
04:17vertical position of the object. And we'll set that = can2_mc.y; so we're
04:26aligning each object to the Y position of can2_mc.
04:32So let's test the movie and see the code in action.
04:35Now, all the trashcans are aligned.
04:38Remember, if you look at the stage, they're not aligned.
04:40So you can use a loop to do that.
04:43And the great thing about it is it doesn't matter if we have 9 trashcans or
04:4810,000 trashcans; modifying this code is as simple as modifying that number.
04:53Finally, if you really want to optimize this code and have it update based on
05:01the size of the array, you can change the 9 value to be the length of the array,
05:08or how many items are in the array. So change that to trashCans.length.
05:17Again, that refers to the number of items that are inside of the array,
05:20which right now is nine.
05:22That way if you ever change the array, you won't have to update the for loop.
05:28To test the movie again, show that it's working.
05:31Now, if I were to click and drag and erase, let's say almost all of the cans
05:37inside of the array, so I only have three cans now,
05:39if I test the movie, then only the first three cans will be aligned, so I don't
05:46have to update the loop.
05:47So this is a pretty a simple example, but you can see how a loop can save you so much time.
05:55Using a for loop, you can perform the same block of code on every object in an
06:00array and save loads of time building your applications.
Collapse this transcript
3. Working with the Display List
Reviewing the display list
00:00Before writing code to manipulate objects in Flash, it is important to understand
00:05how Flash organizes those objects.
00:07In ActionScript, this is referred to as the display list.
00:11The display list contains two primary types of objects:
00:15display objects and display object containers.
00:18Display objects are visual objects, like shapes, movie clips, buttons, and text fields.
00:26So I have a bunch of display objects on the stage.
00:28If I select them, you can see in the Properties panel the Flash does not call
00:33them display objects; rather, there are their specific names, like movie clips,
00:38a bitmap image, et cetera, but these all fall under the category of display objects.
00:45Some display objects are display object containers.
00:50Display object containers can contain other display objects and display object containers.
00:57Now, that may sound a kind of confusing. Just think of a movie clip.
01:01So on the stage here I have this border object and if I double-click the border,
01:05I'll enter its Timeline.
01:07So the movie clip is a container.
01:10It's simply a container, holding all of these frames.
01:13So I can click and drag. We can see that.
01:19And inside of a movie clip, I can have shapes, which are display objects, or I
01:24can have other movie clips, which are display object containers.
01:27For example, I have this Fall movie clip, and if I double-click it, you can see it
01:33has its own display object inside of it.
01:36There is a bitmap image inside.
01:38So in code, a movie clip would be considered a descendant of the display
01:44object container, because it has the same properties that display object containers have.
01:50A bitmap or a shape or a text field would be a descendant, also known as a
01:56subclass, of the display object group, or class.
02:00Now I'll return to the main Timeline.
02:03All display objects in an application are on the stage.
02:08You may remember, in another movie, we added a keyboard event listener to this
02:13stage with a lowercase s, and that's were all display objects and display
02:18object containers reside.
02:20Another thing you need to understand about the display list is the language
02:24between children and parents.
02:26So the boarder on the stage would be considered a child of the stage.
02:31Now if I double-click the boarder and enter its Timeline, then from here, the stage
02:36would be the boarder's parent.
02:38And yes, it is correct to call things grandparents and grandchildren, but
02:43understanding that relationship is key to working with visual objects in Flash.
02:48So to recap, the display list is the term used to describe Flash's system
02:53for organization for visual objects, and consists of display objects and
02:58display object containers.
Collapse this transcript
Understanding instances referenced through ActionScript
00:00One of the more common tasks when working with ActionScript in Flash Pro is to
00:04use ActionScript code to manipulate objects on the stage.
00:08Now you probably already have experience with this.
00:10You have even done that in this title.
00:12The easiest way to do that is to give an object an instance name and use that
00:16instance name to reference the object in the Actions panel.
00:19From the stage here, if I select my border, you will see it's a movie clip, and
00:23it has an instance name of boarder_mc.
00:26Note that you won't see the instance name field unless you have a particular
00:30type of object selected.
00:31Instance names are only applicable to movie clips, buttons, some types of text
00:37fields, and components, like the FLV playback component.
00:40Note that my instance name is boarder_mc.
00:44Instance names are actually variables that are created behind the scenes in
00:48Flash, and need to follow the same naming conventions.
00:51In other words, you can't start with a number - so you should start with a
00:55letter, and you cannot include any spaces or special characters.
00:59I've followed this naming convention for a few years now, so I am used to ending
01:03every movie clip instance name with _mc.
01:07A movie clip instance name ending in _mc in previous versions of Flash would
01:12give you access to instant code hinting.
01:14And in this version of Flash, that feature does not exist, so you don't really
01:19need to have that in there.
01:20That's just something I still do because I am used to doing it.
01:23So again, for you, that is optional.
01:25Let's go to the first keyframe of the actions layer and open up the Actions panel.
01:29So if we want to reference the boarder, all we have to do is type that instance
01:33name in our code, boarder_mc.
01:35If you want to see what Flash sees when you type boarder_mc, you can trace
01:41boarder_mc and test the movie.
01:43You can see that Flash sees an object that is an instance of boarder_4.
01:50The boarder part comes from that boarder symbol that we created, and the
01:54object is an ActionScript object, which is basically an instance.
01:59So again, Flash creates these instances behind the scenes. So that you
02:04don't create a conflict,
02:05don't create variables with the same names as your instance names, or else
02:10Flash may break that connection that you set up by giving an object an instance name.
02:14So to recap, you can apply an instance name to certain types of objects by
02:19selecting them and going to the Properties panel.
02:21By adding an instance name to an object, you can easily modify that object
02:25using ActionScript.
Collapse this transcript
Using dot syntax to modify properties in an instance
00:00To modify an instance using ActionScript, you can use something called dot syntax.
00:05Don't let the name scare you though; dot syntax is simple and easy to learn, even
00:09for the newest programmers.
00:11On the stage I have an instance of a movie clip called boarder_mc.
00:15Make sure to create a movie clip instance and name it, just like mine is named here.
00:19Let's go to the first keyframe in the actions layer and open up the Actions panel.
00:23Dot syntax simply means you can modify an object's properties by typing a dot.
00:29The real special thing about dot syntax in Flash is that you can get code
00:33hinting when typing a dot.
00:34To get code hinting, I am going to make a variable to hold the boarder on the stage.
00:40Now this may seem a little bit counterintuitive, because we have already
00:42given it an instance name, and I am not going to directly use that instance
00:45name to modify the boarder.
00:47I will show you why in just a second.
00:48Type var and then a space, and we will call this boarder, different from
00:53boarder_mc, which is the instance name of the border.
00:56I am going to data type this to a movie clip, and
00:59I am going to set it equal to boarder_mc.
01:02That's the name of the movie clip instance on stage.
01:04Now here is why I do that.
01:07If I want to modify the boarder's properties, I'll just type boarder and then a dot.
01:12I can scroll through a menu,
01:14pick different properties and methods that I can apply to this object.
01:18Let's take a look at one of them.
01:19I am going to double-click on alpha.
01:21This value is a numeric value between 0 and 1 and controls the alpha
01:26transparency of an object.
01:28So if you want to bring its alpha down to 25%, then the value is not 25 - it's 0.25.
01:34boarder.alpha = 0.25.
01:34So I will test the movie, and you can see a semi-transparent boarder on stage.
01:40Some other properties you can modify are x. You have seen that before in this
01:44title, so I can set it equal to 0,
01:47make the boarder go to the left edge of the stage.
01:49I can set it to 500 and
01:51move the character to the right.
01:53Another position property is Y, which controls the vertical position. If I set Y to 500,
01:58the boarder is at the bottom of the stage.
02:00If I set Y to 0, the boarder goes to the top.
02:03Finally, I would like to show you rotation, which allows you to, obviously, rotate
02:08an object, but keep in mind this is not animated rotation;
02:11this is just simply modifying the rotation property.
02:14So I will set the value to 45, and you can see the boarder is rotated 45 degrees.
02:19You can go to -45. And keep in mind that values for rotation
02:24are between -180 and +180.
02:28Also, position and rotation are all relative to the registration point of the object.
02:32See here that the cross is at the top left of my movie clip.
02:36Let's say I were to create another movie clip.
02:38I will just take this trashcan and go Modify > Convert to Symbol, and this
02:44opens a Convert to Symbol dialog, which allows you to set the registration.
02:47The default is top-left, but you can also set it to the center, the bottom-right
02:51or wherever you want.
02:52For the most part, you're going to want to keep registration at the top-left, but if
02:55you're ever going to rotate an object using ActionScript, especially if you are
02:59going to animate the rotation, you might want to have the object's registration
03:02point to be in the center.
03:03So, that's the XY origin of an object when you create it as a symbol.
03:07I am going to cancel out of that menu now.
03:09So wherever the registration point is, you can still manipulate the object using dot syntax.
03:14So to review, dot syntax allows you to manipulate objects by using a dot after
03:19the object's name, which opens the Code Hinting window for easy access to all
03:24the object's properties.
Collapse this transcript
Placing objects at the center of the stage
00:00One of the common tasks in ActionScript is to align an object to the center of the Stage.
00:06On the Stage here, I have an instance called boarder_mc and then a trashcan
00:12that I call trash_mc.
00:14So let's go to the first keyframe in the actions layer and open up the Actions panel.
00:18To align an object to the center of the state horizontally, modify the objects X
00:23property, so boarder_mc.x =.
00:27Now one option would be to center the object to the Stage with the Align panel
00:32and write down the object's position and then write it in right here.
00:36Though that would work, the code would break if you were to change the size of your document.
00:41So you need something that's always going to represent the entire width of the Stage.
00:47You can get that information to the stage.stageWidth property.
00:51So if I set this X position equal to stage.stageWidth, the registration
00:56point would be aligned with the right edge of the Stage, and you wouldn't be
00:59able to see the boarder.
01:01So we are going to do some simple math to put him in the center, by dividing that by 2.
01:05The Division symbol is simply a forward slash.
01:07So there is the border in the center of the Stage, well almost.
01:12His left edge is aligned with the center of the Stage, because his registration
01:17is on his left side.
01:19So in order to completely align the boarder, we are going to need to shift him to
01:23the left a little bit.
01:24So after the 2, I'll type space minus space boarder_mc dot width. That's how you access the width of a
01:33movie clip, and then we take that, and we divide that number by two as well.
01:37We will test the movie, and there the boarder is at the center of the Stage.
01:41If you want to test yourself, pause the movie and try to center the
01:46boarder vertically.
01:48To do that, all I am going to do is take this line of code, copy it, paste it on
01:53the next line, Ctrl+C or Command+C to copy, Ctrl+V or Command+V to paste.
01:58Change the x to a y, change StageWidth to StageHeight and change boarder_mc.width
02:04to boarder_mc.height.
02:06That's it. Test the movie.
02:08And the boarder is in the center of the Stage.
02:10So here is the code to center an object to the Stage.
02:13What if you want to reuse this?
02:15All you have to do is wrap it inside of a function.
02:18So above all my code, I am going to create a function. Type the word 'function'
02:22and a space, and I will call this 'align to center,' I'm writing that in that CamelCase, so
02:27capital T and capital C; everything else is lower case.
02:31Parentheses colon void, and I am going to wrap these two lines of code in curly
02:36braces and tab them in, just to stay clean.
02:39Now what I am going to do is put a parameter inside of alignToCenter, so that I
02:45can align any object to the center of the Stage.
02:48Obj, short for object, inside of the parentheses, after alignToCenter, we're going to
02:54datatype this as a MovieClip.
02:56Flash will automatically add that import statement.
02:58And then I am going to replace boarder_mc with obj.
03:02I am just going to copy and paste over each time it says boarder_mc.
03:07Now I can center any object to the center of the Stage.
03:10For example, if I wanted to center the trashcan, all I have to do is find an
03:15empty line on my code and type alignToCenter, capital T, capital C. Pass in the
03:22movie clip I want to align, trash_mc, close up the parentheses, add a semicolon.
03:28Test the movie, and the trashcan is at the center of the Stage.
03:32If I want to change it back to the boarder, all I do is change trash_mc to
03:36boarder_mc, and there is the boarder at the center of the Stage.
03:39It's as simple as that.
03:41So now you have a reusable function to center any object to the Stage.
03:45So, remember, you can center an object to the Stage by positioning the object at
03:51the width of the Stage divided by two, minus half the width of the object.
03:56The same rule applies with a Y position for the Stage height and the
04:01object's height.
Collapse this transcript
Placing objects at the edges of the stage
00:00Often when working with ActionScript, you want to align an object to the edge
00:05of the Stage, or particularly in a game, detect whether the object has touched
00:09the edge of the Stage.
00:11On the Stage, here I have boarder_mc and trash_mc.
00:15I'll select the first keyframe in the actions layer, and open up the Actions panel.
00:21Centering an object to the left or top edge of the Stage is really easy, if
00:26you have top-left registration.
00:28Type boarder_mc.x = 0 and test the movie.
00:34So there is the border at the left edge of the Stage.
00:40I can copy and paste this line of code - Command+C or Ctrl+C to copy, Command+V or
00:45Ctrl+V to paste - and simply change x to y in the pasted code and test the movie
00:52to see the boarder aligned to the top edge of the Stage.
00:56It's a little different if you want to the object to be aligned to the right
00:59edge of the state, or to detect whether an object is at the right edge of the Stage.
01:03The right edge of the Stage can be reached by Stage.StageWidth - remember that
01:09always represents the width of the Stage, minus the width of the object.
01:15Because remember, if we set the x position of the boarder at the StageWidth, then
01:20it would be off the Stage because the left edge of the border would be aligned
01:24with the right edge of the Stage.
01:25So after the Minus sign, we'll offset that by boarder_mc.width.
01:32Test the movie, see the boarder at the top-right of the Stage.
01:34Now if I simply copy and paste this line of code, change x to y, StageWidth to
01:42StageHeight, and boarder_mc. width to boarder_mc.height,
01:50test the movie, and the same principle applies.
01:54Now let's say you wanted to reuse this code and put it inside of a function.
01:58Let's wrap this code around a function. Push everything down, create a function
02:03called alignToEdge, parentheses, colon, void, tab the lines in and wrap
02:11everything with curly braces.
02:13So now I've wrapped this into a function. I can just call it whenever I want.
02:17I am going to write obj, as a parameter, datatype it to a MovieClip, and then
02:22change boarder_mc to obj inside of the function.
02:27That way, anytime I want to align an object to the edge of the Stage, I can run
02:31the alignToEdge function and pass in whatever object I want, and it won't just
02:36apply to the boarder.
02:37Let's go below the function, and then I am going to type alignToEdge.
02:42Then I am going to pass in trash_mc.
02:46So we'll align that to the bottom-right edge of the Stage.
02:49And test the movie, and it applies to the trashcan.
02:54So if I were to change that to boarder_mc, it would apply to the boarder.
03:00So I can apply it to any object I want, as many times as I want.
03:04Now if you really wanted to add features in this function, you can have it
03:07specify whether you want to align to the left, or to the right, or to the top, or to the bottom.
03:14Let's look at how to do that.
03:15After MovieClip, inside of the parameters area of the alignToEdge function,
03:20type a comma and then type a t. This will represent whether you want to align to
03:25the top edge of the Stage.
03:27Type a colon after the t and then type Boolean.
03:29This is going to be a true or false value.
03:31We'll do the same thing for the right, with an r, also Boolean, for the bottom - I
03:39am working clockwise, you see -
03:41Boolean, and the left which will also be Boolean.
03:45So I can specify how I want to align the object to the Stage.
03:49And in here I can use IF statements to determine how I want to align the object.
03:53All right, inside of the function, I am going to use an IF statement, and I am
03:57just going to see if t is true.
04:00If t is true, then I'm going to align the object to the top edge of the Stage.
04:05So obj.y equals zero.
04:09I can copy and paste this IF statement, change t to l, representing the left
04:16edge of the Stage. If that's true then we can set the x position to zero.
04:21I'll go down below the curly braces, I'll type if r, in parentheses, and wrap
04:27this next line of code into curly braces.
04:29So this will align to the right edge of the Stage if r is true.
04:33And lastly, check to see if b. If we want to align to the bottom edge of the
04:38Stage, tab that code in and wrap it in curly braces.
04:42So now we'll scroll down, and let's say we want to align the boarder to the
04:47bottom-left of the Stage.
04:49So when we run alignToEdge, we're going to pass in those four Boolean
04:54parameters; remember, Boolean is simply true or false.
04:58So after boarder_mc, I'm going to type a comma, and then I have - my code hinting
05:04even shows me - t, r, b, l. So top is going to be false, right is going to be
05:11false, bottom is going to be true and left is going to be true.
05:16So I'll test the movie, and the boarder is aligned to the bottom-left of the Stage.
05:22So you can pass in whatever parameters you want to align any object to any
05:26corner of the Stage.
05:28Now, if any of this got a little bit confusing, don't worry.
05:31I just went out there for people who want to learn a little bit more and go a
05:34little bit deeper into ActionScript.
05:36If you take anything from this movie, just take away that you can align the
05:40object to the top-left, by setting its x and y positions 0, and you can align an
05:45object to the bottom-right by setting its position to the width or height of the
05:49Stage, minus the width or height of the object.
Collapse this transcript
Using the methods of an instance
00:00Variables are to properties as functions are to methods.
00:04In other words, methods are functions that are connected to objects. Just like
00:08accessing properties of an object, accessing an object's methods requires simple dot syntax.
00:14Consider this boarder on the Stage. Again, its instance name is boarder_mc.
00:20If I test the movie, you'll see that it animates to make him fall down.
00:25That's because I removed the stop action in his first frame, so if I double-click
00:30the movie clip, you won't see any action on the first frame.
00:32So the animation plays, and then there is a stop action on the last frame, as
00:36you can see right here.
00:38Just ignore the other code for now. We'll talk about that later.
00:40So I go back to scene one, and let's say I wanted him to stop on frame 1.
00:45Go to the first key from the actions layer, and open up the Actions panel.
00:50Now you may want to access to code hinting, so let's just type 'var boarder,' and
00:55datatype him as a MovieClip, and we'll set it equal to boarder_mc.
01:00Now remember this means exactly the same thing.
01:03We could just type boarder_mc instead of boarder, but this is just going to give
01:07us access to code hintings. So you can do this whatever way you want.
01:09So go down a few lines and type 'boarder.' and then start to type stop, and
01:15you'll see it highlighted in the Code Hinting window.
01:18Double-click that. Flash types the open parenthesis for you.
01:21You can close it out and add a semicolon.
01:23So instead of just stop, like when you want to stop the main Timeline, you type
01:28the objects name, a dot and then stop, and you are sending a command to that
01:33object to stop playing.
01:35So when I test the movie now, the boarder won't animate.
01:38So let's say I wanted to have the boarder stop.
01:42I want it at a later frame.
01:43So let's say I want it to stop at frame 30, or something like that.
01:47I can use to gotoAndStop command, so boarder.gotoAnd, and then capital S, and
01:55Flash will highlight gotoAndStop, and press Return, and Flash tells me which
02:00values that I need to pass into the method.
02:02The only one I need a pass in is called Frame.
02:05So I can just type in a frame number.
02:07So I'll type in 30, close up the parentheses and the test movie, and
02:11there is the boarder on the ground.
02:15I can also type gotoAndPlay, so I'll just change Stop to Play, capital P, and
02:20then let's say frame 2, and then you'll see the boarder animate again.
02:24Let's say you wanted the boarder to fall down when you clicked on him.
02:28So the first thing we'll do is to make him stop.
02:30So right above gotoAndPlay, I'll type boarder.stop. Make sure to close out the
02:35parenthesis and add a semicolon, and then we'll go down a few lines and type
02:39boarder.addEventListener.
02:42This is actually another method that the boarder has, and the event is going to
02:46be MouseEvent.CLICK.
02:49As I type MouseEvent, you'll to start to see MouseEvent.CLICK highlighted, so
02:53might as well type that and have Flash write the rest of the code for you.
02:56And then type a comma and then type clickedBoarder.
02:59Go down a few more lines and create the event handler function, so function
03:06clickedBoarder, some parentheses, inside of the parentheses, evt:MouseEvent.
03:15After the parentheses, type :void, all lowercase, wrap boarder.gotoAndPlay(2)
03:21inside of the curly braces.
03:22So now this code will be executed only when you click on the boarder, because
03:26we've attached this function to an eventListener.
03:29So we'll test the movie, click on the boarder, watch him fall.
03:37So using dot syntax, you can access methods of an object and get all the code
03:42hinting you need to run methods without errors.
Collapse this transcript
Accessing the parents, children, and grandchildren of objects
00:00Your knowledge of the display list can be used to access any object in Flash,
00:05from virtually any location.
00:07One example of that is the parent property, which allows you to communicate
00:11backwards from one object to its parent display object container.
00:15On the Stage here, I have boarder_mc, which is an animation.
00:20If I double-click on that movie clip, you can see the animation.
00:26Inside of that movie clip, on frame 1, I have an instance called boarderSolo.
00:32If I double-click on that instance, I'll enter a movie clip called boarderSolo.
00:36You'll see that I have the basic bitmap shape with the boarder and then the
00:40skateboard inside of a movie clip, with an instance name of board.
00:44So let's say I want to communicate to the skateboard.
00:48In order to do that, I'll need to communicate to the boarder itself, which is
00:52inside of the boarder_mc movie clip.
00:55Let's go to the first keyframe of the actions layer, and open up the Actions panel.
01:00In here, under the line of code where I created the variable, I'll type
01:05boarder.boarderSolo.alpha = .25;
01:14so to access the solo boarder, I can simply use dot syntax.
01:19So boarder, which is the outside boarder or boarder_mc.boardersSolo, is the
01:25boarder itself - not the animation. Then I can tweak that by modifying its
01:30properties with dot syntax.
01:31So let's test the movie.
01:33Now we see the boarder's alpha is .25, but watch when I click the boarder. The alpha goes up.
01:39Take a second to think about why that is.
01:41Are we modifying the boarder's alpha,
01:44or are we modifying the solo boarder's alpha?
01:48Look at the code. boarder, which is boarder_mc, or the animation,
01:52.boarderSolo.alpha, so we are only modifying that first frame.
01:57So we are modifying the instance on that first frame of boarder_mc.
02:03Now take a second to think about how you would access the skateboard, if you
02:07only wanted to modify the skateboard.
02:09Just like accessing the solo boarder, you can access the skateboard through dot syntax.
02:15So after boarderSolo, type .board, and you can modify its properties.
02:21So let's see what happens when we set the x position of the board to 0.
02:27I'll test the movie, and it appears that the code is not working, because the
02:33board doesn't seem to be moved;
02:36however, the board's position is at 0, relative to its parent movie clip.
02:43So if I want to move it backwards, I'd have to give it a negative value.
02:50So I could then type -25, test the movie, and the board has shifted 25 pixels to the left.
02:59So using dot syntax, you can modify child and grandchild elements,
03:04but what if you want to modify a parent element?
03:08Let's look at how to do that.
03:09At the bottom of my code, I created a simple function called restart. This would
03:15represent restarting the game.
03:17All it does is it puts the boarder on the first frame and then sets his x position to 0.
03:23Let's say we wanted to run this function from inside of the boarder.
03:29Let's take a look at why you might want to do that.
03:31Close the Actions panel and double-click the boarder on Stage to enter its Timeline.
03:37So let's say you click on the boarder and he plays his closing animation
03:42where he falls down.
03:44Once the animation is complete, you want to send the boarder back to his
03:48original state by running that restart function on the main Timeline.
03:53So here's an instance where you want some code to trigger after an animation has
03:57played, so it makes sense to put code inside of the Timeline of a movie clip.
04:02I'll select the last keyframe with the actions layer, and open up the Actions
04:06panel. Make sure you're inside of that boarder movie clip.
04:09So that first frame says stop.
04:11Now to communicate to an object's parent, all you need to do is use the Parent command.
04:17So type parent.restart();.
04:20Now by itself, this code may not work.
04:23So if I test the movie, I get an error.
04:28Flash tells me, Call to a possibly undefined method restart.
04:33Let me explain why this is happening.
04:37Flash knows who the parent of this object is;
04:40it's the main Timeline.
04:42Flash also knows that there is a restart function on the main Timeline.
04:46Here's the problem:
04:47the datatype for this parent property is not a movie clip;
04:52it is the display object, and a display object does not have a function called
04:58restart built-into it.
05:00So what I need do is tell Flash to treat this as a different type of object.
05:05So before parent, I'll type Object, with a capital O, and wrap parent in parentheses.
05:11Object is basically a generic object that can have any properties and methods.
05:16So this is the easiest way to do this.
05:18Type Object, capital O, wrap parent in parentheses and after the close
05:21parenthesis, type .restart. Test the movie.
05:25You don't get any errors.
05:27If you click the boarder, he'll fall off, then return to the left edge of the Stage on frame 1.
05:35So this technique where we wrapped parent inside of Object and told Flash to
05:39treat it as a different type of data is called Typecasting.
05:42So using the parent property with the Typecasting technique, you can access any
05:47object from within any timeline of your application.
Collapse this transcript
Creating instances from Library movie clips using ActionScript
00:00Many types of apps, especially games, need to be able to re-spawn or
00:04clone different assets.
00:06In order to do this in Flash, you have to create new instances from
00:09library symbols at run time.
00:11So let's imagine a scenario - go ahead, and test the movie here - and we have
00:15this skateboarding game.
00:16Let's say as a skateboarder is going through the town, the whole background is
00:20scrolling to the left, and as the boarder goes to the town, there are going to
00:25be little obstacles that come up, like trashcans.
00:27One way to create the obstacles is to build them in Flash and have them be part
00:34of your FLA file from the beginning.
00:35However, that creates more objects in memory that you don't need because they
00:39won't always be on the Stage.
00:41The most effective way to create a game is to have objects on the Stage appear
00:47immediately when you need them, and delete them as soon as you don't need them anymore.
00:52The experience is the same to the user, but as you program the game, you get
00:56much better performance.
00:58So the thing we want to do is add trashcans to the game from the library, while
01:03the Flash movie is playing.
01:04That way we don't have to have them taking up more memory than is necessary.
01:09The first step to do that is to create a MovieClip symbol.
01:12So I already have one called TrashCan.
01:14If I right-click TrashCan, and then choose Properties, I can tell Flash that I'm
01:21going to want to create new instances of this symbol using ActionScript.
01:25To do that, make sure you're in Advanced mode.
01:28You may have to click the Advanced button at the bottom-left of the
01:31Symbol Properties window.
01:32So you should see this Expanded View here.
01:35Simply click the Export for ActionScript box in the Linkage area.
01:39In the Class field, you'll see the name of the MovieClip symbol.
01:43This is the name that you're going to refer to this object by when you create
01:47it with ActionScript.
01:48The Base class says that this is essentially a movie clip.
01:53Now, we're going to get into a lot more detail about what Class and Base Class
01:57mean later on, but for now just know, you just need to remember TrashCan.
02:02So click OK. Flash will give you messages that says, A definition to for this
02:06class could not be found in the classpath.
02:08It's not an error message;
02:09it's just a warning message, so I'll just click OK.
02:12Again, we'll talk more about how that works and why you got that warning message later on.
02:16And then look into library and see that there is something under Linkage for
02:20your TrashCan MovieClip.
02:22So Flash is telling me this is exported for ActionScript as TrashCan. So it's
02:27nice and easy to remember because it's the same name as my MovieClip symbol.
02:29Let's go over to the first keyframe of the actions layer, and go ahead and open
02:33up the Actions panel.
02:34Create a variable called can and datatype it to TrashCan.
02:39You can also datatype it to a movie clip since it's basically a descendent of a
02:44movie clip, but for this purpose I want to datatype it as a TrashCan.
02:48So click OK and then type new, space, TrashCan, capital T, capital C;
02:55and parentheses and a semicolon.
02:57At this point, if you want, you can test the movie to make sure you don't have any errors.
03:00It looks like everything is fine here.
03:04And now what I'm going to do is place the object on the Stage.
03:09To do that, use a method called Add Child.
03:13Type add, and then Child, with a capital C, and in the parentheses, type can.
03:20This adds an object to the Stage,
03:23so it's going to put the can on the main Timeline.
03:25We'll talk more about the Add Child method later.
03:29See the can at the top-left of the Stage?
03:32That's the can that we pulled out of the Library using ActionScript.
03:36Now let's just place it in a normal spot, so it's not floating in the air.
03:40The reason why it's floating in the air is because it goes to 0,0 coordinate
03:46automatically when you put addChild.
03:48So let's set its x and y position: can.x equals 577
03:53and can.y equals 493.
03:57In case you're wondering where I got these numbers, I put the can where I wanted
04:01it, and then I just jotted the numbers down in the Properties panel.
04:05So, test the movie, and there is the trashcan right there.
04:09If you remember, the trashcan is not on the Stage in that position, so it's been
04:15placed there using ActionScript code.
04:18So using this technique, you can draw objects out of the library as you
04:22need them.
Collapse this transcript
Adding objects to the stage and changing parents with the addChild method
00:00Placing objects on the Stage is an essential skill when working with AS3
00:04in Flash Professional.
00:06Sometimes, however, you will want to move an object into a different container
00:10once you have already placed it in one.
00:12For example, here I'm creating a trashcan from the library and placing it on the Stage.
00:19I'm adding it as a child object of the Stage.
00:22But what if I started creating more of these trashcans?
00:26For example, I could copy and paste this code, change the name of can, to can1
00:38and change the position of the can to 677 pixels.
00:43So I test the movie, and I have two cans.
00:46What if I'm going to have many cans, like a hundred cans?
00:50It might make sense to start placing them inside of a container.
00:53Let's take a look at how to do that.
00:56Above all this code, I'm going to create a new variable.
00:59I'm going to call this 'container.' I'm going to datatype it to a sprite.
01:03Now a sprite might be a new concept to you, but it's actually, fairly simple.
01:07Think of a sprite as a movie clip with only one frame.
01:11That's all there is to it.
01:13It's a movie clip, but it takes up less memory in the Flash Player.
01:17It's also ActionScript only.
01:19So, I'm going to create a new sprite here.
01:21So container, datatype it to a sprite.
01:24Space, equals, space, new, space, Sprite, with a capital S. Some open and close
01:30parenthesis and a semicolon.
01:31So that creates a new, empty sprite, which is basically like an empty movie clip.
01:36So, now what I'll do is I will place the container on the Stage.
01:42So I'll use the addChild method.
01:44Remember that if you forget to use the addChild method, you'll never see your
01:48object on the Stage.
01:49Creating a new instance of an object simply creates it in memory.
01:53addChild is what allows you to see the object, so you need to put it in
01:57some sort of Timeline.
01:59So, I'm going to add the container to the Stage, and then I'm going to set this
02:03container's x and y equal to 577 for x, and container.y = 493.
02:14Notice that they are the same coordinates that I used for the original trashcan.
02:18Now I'm going to delete the coordinates for the original trashcan, and I'm going
02:24to delete the coordinates for can1.
02:25Now let's test the movie.
02:29So I can't see the sprite container, but it's in this area over here.
02:35It's invisible because it's an empty container, with nothing inside of it.
02:39I do see the trashcans at the left of the Stage.
02:42Now, if I want to put the trashcans inside of the container, all I have to do
02:49is have the container be the one that's adding the child, instead of the main Timeline.
02:53So, right before addChild(can), I can type container.addChild(can).
02:59So I can do that in both instances.
03:04For the second can, which is can1, I'll set its x-position to 100, can1.x =100.
03:12Remember these coordinates are going to be relative to the container object.
03:16So a 0,0 position will start at the container's x and y position.
03:21So test the movie, and there are the trashcans.
03:25The thing that's great about this is when I move the container, I can also
03:30move the trashcans.
03:31So at the bottom of my code, let's say I want to move the container 100
03:33pixels to the left,
03:35I could type container.x -=.
03:39This is a shorthand way of subtracting a value from its current position, = 100.
03:46So, it's going to take the position of the container and shift it 100 pixels to the left.
03:51So test the movie, and you can see the container is all the way over there.
03:55For a more dramatic effect, I'll put in 500.
03:59The trashcans are over there.
04:01So this is an easy way to stay organized because I can move all of my trashcans
04:05at the same pace, because they're in the same container.
04:08Now there is one more thing I want to look at.
04:11You can actually reparent a child object if you've already added it to the Stage.
04:17So let's take this code, container.addChild (can1), and copy and paste it a few lines down.
04:24I'm going to temporarily disable the line of code that I pasted by pressing two
04:29forward slashes at the beginning of the line.
04:32So, it'll turn gray, and Flash will not process that code.
04:35I'm going to erase container.before addChild, and then we'll test the movie.
04:42So can1 is 100 pixels from the left edge of the Stage, and it is now a child of
04:47the main Timeline, unlike the original can that's a child of the container movie
04:53clip, which you can tell because it's at the bottom-right of the Stage.
04:59Now if I uncomment this line of code and Flash processes it, you'll see that
05:04the can goes from being a child of the main Timeline to being a child of the container.
05:11So test the movie, and now the second can is a child of the container.
05:16Notice that we don't see it move from the one place to another because all this
05:21code is processed before the frame is drawn.
05:24So if you ever need to, you can reparent a child object to another parent
05:28by simply calling the new parent's addChild method and passing in the new child object.
05:35So to recap, the addChild method allows you to place a display object, or any
05:40other visual object, inside of a display object container.
Collapse this transcript
Removing objects from the stage with the removeChild method
00:00When you destroy an enemy object in the game or click to close a menu in one
00:05of your apps, you may need to free those objects from memory to increase performance.
00:09While you can't control which objects Flash deletes from memory, you can control
00:14which objects are eligible for garbage collection.
00:17Garbage collection refers to when Flash incrementally cleans up memory.
00:23When your movie begins to take up too much memory, now this 'too much' is not
00:28really defined by Flash or Adobe, Flash will do something called garbage
00:33collection, and take objects that are no longer being used and throw them out,
00:38deleting them from memory.
00:40So in order to make sure that objects get deleted when Flash does
00:44garbage collection, you need to make sure the objects are eligible for
00:48garbage collection.
00:49So on this Stage here I have boarder_mc and a movie clip called can.
00:55Now, boarder_mc has an animation that starts on frame 2, and the frame 1 is stopped.
01:01Let's go to the first keyframe in the actions layer and open up the Actions panel.
01:07So the boarder can be controlled with the keyboard, and in that keyboard block
01:12of code, I checked to see if the boarder is touching the can.
01:17That's a method called hitTestObject, so boarder_mc.hitTestObject, capital T,
01:25capital O, and in parentheses (can).
01:28So if the boarder is touching the can, then run the block of code in curly
01:32braces, which will play at the second frame of the boarder movie clip.
01:35Let's see how that looks.
01:38So I'll move the boarder to the right.
01:41When it hits the trashcan, I'll release my finger off the keyboard and see
01:45him animate downward.
01:47Let's say you wanted to remove the trashcan when he hits it.
01:52Go back to the code, and below that line of code, type removeChild, can, in parentheses.
02:00Make sure child has a capital C, and that can is spelt just like it is in the
02:06instance name on the Stage.
02:08If you test the movie now, you may see errors.
02:15So, the animation happens, but in the Output window, you'll see that there
02:19is something wrong.
02:26Everything says that a supply DisplayObject must be a child of the caller.
02:33Now, what that's referring to is the removeChild call.
02:38Once we take the can out of the display list, we can no longer remove it
02:42from the display list.
02:43So we've got to make sure this code stops running once it executes the first time.
02:49To do that, I'll go in to tell the Stage to stop listening for keyboard events.
02:53So, we'll scroll up in my code, copy line of code where I add the
03:02EventListener to the Stage -
03:03that's Command+C or Ctrl+C, and then right below removeChild(can), I'm going to
03:08paste the code I just copied that says addEventListener, and change the
03:12word 'add' to 'remove.'
03:15That will stop the Stage from listening to keyboard events, and responding to them.
03:20So when the boarder hits the can, the can will disappear,
03:23the skateboarder will fall over, and the keyboard interactivity will be removed.
03:29That should not give us any more errors.
03:31So let's test the movie, drive over to the trash can, and the skateboarder falls over.
03:37So in order to make an object eligible for garbage collection, you'll need
03:41to take it out of the display list, which you can do by using the
03:44removeChild method.
03:45In addition, you'll have to make sure that any code that references that object,
03:50like the code I have here and here, is no longer executed, by removing the
03:56appropriate event listeners.
03:58You also have to make sure there are no event listeners attached to that object.
04:02Finally, make sure that object is taken out of any arrays that it may be in.
04:07If you do all these steps, your object will be eligible for garbage collection
04:11and will be deleted when that happens.
04:13That way, your applications can be optimized.
Collapse this transcript
Using the numChildren property to loop through a container's child objects
00:00Sometimes you may want to perform an action on each child object in a
00:04display object container.
00:06Using arrays and for loops, it's a simple process.
00:10Let's take these trashcans on Stage here.
00:12Notice that they're in a movie clip called container.
00:15If I double-click container, I can enter its Timeline.
00:19Notice that none of these trashcans have an instance name, but I still want to
00:25align them to the zero position, vertically.
00:29Let's take a look at how that is possible.
00:31This is a trick that's going to work, whether or not objects have an instance name.
00:36Let's select the first keyframe of the actions layer, and open up the Actions panel.
00:40I'll start by creating a for Loop.
00:44Remember, it's for, and then parentheses, and curly braces.
00:50Inside of the for Loop, type var space i, colon uint; -
00:53remember that's a positive integer, equals zero, semicolon, space, i, less than, and space.
01:04We'll stop there for just a second.
01:06Now here, we want to run a loop for each child inside of the container object.
01:12That way it will work whether or not we add or subtract objects inside of the container.
01:18So to get the number of children inside the container, we type
01:22container.numChildren with a capital C, semicolon and the space, and then i++.
01:32The great thing about the numChildren property and using numbers in a loop
01:37is that you can actually access each child in a display object container by a number.
01:43Within each display object container, there is layering.
01:46Each time a new child is added, it's added in front of the highest child before it.
01:52So we have sort of a stack going on.
01:55It's just like the layers in Flash.
01:57So each time you add a child, it goes on top of the one before it.
02:00So, to access the children, type getChildAt.
02:06So getChild, with capital C, At with capital A, and then parentheses and then
02:13just pass in i in the parentheses.
02:15Since we're looking for children inside of the container, we're going to need to
02:19add a container before getChildAt.
02:21So container.getChildAt(i);.
02:25So this is how we reference all of the children inside the container, through
02:29their index numbers.
02:30Now after the close parenthesis, you can type any property or method you
02:34want, using dot syntax.
02:36So dot, y, space, equals, space, zero.
02:42So even though these objects don't have instance names, they still exist, and
02:46you can still reference them through ActionScript.
02:48Let's see how it works.
02:50Test the movie, and all the trashcans or aligned.
02:53You'll notice that if I move the container over, double-click it, and Option+Drag or
02:58Alt+Drag to create more trashcans, the code will not break.
03:02I'll test the movie again, and all the trashcans are aligned.
03:07So using the numChildren property inside of a for Loop, and the getChildAt
03:12method, you can reference any object, even if it doesn't have an instance name.
Collapse this transcript
Using the getChildByName method
00:00Occasionally, you'll run into situations where you cannot access a child
00:04object through dot syntax.
00:06This becomes more common as you work with ActionScript classes in object-
00:10oriented programming, which we will look at later on.
00:13In order to keep your code from breaking, you should always be prepared
00:16for those situations.
00:18So on the Stage, I have boarder_mc.
00:22Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:26Now, of course we can access this object directly by typing boarder_mc. We also
00:32look at storing the boarder in a variable.
00:35Another option is to use a method called getChildByName.
00:38So I'll type var, space, and then boarder, datatype it to a MovieClip.
00:45I'll set it equal to getChildByName, and in parentheses type some quotes, and then type boarder_mc.
00:57When you use getChildByName, you've got to make sure you're using a string, which
01:00is going to be in quotes and green.
01:02So we're referencing the object by its name.
01:07Now, if you test the movie, you might see an error because Flash is trying
01:12to convert the object that comes from getChildByName into a movie clip, and
01:18it's having trouble.
01:19So if you look in the Compiler Errors window, you'll see an error.
01:23If you read into it, it basically says Flash is trying to treat a display object
01:28as a movie clip and something is going wrong.
01:30So let's go back to our code and see what we can do.
01:33You might remember earlier when we had to typecast an object as another type of
01:39object so that we didn't have an error.
01:41That's the same thing we have to do here.
01:43Right before getChildByName, we are going to tell Flash to treat this data as a
01:48movie clip because child objects, by default, come in as display objects.
01:55So when you run this method, Flash is going to give you what it thinks is a display object.
02:00So you want to tell Flash to treat that display object as a movie clip.
02:04So I'll type MovieClip;
02:07capital M, capital C, and then wrap the getChildByName and all the parentheses
02:14inside of another set of parentheses.
02:15So you should have two close parentheses at the end.
02:20So now if I test the movie, I shouldn't receive any errors.
02:24So now if I want to manipulate the boarder, I can do that through that boarder
02:28property I just created. So boarder.x =0.
02:32I'll test the movie, and it works just fine.
02:36Most of the time when you're working in Flash you won't need to use this method,
02:41so you'll be okay just by typing boarder_mc;
02:44however, when you get into more advanced ActionScript and you start using
02:48external files, it will become a necessity to use this technique to access your objects.
02:55So using the getChildByName method, you can access child objects using an
03:00object's instance name without having to worry whether Flash created custom
03:04variables behind the scenes.
Collapse this transcript
4. Working with Timelines
Understanding timelines
00:00Timelines are somewhat infamous in Flash, and many programmers refuse to write
00:04code unless all assets can be contained within a single frame.
00:08While simultaneously working with ActionScript in Timelines can be tricky, with
00:13proper planning, you will be able to set up your Timelines and code to best work together.
00:17Let's take a look at some examples of why Timelines can be frustrating.
00:23I'll play the animation here.
00:27So we have some text on the screen, then it fades out, and a bear walks onto the Stage.
00:33This is all controlled through motion tweens.
00:36Let's say we wanted to communicate with the bear through ActionScript.
00:40So we'll close the window and go to the first keyframe of the actions layer on
00:44the main Timeline and open up the Actions panel.
00:48Rather than navigating through my whole file, I can find the bear instance
00:52name, or give the bear an instance name, through the Insert Target Path button
00:57in the Actions panel.
00:58I'll click that button, click on main_mc, select BearWalking, and then it says,
01:07"Instance name required to use this object as a target."
01:10So I'll click Rename.
01:12Now I will give it an instance name of 'bear.'
01:13I'll click OK, and now it has an instance name.
01:18If I click OK, then Flash will actually write the code to communicate with
01:24that object for me.
01:26I don't need object this there, so I'll just delete that, and I will have main_mc.bear.
01:35So let's say I want to put its x position at zero, so you can type .x = 0.
01:40Now I test the movie, and I get an error.
01:43The reason why I get an error, it's because if you look at the Timeline, you'll
01:49see there is nothing on the main layer. And if I scroll to frame 100, you will
01:56see that the main layer is what contains main_mc.
01:58So I am trying to communicate with an object that doesn't exist.
02:03In order to communicate with that object, I need to put the Timeline code on a
02:08frame where the object exists.
02:12So I'll go to frame 1, open up the Actions panel, cut the code using Command+X
02:18or Ctrl+X, move to frame 100, select that keyframe of the actions layer, open up
02:27the Actions panel and paste the code with Command+V or Ctrl+V. Now when I test
02:31the movie, after the text fades out, you'll see the bear can be affected by
02:36ActionScript, and there he is, on the left side of the screen.
02:40If you keep watching, you'll notice the bear moves over to the left and walks in again.
02:46Here is why that happens.
02:50Look at my keyframes in the Timeline.
02:52I have one keyframe on the main layer here, and then on frame 145, there is
02:59another keyframe on that same layer.
03:03So when that keyframe hits, that change that I made to the bear gets reset, and
03:09the bear then walks in.
03:12Also notice that modifying the bear with ActionScript completely destroyed the
03:17animation that I created.
03:20So playing with Timelines, again, can be very tricky.
03:24You need to be very precise with how you set up your FLA files so that you can
03:29communicate effectively.
03:31One way you can do that is to keep objects that you are going to need to
03:36communicate with in one frame.
03:40So you have one keyframe, and you manipulate the object only with ActionScript.
03:46If you have to have multiple keyframes, then maybe you can put different
03:51ActionScript for each keyframe and have the code reapply.
03:56In some instances, you are going to have to get really creative to pull it off.
04:01I don't say this to scare you away from working with Timelines, but you should
04:04know what you're dealing with.
04:06Now that you know potential problems of working with Timelines, make sure to set
04:10up all of your Timelines appropriately so you can best integrate interactivity.
Collapse this transcript
Using common timeline navigation methods
00:00There are many ways to navigate through a Timeline, but some are highly useful
00:04and incredibly common in ActionScript 3.
00:08On the Stage here, I have several elements.
00:10I have an animation contained in main_ mc, which if I double-click, you can
00:15preview that its Timeline is relatively simple. The bear stays stagnant for a
00:20few frames, and then he walks on the Stage,
00:22eats a fish, and walks off.
00:27How the animation is set up is irrelevant for this exercise.
00:30We are just concerned about controlling the Timeline of that movie clip.
00:34Then I have back_btn, playPause_mc;
00:42capital P for pause, and then forward_btn.
00:46playPause_mc contains two buttons:
00:50one called play_btn and then one called pause_btn.
00:57I have already written some ActionScript code,
01:04so let's take a look at it,
01:06importing the MouseEvents and adding EventListeners to all of the buttons on the Stage.
01:10We've already created the event handler functions, and I've also hidden the
01:17Pause button, since the movie clip is going to be stopped by default.
01:22So let's start by making the movie clip play.
01:26Inside of the playClicked function, I can type 'main_mc.play.'
01:35After play, type parenthesis and then a semicolon.
01:38So I will test the movie, and you won't see the animation taking place until I
01:45click the Play button.
01:49After a few seconds, the bear will walk onto the Stage.
01:55So we can see that that's working.
01:56When I click the Play button, I also want to hide the Play button, and
02:00reveal the Pause button.
02:01So inside of this function, I'll type playPause,
02:07capital P, underscore mc. play_btn.visible = false.
02:16This will both hide the button and disable its interactivity.
02:19Now I want the Pause button to show,
02:25so I'll copy this line, paste it on the next line, change play_btn to pause_btn,
02:31and change the false value to true.
02:35Now I'll test the movie, click the Play button, and you will see the Pause
02:40button shows, and of course, you will see the bear walking in.
02:47Now let's write the code for the Pause button.
02:49Select all the code in playClicked, copy it with Command+C or Ctrl+C, click
02:55inside of the pauseClicked function, and paste the code with Command+V or Ctrl+V.
03:02In the pasted code, change play to stop, change the first false to true, and
03:10change true to false.
03:12So we want to stop the Timeline.
03:16We want to show the Play button.
03:17We want to hide the Pause button. Test the movie one more time.
03:23Click the Play button to start the animation, and once the bear comes on, click
03:28the Pause button to watch the bear stop.
03:30So we can pause the movie, and we can click Play again to have it play;
03:35it almost got that fish.
03:42Now let's add the interactivity to skip forward and backward one frame at a time.
03:47Here what I am going to do is copy the code that shows the Play button and
03:53hides the Pause button.
03:56This is because when I click to go to the next or the previous frame, the movie
04:00is actually going to pause anyway.
04:02So I want to treat it like I click the Pause button.
04:04So copy those two lines, paste them inside of backClicked and forwardClicked,
04:13and then inside of backClicked, above the two lines that you pasted, type main_mc.prevFrame,
04:20p-r-e-v, short for previous, Frame, capital F, some parenthesis and a semicolon.
04:29So this will move back one frame, and we can just copy and paste this line of
04:34code into forwardClicked, above the rest of the code that's already in there,
04:40change prev to next, so it's nextFrame,
04:44capital F. Test the movie.
04:47You can click the Play button to start the animation. Once the bear comes on
04:56the Stage, you can press Forward button to advance in frames or the Back button
05:03to step back in frames. Then you can press the Play button to continue the animation.
05:15So you've seen that you can navigate through the Timelines of various objects,
05:20including animations, by using several built-in methods, like nextFrame,
05:25prevFrame, Play, and Stop, and you can control the visibility and interactivity
05:31of objects using the visible property.
Collapse this transcript
Using the currentFrame and totalFrames properties
00:00Have you ever wanted to track the playback of a SWF file as a percentage, like a video?
00:06You can do that using simple math and some built-in properties.
00:10On the Stage, I have my main animation, which is main_mc, and then I have
00:16playPaused_mc that controls the playing and pausing of the animation.
00:21I am going to have prog_mc.
00:24That's a progress bar.
00:25By double-clicking, you can see that its Timeline is a 100-frame animation.
00:34As a side note, you'll see that I use the classic tween instead of a motion
00:37tween; sometimes I just do that because it's faster for me, but there is no
00:41particular reason why you would need to use a classic tween here.
00:45On the first frame, I have a stop action, and that's all there is to that movie.
00:53What we are going to do is track the progress of the main animation as a
00:59percentage, and tell the prog movie clip to go to the frame that corresponds to
01:05the percent that's played in the movie.
01:07So let's go to the first keyframe of the actions layer and open up the Actions panel.
01:13Inside of playClicked, below the last line of code that's in there, I am going
01:19to add an EventListener to main_mc. Type main_mc.addEventListener, capital E for
01:28Event, capital L for Listener; it should be blue after you type it. Then in the
01:33parentheses, I am going to type the event that I want to listen for.
01:38Now this event is called ENTER_FRAME.
01:41It runs every time your movie enters a new frame.
01:45So it's executed with each frame.
01:50That's the perfect interval to track the playback progress for this animation.
01:55So the ENTER_FRAME event is Event with a capital E Event, .ENTER, in all caps, _FRAME, also in all caps.
02:05This should all be blue.
02:06So type a comma, and then after that, we'll type trackProgress.
02:15So that will be the function that it will create.
02:18Now before we go on, I want to take this line of code, copy it with Command+C or
02:23Ctrl+C, and then paste it at the bottom of the pauseClicked function.
02:29Now the reason I want to do this is because I want to remove the EventListener.
02:34The ENTER_FRAME event runs very frequently and takes up memory in the Flash player.
02:41So when you don't need to run it, like when the movie is paused, it's best to not use it all.
02:47So let's change add to remove in the pasted code, and that way we won't use
02:54memory when we don't need to.
02:57Below the pauseClicked function, let's define the trackProgress function.
03:01So type function trackProgress with a capital P, some parentheses, and inside of
03:11the parentheses we need to receive an event object, so type evt:Event with a capital E. After the
03:18close parenthesis, type colon, void,
03:21all lowercase, then type some curly braces.
03:27So I have open and close curly braces, and I am indenting here.
03:33To track the progress, we are going to need two things:
03:37the current frame that the movie clip is on and the total number of frames
03:43the movie clip has.
03:45So in the trackProgress function, we are going to create a variable called
03:51percent. And this is going to be a Number, capital N. I am going to set it equal
04:01to the percentage of the movie that has been played.
04:04To find the percentage, we are going to take the current frame that the movie is
04:09on and divide that number by the total number of frames in the movie.
04:14That will give us a number between 0 and 1 that will represent the percentage,
04:21and then we can multiply that number by 100 to get a whole number percentage.
04:29So let's see how that works.
04:32Type main_mc.currentFrame / main_mc.totalFrames.
04:47Before we move on, let's just trace the value of percent to see what we get.
04:55Test the movie and then click the Play button and just look in the Output window.
05:01We'll close the movie, and we'll scroll down, and we get these values that start
05:13off at 0.01, 0.02, 0.03, and going on and on.
05:21So what this represents is right now where I have highlighted 0.06 that is 6% of
05:29the movie that's played.
05:31So I ended at about 28% of the movie played.
05:37So if we multiply this value by 100, by moving that decimal point two places to
05:42the right, then we can get a whole number to use as a frame.
05:46So let's go back to our code, and we'll look at getting a whole number.
05:54So the first I am going to do is wrap some of this code in parentheses, and
05:58parentheses in math, in ActionScript, work just select like parentheses in math class.
06:04They make sure that one process is done before moving onto another process.
06:09So they give this priority, this division right here.
06:12So I am going to take that,
06:15I am going to multiply that value by 100 to move the decimal point, and there we
06:22should have a number between zero and 100.
06:23The problem is it's still not going to be a whole number.
06:27We need to round it off somehow.
06:29So I'll take that and wrap this whole thing in parentheses, so I'll have two open
06:34parenthesis at the beginning of this equation, and at the very beginning I am
06:38going to type math.round, and then I am going to delete that extra parentheses
06:45that Flash created for me.
06:46And what this is going to do is it's going to take this whole number and round it
06:51off. So instead of having 28.075, I am just going to have 28.
06:59So I can use this percent to correspond to a frame in the progress bar movie clip.
07:07So we'll go to the next line and type prog_mc.
07:11That's the name of the progress bar .gotoAndStop;
07:16capital A, capital S, and in parentheses pass in the word 'percent.'
07:22Now let's test the movie, see what we get.
07:26Click the Play button and watch the playhead at the bottom of the screen track
07:30the progress of the animation.
07:32So you see the bear walk in, eat the fish, and then walk out, and the whole time
07:36you can see the progress right at the bottom of the screen.
07:39Notice at the end, the progress bar goes all the way back to the beginning.
07:44So now I'll close the preview window.
07:48It looks like everything is working great.
07:51So by using the current frame and total frame properties, along with common
07:55math equations and rounding numbers, you can track the playback progress of an
08:00animation.
Collapse this transcript
Controlling the timeline of an instance
00:00Controlling the playback of an animation is an important skill to have, and it
00:04goes without saying that it's sometimes necessary to control the Timelines of
00:08movie clips from within other objects.
00:11Let's take a look at the animation I have here.
00:13I'll test the movie, and you'll see the bear walks in, starts eating food out of the basket.
00:21This is an information video about how you shouldn't feed bears and how do put
00:28your food up on a rope in a tree to keep it away from a bear.
00:33So, aside from looking a little bit sloppy, this animation could be a lot better.
00:37For example, we could change the camera angles and have the animation cropped,
00:41focusing on the most important parts.
00:47So this animation is a movie clip, and I have different keyframes in the
00:52Timeline that are scaled and move the movie clip to focus on key parts of the animation.
01:01If you don't have access to the exercise files, just create any animation inside
01:06of a movie clip and then set different keyframes upon the main Timeline with
01:09different views of that movie clip.
01:11So what we are going to do is go inside of the movie clip and communicate back
01:18to the main Timeline, and give it cues as to when it should change scenes.
01:23So, let's go to frame 1 and double-click the movie clip.
01:27So here you can see the animation.
01:29Again, if you don't have exercise files, just create any simple animation.
01:33So, when the animation starts, we want it to have the normal view,
01:41in other words the full, panned-out view.
01:43So, the bear walks in, and then the bear starts munching on some food.
01:49I've actually already created a keyframe in the actions layer here.
01:53So we'll put a command to go to the appropriate frame on the main Timeline.
02:00The appropriate frame, if we go to Scene 1, is going to be frame 20;
02:05that's this frame right here.
02:06You can see where the keyframe is.
02:08So, let's go back into the animation.
02:10So I'll go up to frame one on the main Timeline and double-click the movie clip.
02:17Then I'll scroll to frame 103, click on the first keyframe of the actions layer,
02:23and open up the Actions panel.
02:26So, we want to send a command to the parent.
02:28I want to use gotoAndStop so the playhead on the main Timeline isn't skipping
02:32around in different views on its own.
02:34I want to control all of that from within this movie clip.
02:37So gotoAndStop at frame 20.
02:42The problem is the parent property, if you remember, gives us a display object
02:49container, which doesn't have a property called gotoAndStop.
02:53So we are going to Flash to treat the parent as a movie clip.
02:57So wrap the word in a parenthesis.
03:03Before the parenthesis, type MovieClip,
03:06capital M, capital C all one word.
03:10So this code tells Flash to treat the parent as a movie clip, which it is, and to
03:18gotoAndStop at frame 20.
03:20So let's the movie and see this in action.
03:22So, when the bear gets to the basket, the angle should change. There we have it!
03:29So now all there is left to do is update each one of the different
03:34scenes appropriately.
03:35So, let's go back to the main Timeline.
03:39So we have a view of just the basket at frame 41, and then it pans out a
03:49little bit at frame 71.
03:51So, let's go to frame 1, go back inside the movie clip, and continue on,
03:57previewing the animation.
03:58So, the bear is eating out of the basket.
04:03Then it shows the basket by itself.
04:05So, this is a good place to show the close-up view of the basket.
04:09Let's select that keyframe of the actions layer, on frame 196, open up the
04:14Actions panel, and we'll type the same code.
04:18Since we know where we are going from the beginning, I'll type MovieClip to
04:21start out with, capital M, capital C, some parentheses, put parent inside of the
04:26parentheses to treat the parent as a MovieClip .gotoAndStop.
04:34Then we'll pass in the frame, that's going to be 41.
04:37So, you can test the movie again. The bear goes to the basket.
04:48There is a close shot and then the shot of just the basket by itself, and then
04:55when the basket goes up, we want to zoom out all the way.
05:01So, let's scroll forward to frame 243.
05:05That's where the animation starts of the basket going up, and in that keyframe
05:12of the actions layer, we'll do the same thing: MovieClip(parent), after the
05:22parenthesis .gotoAndStop, pass in frame 1. That will give us the original shot
05:28from the beginning.
05:29I am just going to select this line and copy it now, and we'll apply it one more time.
05:37Close the Actions panel and go forward, and the last part of the animation is the
05:42bear reaching for the basket.
05:44So select that keyframe of the actions layer, which is on the frame 329, and open
05:49up the Actions panel, paste the code that you copied - it's Ctrl+V or Command+V
05:53to paste - and then change the frame number to 71.
06:02Test the movie, and you can preview the entire animation.
06:06So once the bear gets to the basket, you have a close-up shot,
06:11then a close-up shot showing the basket by itself,
06:14then the basket being pulled up into the tree, and the bear trying to reach the
06:20basket, but he can't.
06:23So with dot syntax and some simple built-in properties and methods, it's easy to
06:28control Timelines of movie clips, even if they are nested within other objects.
06:33Using the typecasting technique by wrapping the parent code inside of a
06:37movie clip, you won't have any errors either.
Collapse this transcript
Detecting and navigating frame labels with ActionScript
00:00One recurring theme in this title, and many other titles for that matter,
00:04is staying organized.
00:06Making a project scalable is often a crucial part in building a solid app,
00:11and scalability is usually easiest to maintain when using frame labels
00:15instead of frame numbers.
00:17Let's take a look at our file here.
00:19On the main Timeline, I have this movie clip, and if I double-click, it you can see its Timeline.
00:26I have some actions telling the main Timeline to jump to a specific frame so
00:32that we can change the angle of the view of this animation.
00:37So I have this code here MovieClip.(parent).gotoAndStop(20) and if I test the
00:42movie, you can see an example of that. When the bear gets to the basket the camera
00:46sort of zooms in on the bear.
00:48Really, what's happening is the movie clip is scaling up.
00:51I set that all up in keyframes on the main Timeline.
00:55If you are not familiar with frame labels, frame labels enable you to
00:59actually name a frame.
01:01So, instead of saying go to frame 20, I could say go to the frame where the bear
01:06is eating out of the basket.
01:08That way if I move around the frame label, it doesn't matter.
01:12So let's close this and return the main Timeline.
01:15Now, I am going to go to frame 20, and I am going to create a new layer
01:22above the frame layer.
01:23I'll call this new layer 'labels.'
01:27In the labels layers, I am going to create a keyframe at frame 20.
01:32I'll do that by pressing F7 to insert a blank keyframe.
01:35When you're working with labels, you should always have them on their own layer,
01:40and use blank keyframes.
01:43So, no art on the labels layer,
01:45only blank keyframes.
01:47Select frame 20 of the labels layer, and in the Properties panel, click in the Name field.
01:55Here, you don't want any spaces or special characters, so type bear_eating and
02:02then press Enter to commit the change.
02:05You should see bear_eating in the Timeline.
02:08Now, let's go back and change our code to the label instead of the number.
02:14Go to frame 1 on the main Timeline, double-click the movie clip, move the
02:20playhead to where the ActionScript is, which is on line 103.
02:25Select that keyframe of the actions layer and open up the Actions panel.
02:31Select 20 and delete it.
02:35Now if you just type bear_eating right here, just like you typed in the frame
02:39label, you are going to get an error, because there is no variable in Flash
02:44that's called bear_eating.
02:46It's not built-in to Flash, and you don't create a variable when you create a frame label.
02:52So, you need to refer to frame labels as Strings.
02:55So you should put them in quotes.
03:00Just think of this bear_eating thing as text.
03:02So, when you get a frame label, you're assigning text to a frame.
03:06Whenever you refer to an ActionScript, you need to use a String because it's text.
03:10So let's test the movie and see what we get.
03:13So you can see that it works just the same.
03:21Let's go through the rest of our frame labels and repeat this change to get some practice.
03:27Return to the main Timeline, and above each keyframe in the main layer, create a
03:32keyframe for the labels layer.
03:40So on frame 41 in the labels layer, I am going to select that frame, go to the
03:45Name area in the Properties panel, and type basket.
03:49This is the view of just the basket.
03:52Note, there are different types of frame labels,
03:54so if you click that dropdown menu, you'll see Name, Comment, and Anchor.
03:58Names allow you to navigate with ActionScript, like we've been doing.
04:02Comments allow you to write comments, just like you would in ActionScript, except
04:05for they're in the Timeline.
04:07So you can explain what's happening in that part of the Timeline.
04:09That's makes it actually really easy to navigate around the Timeline.
04:14Then finally, anchors allow you to communicate with the back button in the browser.
04:18Now, the feature does sound really exciting, but it doesn't work with all browsers,
04:22so I usually don't use it.
04:23So we'll select the Name and then go to the last frame of the labels layer that
04:28we created, on frame 71, click in the Name field, and type bear_reaching.
04:39Press Enter or Return to accept the change.
04:41You should see three named labels there.
04:45Now, just for good measure, we can go to the first frame, click on the first
04:49keyframe of the labels layer, and then click in the label field and type normal.
04:53Now frame 1 is pretty much always going to be the frame 1, but just in case if
04:57ever you want to move it around, it's wise to give it a name anyway.
05:01So we have normal, bear_eating, basket, and bear_reaching.
05:05So, let's go to frame 1 and double- click the main_mc movie clip and select the
05:12first keyframe of the actions layer and open up the Actions panel.
05:16We are going to tell the main Timeline to go to the normal frame here.
05:20So MovieClip, in parenthesis put parent - remember, that solves any errors that may occur from using
05:29parent, because Flash looks at parent as a display object container.
05:32So you are telling Flash to treat it as a MovieClip. .gotoAndStop, capital A, capital S.
05:41Then instead of passing in 1, I'm going to pass in normal in quotes.
05:48Close out the parentheses and add a semicolon.
05:51Now, we'll close the Actions panel, and we'll make all the rest of the changes
05:55for our navigation in the actions layer of the main movie clip.
05:58We've already made the change at 103,
06:02so I'll go to 196, select that actions keyframe, open up the Actions panel,
06:09replace 41 with a String called basket.
06:12Then we'll go to the next keyframe, which is of the basket going up.
06:22That's going to take us back to the beginning.
06:23So I'll open up the Actions panel and change 1 to the string normal. And moving on,
06:33the last one is the bear_reaching.
06:37So I'll open up the Actions panel on that frame and change 71 to bear_reaching.
06:47Now you should be able to test the movie, and it should work just the same.
06:50So the bear walks in.
06:55When he starts eating the food, it zooms in on him, then zooms out, just shows us
07:00the container by itself.
07:01The container goes up into the tree on the rope, and the bear tries to reach at it.
07:07So now we've replaced our numbers with frame labels.
07:16Let's take a look at the advantage of doing that.
07:18Go to Scene 1, and let's say we were to move around our keyframes for some reason.
07:23So, I'll select frame 20 on the main layer, so that keyframe is selected, and I'll move it.
07:30So I am moving it to frame 25.
07:33Now all I have to do is move that bear_ eating label, and I don't have to change the code.
07:38So, I'll test the movie, and you'll see that I'll still go to the bear_eating at
07:43the appropriate time. There it is!
07:49So with frame labels your app can withstand animation and design changes without
07:54requiring any code modification.
Collapse this transcript
Solving problems when timelines and ActionScript animation collide
00:00Even when you plan on adding code from the foundation of your application, you
00:04may still run into problems because of working with the Timeline.
00:08Let's take an example here.
00:10Let's say I want to click on the bear in this animation and have it take me to a
00:17link to explorecalifornia.org.
00:20Now, on the Stage, I have main.mc, and inside of there, there is a bear, and
00:26it's called bear_mc.
00:30Let's return to the main Timeline, go to the first keyframe of the actions layer
00:35and open up the Actions panel.
00:37So I have a link, so when you click on bear_mc, you'll go to
00:42explorecalifornia.org.
00:44As a side note, if you're curious to learn more about navigateToURL or
00:48URLRequest, you can look these things up in Flash Help.
00:51So let's see how this code works.
00:53I'll test the movie and click the bear over and over again, but I don't get any link.
01:03The first thing to do is check to see if I typed the instance name correctly:
01:07main_mc.bear_mc, all lowercase.
01:11So on the main Timeline, I have main_mc, and then inside of there, I have bear_mc.
01:20However, the bear doesn't walk into the Stage until frame 17.
01:27I've also named this bear bear_ mc, but the code does not work.
01:35Here is part of the problem.
01:37This BearWalking instance is different from what we see on frame 1, which is an
01:43instance of a symbol called Bear.
01:47Just because of that discretion, this is not working, even though we have
01:52the same instance name.
01:53So you can start to see how Timelines can be kind of difficult to work.
02:00Here is one solution for that.
02:02I'll return to the main Timeline, click on frame 1 of the actions layer, open
02:09the Actions panel, select all my code, except for that stop() action -
02:13we still want that - and then cut it using Command or Ctrl+X. So I don't want to delete it.
02:20I want to cut it and then paste it somewhere else.
02:24Now, I'm going to go to the library, and I'll find the movie clip
02:29called BearWalking.
02:31I'll have to minimize all my folders.
02:33I can find it right here, BearWalking.
02:36So I'll double-click the movie clip to enter its Timeline.
02:39You can see its animation.
02:41I'll add a new layer at the very top, called actions.
02:47I'll select the first keyframe of the actions layer, and open up the Actions panel;
02:56paste the code.
02:58Now, of course, we don't want to go from main_mc to bear_mc here;
03:03we want to apply this code to the WalkingBear.
03:06To apply this code to the object that it's already within, I can simply delete
03:12everything, except for addEventListener, or I could also type
03:15this.addEventListener.
03:18This refers to, well, this object,
03:22in other words the BearWalking movie clip.
03:26So let's test the movie now and see what we get.
03:29So I can click the bear, and the link opens.
03:36Now, if I have another animation of the bear, the link won't open; only that
03:40BearWalking movie clip will work.
03:42So we'd have to copy and paste this code into each of the bear animations to get it to work.
03:46I am not going to do that right now, but you can use the same technique in other movie clips:
03:51just copy and paste this code into any movie clip, and then when you click on
03:55that movie clip, the link will open.
04:00Let's return to the main Timeline.
04:03So if you have problems communicating to an object from the main Timeline,
04:07remember that you can just cut and paste that code inside of the object itself,
04:13so that you can guarantee that the code will work.
Collapse this transcript
Condensing a multi-frame timeline into one frame
00:00If you feel uneasy about working with Timelines, you may want to condense your
00:04Timelines into one frame wherever it's possible.
00:08Let's take a look at condensing this main Timeline into one frame.
00:14So I have my bear animation, and then at certain key frames the view changes.
00:22So instead of controlling this with frames, let's control the whole thing
00:28using ActionScript.
00:30To do that, all I need to do is mark down the position and scale of the movie
00:36clip in each of the keyframes.
00:40So go to the first keyframe,
00:43then look in the Properties panel at the position and size of the movie clip.
00:48The X position is 398.7 and the Y position is 276.9, and then a Scale of 100.
00:56I can go through each of these frames, mark down the X position, the Y position
01:03and the Scale, and then I can apply those in ActionScript.
01:10So what I'm going to do is just delete the labels layer and then click and drag
01:20from after the last frame on the bottom layer all the way up to the second frame
01:27of the top layer and then press Shift+F5 on my keyboard.
01:31Now I just have one frame.
01:34Now what I'm going to do is make a function that I can use from within the bear
01:37movie clip to modify the movie clip appropriately for each view.
01:43So let's select the first keyframe of the actions layer and open up the Actions
01:47panel and create a function.
01:49So type 'function,' and I'm going to call this goto, and then I'm going to type View with a capital V.
01:58I'm going to type parentheses, and as a parameter, I'm going to type view,
02:01lowercase, and datatype it to a String.
02:06The return value will be void, so I'll type :void after the parenthesis and
02:11then some curly braces.
02:13So what we're going to do is basically replace that call to goto and stop
02:19inside of main_mc with a call to this function, gotoView, and we'll pass in the
02:25same name of each view, like bear_reaching, bear_eating, et cetera.
02:31So we'll use the function here to handle that.
02:33So I'm going to type a switch statement.
02:35Remember, this is a conditional statement that we looked at earlier, switch,
02:40some parenthesis, and some curly braces.
02:45In the switch statement, I'll type view.
02:47So we're going to compare everything against a view.
02:50So the first case is going to be "normal", and I'll type a colon after that and
03:02go to the next line.
03:04Now we'll set the position and scale of main_mc.
03:11So I'll type main_mc.x = 399,
03:20then I'll type main_mc.y = 276,
03:29and then I'll type scale.
03:32The scale is done through the scale X and scale Y properties.
03:36Since those properties are going to be the same, or since the scale of the
03:42horizontal and vertical axes are the same for the movie clip every time, I'm
03:48going to write a shorthand way.
03:50I'll type main_mc.scaleX = main_mc.scaleY = 1.
04:06ScaleX and scaleY are values between 0 and 1, 1 being 100% scale.
04:12So you can do 300% scale with a value of 3.
04:16So I'm typing 1 right here, and then I'll type break on the next line.
04:21Now let's create the other three cases, and then we'll apply the code inside of
04:26the main_mc movie clip.
04:29So select these five lines of code, copy them with Command+C or Ctrl+C, go below
04:35break, and then paste the code with Command+V or Ctrl+V. I'm going to repeat
04:41this, so I have four total blocks of code, so two, three, and then four.
04:48So we'll go to the second one and change the string to bear_eating, just like
04:56the frame label was before.
04:59Here the x position is going to be 141, the y position is going to be 237, and
05:08the scale is going to be 1.89.
05:16For the next label, I'll change the case to basket.
05:24Change the x to 352, change the y to -17 -
05:31so just a minus sign and then 17, -and the scale to 3.91.
05:40Finally, I'll do the bear reaching, so bear_reaching.
05:48The x will be 161, y will be 509, and the scale will be 1.98.
06:07Now we'll have to update the change inside of main_mc.
06:11Now the shortcut to doing this would be to open up the toolbox.
06:14So I'll click that arrow on the left side of Actions panel to open the toolbox.
06:20Then I'll click and drag to expand the bottom section of the toolbox.
06:24I'll also expand its width a little bit.
06:28So I can see I have actions at all of these frames inside of main_mc, so you see
06:35that Main movie clip here?
06:36This is all the actions in there.
06:38So I can click at frame 1 and then change where it says gotoAndStop to gotoView.
06:46Now I can quickly navigate to frame 103 here.
06:50Click that and then type gotoView.
07:01So gotoView, and for 243, gotoView, 329, gotoView, and 410, which is set stop, so I
07:17don't need to make any changes.
07:19Now if you test the movie at this point, you may notice a problem.
07:25Let's see if we have any errors.
07:28So the bear walks in. Everything looks normal so far. The bear starts eating.
07:32That looks normal. The basket view is normal. The basket goes up. Then the bear reaches.
07:44Everything looks good.
07:46Now once in a while, when you use this parent code with MovieClip, you might
07:51run into a problem.
07:53Now if that ever happens - and it's very rare that it will happen - just type
07:58Object instead of MovieClip.
08:00Object allows you to write any properties and methods you want; then you
08:04shouldn't have any errors at all.
08:06I'll go back and change it to MovieClip, since we didn't have any problems with it.
08:10So if you ever feel the need to condense all of your frames into a one frame,
08:18you can go through this process, change your methods a little bit, write your
08:23own custom function on the main Timeline and navigate through the "frame
08:28labels" and navigate through the different views in this way.
Collapse this transcript
Creating a simple slide presentation app in the timeline
00:00To get practice navigating through Timelines, we'll build a simple
00:03slide presentation app.
00:05I've taken this bear animation, and I've broken it up into different frames.
00:11So we have this one, and this one, and this one, and then the basket goes up
00:16into the tree, and the bear tries to get it.
00:17So, I'll just scroll up a little bit.
00:21So you can see the bear trying to get the basket.
00:24These are all just still images that I exported using the File > Export >
00:30Export Image command.
00:31If you want to do the same thing with your animation, it's really best to do
00:36this on the main Timeline and not inside of a movie clip, because the
00:40coordinates inside of the movie clip are relative.
00:42So just keep that in mind.
00:46So on the Stage, I have a button called back_btn and a button called forward_btn.
00:53And then in my code, I've just created some simple EventListeners and
00:58EventHandlers for them.
00:59So I don't have any code inside of them, but I've just built this already.
01:03So you've seen this function before, the previous frame function, which is
01:07prevFrame and parentheses.
01:11So we can do that when we click the Back button, and when we click the Forward
01:15button, we can go to the next frame.
01:17So we'll type nextFrame with capital F, add some parentheses and
01:21Before I test the movie I want to add one more line of code.
01:23I'm just going to put a stop action.
01:25I'll put that at the top of the code, below line 2.
01:29So I'll go to the next line and just type stop, some parentheses, and a
01:32semicolon, because we don't want the movie to plat by default.
01:35So let's test the movie, and then we have our little slide show, so we can click
01:41the Next button to advance through the slides and the Previous button to step
01:46backwards through the slides.
01:51So if you ever want to make a slideshow, just whip up this code real fast,
01:56and there you have it.
01:57Let's say you wanted to advance in a loop, so you go to the last frame and
02:02you click the Next button, and you want to go back to the first frame again, and vice versa.
02:08You want to go from the first frame to the last frame if you click the Back button.
02:13Let's take a look at how to do that.
02:15So go to forwardClicked and right below nextFrame, type if, some curly braces.
02:25Now, what we can do is check to see if the current frame is the last frame.
02:33So to do that, we could say, if currentFrame is equal to - what's the last frame? 6 -
02:42we could put 6 right there,
02:44but the better way to do it is to use the totalFrames property.
02:49So if the currentFrame is the last frame, or in other words, if the currentFrame
02:53is equal to totalFrames, you see why that's better?
02:56That's because, if you add more frames or take away frames, the code won't break.
03:02So it's always better to make it so that the code won't break if you change the
03:06design, because it is pretty likely that you will change the design in your app.
03:10So the more solid the code is, the better.
03:14So if the currentFrame is equal to totalFrames, then we'll go back to frame 1.
03:19Take a second to think of the code that you would use to go back to the first frame.
03:26Simply type gotoAnd with a capital A, Stop, capital S, and 1 in parenthesis.
03:32Now, I've stressed the importance of using frame labels before.
03:37So you could use one if you want here, but actually, if I go to the last frame
03:42and click the Next button, I'm always going to want to go to the first frame.
03:46So unless I completely redesign this app, then this code is going to be solid, no
03:50matter how many frames I have.
03:52So using the number 1 is okay in this case.
03:56So I have my code set up here, and we'll test the movie and then click the
04:01Forward button and watch as we advance through the frames.
04:05See if you notice anything off.
04:08When the basket is up, I click the Next button, and it goes back to the beginning.
04:12But what about the frame where the bear is trying to reach for the basket? We don't see that.
04:18Let's take a look at why that is happening.
04:24So the function goes to the nextFrame, and then checks to see if the
04:29currentFrame is equal to the total number of frames.
04:32If so, gotoAndStop. Here is the problem.
04:37The fact that nextFrame is before this if statement makes the frame updated
04:44before you see it updated.
04:47So it happens without you seeing it.
04:50Flash makes the frame go to the last frame with the bear, but before you see it,
04:54Flash runs this code to go back to the first frame.
04:58Occasionally, you'll run into these problems when you're working with frames.
05:02You have to make sure that you change the frame last, so that the last thing you
05:09see is the new frame.
05:11To make sure this happens correctly, cut nextFrame with Command+X or Ctrl+X.
05:17Then below the if statement, let's write an else statement.
05:22So write else, and then some curly braces, and then paste the code.
05:30So if the playhead is on the last frame, we'll go back to the beginning.
05:34If not, then we'll go to the nextFrame.
05:38So we don't have the nextFrame code running every single time, like we did before.
05:42It only runs when we're not on the last frame.
05:45So let's test the movie now, and you should see all the frames of animation.
05:49So bear comes up, basket goes up, and there is the bear. Click the Next button,
05:54and you go back to the beginning.
05:55So now what we'll do is we'll apply the same technique, but in reverse, to the
06:04backClicked function.
06:05So let's scroll up.
06:09Then above prevFrame, let's write an if statement:
06:12so if, some parentheses, and some curly braces.
06:17In the if statement, we want to see if we're on the first frame.
06:21So all we have to do is check to see if currentFrame - capital F - is equal to 1.
06:27If so, then we're going to go to the last frame.
06:31So gotoAndStop and then parentheses, pass in that totalFrames property - capital F.
06:39It should be blue after you type it.
06:42Then we'll wrap prevFrame in an else statement.
06:45So we only go to the previous frame if we're not on the first frame.
06:50I'll just tab that in and write some curly braces around it.
06:56So if the current frame is 1, we're going to go back to the end, which is
07:00represented by totalFrames.
07:01Again, we're using that just in case we increase or decrease the length of the
07:05animation. And then the else statement says if we're not on frame 1, then we're
07:10going to go back to the previous frame.
07:12So let's test the movie, and if you click the Back button, you'll go to the last frame.
07:18Continue to click it, and you'll step backwards through the animation, and the
07:22Forward button still works as well.
07:27So by using these conditional statements, along with the next frame and previous
07:32frame commands, we can navigate through each frame of the Timeline and have the
07:37slideshow play in a loop.
07:38So navigating through Timelines is a key component in many apps, and as you get
07:44more practice using these commands, you'll be able to add Timeline navigation to
07:48all sorts of applications.
Collapse this transcript
Using one event handler with multiple buttons
00:00One excellent way to optimize your code and prepare it for changes in the future
00:06is to use one event handler with multiple objects.
00:11Here I have a Timeline organized into three frame labels: board1, board2,
00:18board3, and I have some simple animations playing.
00:24What I did here is I took each of the buttons, notice they are all on the
00:28buttons layer which only has one keyframe.
00:33I intentionally did that to make sure I wouldn't have any problems with my code.
00:38So they are on one keyframe that lasts for the whole duration of the Timeline.
00:44So I named the buttons the same as my frame labels.
00:48So I have that first one that's called board1, the second one that's called board2,
00:52and the third one is called board3.
00:54Let's go to the first keyframe of the actions layer and open up the Actions panel.
01:01Here I've connected board1 to the buttonClicked event handler.
01:06I've done the same thing for board2 and board3, so they're all pointing to the
01:11buttonClicked event handler.
01:15Now what I'll do when a button is clicked it send the Timeline to the frame
01:19that corresponds to the clicked button.
01:23Let's take a look at how to do that.
01:25I know, for sure, I'm going to want to use gotoAndPlay, and some parentheses and a semicolon.
01:36Now, let's say, I want to access information about the button that was clicked
01:42that triggered this event handler.
01:43So let's say I clicked the board1 button.
01:46How do I find out which button I clicked?
01:50To do that, I can go to event - that's the name of the event - .currentTarget with
02:06a capital T, and that's going to create a reference to whatever button that I clicked on.
02:11So if I clicked on board1, it's going to give me a reference to board1.
02:14If I happen to click on board2, it's going to give me a reference to board2, et cetera.
02:20Then I want to get the object's instance name, because if I just have
02:24event.currentTarget, the name would be board1, board2, or board3, but that
02:30gives me a reference to the object and not its instance name.
02:34So I need to just have its instance name as a string, so that it will work as a frame label.
02:39To do that, I type .name.
02:43Now it gives me the name property of the object that was clicked.
02:47So it gives me board1 as a string, board2 as a string or board3 as a string.
02:52I can test the movie,
02:53I can click the buttons, and navigate around to the different frames.
03:04So by using the currentTarget property, I'm able to access any information I
03:08need from the object that triggered the event handler.
Collapse this transcript
5. Working with Classes and Object-Oriented Projects
Creating a class using Flash templates
00:00Creating an ActionScript class can be a daunting task.
00:04There are many required phrases, and the structure of your files has to be
00:08perfect or the code will break completely.
00:12To make this process easier, Flash has pre-built templates for creating
00:16ActionScript class files.
00:19You can see these templates through the welcome screen, or you can go to File > New.
00:26I mentioned that this file is a template, but you don't find it under the Templates tab.
00:30You'll have to go to the General tab in the New Document window to find
00:34ActionScript 3.0 Class.
00:36That's the first file.
00:37That's an ActionScript file.
00:39If you look at the description, you'll see that it says, Create a new AS file
00:43with a .as extension to define an ActionScript 3.0 class. So I'll click OK.
00:51Here, because of my ActionScript settings, Flash is asking me which app I'm
00:55going to use to write ActionScript code.
00:59I can either use Flash Professional or Flash Builder.
01:03For this course, we're only going to be working in a Flash Professional.
01:08If you get deep into object- oriented programming and you want a more
01:13robust application for writing ActionScript classes, then I recommend
01:16getting into Flash Builder.
01:19But for now, Flash Professional is fine, and I'll click Don't Show Again, and
01:23type the Class name.
01:24I'll just call this Example, capital E, and then I'll click OK.
01:33So, Flash has created an ActionScript file for me.
01:37It's important to note then the ActionScript file is really just a text file.
01:42You can actually use the text editor Notepad to create an ActionScript file.
01:48As long as it's plain text - so, not rich text - and as long as it has the .AS
01:54extension, it can work in Flash.
01:57So let's walk through this code.
02:00The first thing inside of here is the package declaration.
02:04So it says the word 'package' and then a space.
02:07The package identifies where this code resides.
02:13If you want to be really organized, you'll group your different ActionScript
02:17class files into folders. And packages allow you to tell Flash how that code is organized.
02:24This is something that's more of a deep concept in ActionScript classes, and
02:28we'll talk about it later on in this course.
02:32The next line of code says public class Example.
02:38When you create an ActionScript class, use the word 'public' and then a space and then class,
02:46just like as if you're defining a function.
02:49You specify the keyword class to create a class.
02:53These words like 'public' and maybe you'll see 'private' sometimes, these words are
02:58special words that you only use inside of a class file when defining functions
03:03and classes as well.
03:05Then we have the name of the class which is Example.
03:07Flash added that in for us when we typed Example for the name of our class, and
03:12then it says public function Example and constructor code.
03:17Let's take a look at the organization of the curly braces.
03:22Notice that the open curly braces start on the same line as the
03:25package declaration, the public classic declaration, and the public
03:29function declaration.
03:30If I click the Auto Format button, then Flash will organize this code how
03:35I've been writing it throughout this course, which is with open curly braces on separate lines.
03:41So we can see the outer curly braces for package wrap all the code in this file.
03:47Then the class code is the first level in.
03:52So that wraps the rest of the code in the file.
03:55And finally we have the public function Example.
03:59That's the most inner set of curly braces.
04:01Each time you create a class, you should have this function called a
04:06constructor function.
04:07A constructor function has the same name as the class name, which in this case is Example.
04:16This should also be the same name of your ActionScript file.
04:20So the file should be called Example with a capital E .AS.
04:26As we go forward, we'll get more in practice working with ActionScript classes.
04:30The main thing I want you to take away from this is that using the ActionScript
04:34class file templates in Flash CS5, you can easily create class files without
04:40having to worry about getting everything right.
Collapse this transcript
Setting a document class
00:00If you're interested in getting all of your code out of the Timeline and into
00:04organized class files, then document classes will allow you to do that.
00:09With document classes, you can create external ActionScript classes that
00:12completely power your FLA files.
00:15So you can take all or most of your code out of the Timeline and put it into
00:19ActionScript classes with only a few steps.
00:23The first thing you're going to need to do to create a document class that
00:27powers your FLA file is to make that class extend the MovieClip class.
00:33That basically says you're going to take the movie clip concept and add to it,
00:39with whatever's in this file.
00:42That may sound a little bit strange and new, but you're already familiar with
00:46that idea because every time you create your own custom movie clip, you're
00:51essentially extending the MovieClip class.
00:53You're taking the idea of a movie clip, and you're putting your own art and
00:56your own animations inside of it.
00:57This is the same thing, but it's with code.
01:00So, right after DocumentExample, type a space and then type the word extends.
01:05Then type another space and type MovieClip, capital M, capital C. When you type
01:10that line of code, Flash will automatically add the import statement for you.
01:16Import statements will make sure that you can use the code that you're
01:21referencing in your files.
01:22So you absolutely need to have this line of code here.
01:26If Flash doesn't insert it for you, or you forget to insert it, then you'll get an error.
01:30So just keep that in mind.
01:33Also, make sure not to add a semicolon after this line of code.
01:38See this class declaration is followed by curly braces, here and here.
01:45If you put a semicolon, then you'll make an error because these curly braces are
01:51a part of this public class statement.
01:56The next thing to do is to write some code in the constructor area.
02:00The constructor area is the initialization code.
02:04That's going to be the function that automatically runs when your FLA is initialized.
02:10So in there, let's just put a simple trace statement to make sure that this
02:13is working properly.
02:14So type 'trace,' some parentheses and a semicolon and in quotes, type "it's working!"
02:23with an exclamation point.
02:25Before you go over to the FLA file, save this file.
02:32When you're working in an FLA, you can change the ActionScript and test the
02:36movie, and those ActionScript changes will be reflected in the published movie.
02:41With a class file, however, the file gets loaded into Flash when you publish the movie.
02:48So, it loads the last saved version of that file. So if you ever make a change,
02:53you need to save it for that change to be reflected in the SWF file. So save the file,
03:00then go back to the FLA file, and in the Class section in the Properties panel -
03:07and by the way you won't see this unless you have nothing selected, so make sure
03:09you deselect everything -
03:10click in the Class section and type the name of the DocumentExample class.
03:16If you type it differently than the name of the file, it won't work - and by
03:20differently, I mean different casing, like a lowercase e instead of an uppercase
03:24E, and any other spaces or special characters, aren't going to work.
03:29So type it exactly as the name of the file is.
03:31So DocumentExample, capital D, capital E, and then press Enter or Return
03:39to commit the change.
03:41Then test the movie, and you should see the code pop up.
03:45It says it's working in the Output window.
03:48Now, you'll notice that I can still click the buttons and navigate around.
03:53Let's say you wanted to put that code inside of a document class.
03:57Let's look at how it may be a little bit different than the code is on the main Timeline.
04:01Go to the first keyframe of the actions layer and open up the Actions panel.
04:08In here, select all of the code and then press Command+X or Ctrl+X to cut the code.
04:15Close the Actions panel and move over to DocumentExample.as.
04:21Highlight the trace statement and paste the copied code over.
04:27This is going to look a little bit weird, so click the Auto format button to
04:31rearrange the code a little bit.
04:32Now we're just going to cut and paste some of these lines of code to put them
04:39in the right places.
04:41Import statement needs to be with the other import statement, and the function
04:47should be outside of the function that it's within right now.
04:51So let's select that function real quick, Command+X or Ctrl+X to cut, and
04:56then you're going to want to paste it right below the constructor method,
05:00which is public function.
05:01Keep in mind that a method is just a function, but it becomes a method when it's
05:05inside of a class file.
05:08So, scroll down and find that closed curly brace for the constructor, go a few
05:14lines below it, and paste that function.
05:17Again, I'm going to click Auto format.
05:23Now I'll scroll up, select that import statement,
05:27the one that imports the MouseEvent,
05:29and then press Command+X or Ctrl+X to cut the code, scroll up, place the code
05:36after the import MovieClip statement,
05:38Command+V or Ctrl+V. So, in the constructor, all I have are the
05:43addEventListener lines of code.
05:46I have the function organized in a different spot outside the constructor
05:51method, and then I have that import statement.
05:55So let's save the file and then test the movie.
06:00Remember that you can test the movie directly from an ActionScript class file
06:04as long as the targeted file is correct.
06:06So see the Target is Document_Class.fla.
06:09So, as long as that's set, I can test it from here.
06:13So the same keyboard shortcut: Command+Return or Ctrl+Enter.
06:16Looks like everything's working and all the code is working properly.
06:23Now I've taken it off of the main Timeline and placed it inside an ActionScript
06:28class, so it's clean and organized.
06:31Note that I haven't taken off the stop actions, because these need to happen
06:35after a certain point in time.
06:37So for me, it's ideal that they stay right here.
06:40By using a document class, you can keep all your code object-oriented or in
06:46classes, and the document class can be set up through the Properties panel.
Collapse this transcript
Preparing a class to be connected to a symbol
00:00Just like a document class, classes can be connected directly to movie clips,
00:04so that code can be taken off the Timeline and organized through ActionScript classes.
00:10This organization through classes is known as object-oriented programming.
00:15It is especially useful when you have many different objects and you want to add
00:19the same functionality,
00:21like if you wanted to add keyboard control to multiple characters in a game or
00:25use that same functionality in a different game.
00:29Let's look at how to prepare a class to be connected to a symbol.
00:34I have the file Boarder.as that's a basic class template created by Flash, and
00:40then I have some code in the Timeline.
00:42This code controls the skateboarder on Stage with the keyboard.
00:46So test the movie and use the left and right arrows to move back and forth.
00:54Let's go over to Boarder.as.
00:56The first thing you need to do is either extend the MovieClip class or the Sprite class.
01:01Remember that a sprite is a single-frame movie clip.
01:05Since the skateboarder has multiple frames, it's better to extend the
01:08MovieClip class in this case.
01:12So, after public class Boarder, type a space and then type extends and then
01:17another space and then MovieClip.
01:20Note that Flash imports that line of code.
01:23That's important.
01:24Since I already know we're going to be using the keyboard, I'm going to go to the
01:31next line and type import flash.ui.Keyboard;.
01:35It's important to note that these names, like flash.display and flash.ui, are
01:45actually package names.
01:46Again, we'll get more into package names later on, but for right now, make a
01:51mental note that these are actually folder structures.
01:54Somewhere on your hard drive, there is a folder called flash, and in there,
01:57there's a folder called display, and in there, there's a file called MovieClip.as.
02:02Also, in the flash folder is the ui folder, which contains a file called keyboard.
02:08If you want to import all of the files inside of a folder, or in other words
02:12all of the classes within a package, you can use the asterisk or wildcard
02:17symbol, just like this.
02:18I'm going to undo that change though for now.
02:22Let's jump back to the Preparing.fla file and go to our ActionScript code.
02:27The first thing I'll do is select the import statement and cut it using
02:31Command+X or Ctrl+X. I'll move over to Boarder.as and paste the code under the
02:37Keyboard import line of code.
02:40Note that the Keyboard is different from KeyboardEvent.
02:45Jump back to the FLA and into the code.
02:48Select the stage.addEventListener line of code.
02:51Cut that code, go back to Boarder.as and add that code to the constructor.
02:59Back to the FLA, to the Actions panel, select the keyPressed function.
03:06Cut it and delete the extra lines of code and go back to Boarder.as.
03:13Below the constructor method - that's the closed curly brace right below
03:16stage.addEventListener - paste the code that you just cut. It may be indented
03:24a little bit weird,
03:25So just click the Auto format button, and Flash will fix that for you.
03:30Now, I'm going to make a simple change to the code to make sure that this code
03:33will work for any object we apply this class to.
03:38Boarder_mc is going to be irrelevant.
03:43We don't want to move boarder_mc inside of the skateboarding movie clip;
03:47we want to move the whole movie clip itself - in other words the
03:51whole skateboarder.
03:53To do that, I can replace boarder_mc with this.
03:58That will move the whole movie clip.
04:01A shorter way to write that is to just eliminate this and the dot and just use x += 5 and x -= 5.
04:11So let's Save the file, and later on, we'll connect this to a class.
04:19The main thing I want you to take away is that in order to prepare a file to be
04:24connected to a symbol, you need to extend the sprite or MovieClip Class.
Collapse this transcript
Using the Symbol Properties menu to connect a symbol to a class
00:00Connecting a symbol to a class is a simple process, but some parts of that
00:04process can be confusing.
00:06Before we look at how to do that, I want to point out that I have the file
00:09Boarder.as that's in the same folder as my FLA file.
00:15Note that it's the same name as the movie clip I want to connect that to.
00:20That's not necessary, but it's very helpful, as you'll see in just a second.
00:26Let's go to the FLA file, right-click the Boarder movie clip and choose Properties.
00:33In the Symbol Properties window, make sure the Advanced area is expanded.
00:38In the Linkage area, check the Export for ActionScript box.
00:43You may be familiar with this process already, because we could have used it
00:46earlier in this course.
00:49In the Class field, you can specify the class that you want to connect to this movie clip.
00:53So the class name is Boarder.
00:57If the class doesn't exist already, Flash will create a class for you, which is
01:03why you may see a warning when you export a movie clip for ActionScript and
01:07haven't created a custom class.
01:09The warning simply says Flash can't find the class, so it's creating one for you.
01:17When you set the class like this, you can only use one custom class per library,
01:28so I can't do that with any other class.
01:30If I wanted to take the Boarder functionality and apply it to another object,
01:34I could set it as the Base Class, and then put whatever class I want as the Main Class.
01:40What Flash would do is it would take the functionality from the Base Class and
01:44apply it to that created class.
01:46We'll see how that works in just a minute.
01:48So right now I have Boarder for my Class, and then I'll click OK.
01:54You shouldn't see any kind of warning message when you click OK.
01:57If you do, double-check the location of your Boarder.as file, the name of the
02:02file, the name of the class, and the name of the constructor.
02:08Once everything works, you should see Boarder under the Linkage area.
02:12Test the movie at this point, and your Boarder has keyboard control.
02:17Let's say we wanted to apply a keyboard control to the little cat over there.
02:22Simple! We've already written the keyboard control code in the Boarder class,
02:29so if we connect the Boarder class to the Cat, everything should work, right?
02:33Let's see. Right-click Cat in the library and choose Properties.
02:39Check Export for ActionScript, change Class to Boarder, and click OK.
02:47Flash then gives you an error message that says "Please enter a unique class name
02:52that is not associated with other library symbols."
02:55If you think about it for a second, this message actually makes sense.
02:58Remember, early in the course, we exported a symbol for ActionScript, and we
03:02created new instances of that symbol by calling its class name.
03:07We can't have multiple symbols with the same exported class name, or else which
03:12one is Flash going to use, when you write the code to create one?
03:16So it makes sense that each of those should be unique.
03:19So for this situation, I'm going to cut the word Boarder, and paste it in Base Class.
03:26Then for Class, I'm going to type "Cat."
03:28When I do this, I should get a warning message. I'll click OK.
03:33The warning message says A definition for this class, which is Cat, could not be
03:39found in the class path.
03:41So one will be automatically generated in the SWF file upon export.
03:45So Flash is going to make a Cat class for me that I'm never going to see, and
03:50it's going to extend the Boarder class.
03:52So it's going to have all the functionality of the Boarder class, but I don't
03:56have to create a new class just to say that.
03:59So I'll click OK, and now you can see the Cat is exported as Cat, and when I
04:06test the movie, the Boarder and the Cat both move together.
04:10It's like the Boarder is chasing the Cat right now.
04:19So using this technique, I can apply keyboard functionality to any object I
04:24want, simply by modifying its Base Class.
04:28The ability to reuse code by connecting a class to a MovieClip symbol is a very
04:34powerful thing in Flash.
04:36The more you use it, the more you'll understand how much easier it is than
04:41writing code time after time in the Timeline.
04:45And again, all you have to do to connect a symbol to a class is put your custom
04:49class name in the Class or Base Class field in the Symbol Properties window.
Collapse this transcript
Resolving problems with instances in a linked class
00:00Occasionally, you'll run into problems when linking a class to a symbol.
00:04Knowing how to resolve these issues will prevent countless headaches in the future.
00:09I've mentioned before that whenever you work with instances and Timelines and
00:15object-oriented code, you could have problems.
00:19In this file here, I've connected the TrashCan symbol to the Obstacle Base Class.
00:28Let's take a look at the Obstacle Base Class.
00:33In Obstacle.as, the constructor method adds an EventListener to listen for
00:38the ENTER_FRAME Event.
00:42Remember, the ENTER_FRAME Event executes every frame, so over and over and over
00:49again, 24 times a second or 30 times a second, depending on how fast your frame
00:54rate is for your FLA file.
00:57So every frame, I'm running this function called checkCollision.
01:02This code may be familiar to you because you used it before.
01:06I simply copied and pasted this code from another exercise and made very
01:10small changes to it.
01:12The only change is that I added the removeEventListener line of code.
01:16So the if statement checks to see if the boarder is hitting the can.
01:22If so, the boarder goes to the second frame and plays, and removes the can from
01:27the stage, the stage EventListener is removed for the KeyboardEvent, and again, the
01:34ENTER_FRAME Listener is removed.
01:38Now, if I were to test the file at this point, I would find a whole slew of problems.
01:44Look at all of these problems.
01:48First of all, there's an undefined property called boarder_mc, but boarder_mc is
01:55on the Timeline, as we know.
01:57Why is there a problem here?
01:58Well, first of all, boarder_mc is not inside of the TrashCan's Timeline;
02:04it's on the main Timeline.
02:06That's why this doesn't work.
02:09So what we can do is reference this object from the TrashCan's parent.
02:15The trashcan and the boarder have the same parent, so this should work.
02:19So we'll type MovieClip, then parentheses, parent inside the parentheses, after
02:27the parentheses, a dot.
02:28So, I'm going to check that hitTestObject in there, and then can does not exist either.
02:35Remember that the obstacle is the can, so we need to change can to this.
02:42You'll find can in the removeChild line of code as well, and it says this.
02:50Also, the removeChild line of code is going to break as well.
02:54It's going to break because we're calling it from inside of the trashcan.
03:00It's calling it to itself, and that's not going to work.
03:04You need to have the parent object remove the child.
03:10So we can do this: parent.removeChild(this).
03:15Now, we don't have to wrap this code inside of a MovieClip in parentheses
03:19because this is a display object container already.
03:21So that has the removeChild method.
03:23So, you don't need to wrap that in MovieClip if you don't want to.
03:28But we will need to do that for boarder_mc.gotoAndPlay.
03:31So we'll type that again, MovieClip, capital M, capital C, parentheses,
03:37parent inside the parentheses, dot after the parentheses, and then we can have boarder_mc.
03:44Now let's see what we get. Test the movie.
03:46We have just as many errors.
03:51Now, before we test the movie, I'm going to save the file.
03:54Now I'll test the file again, and I only have one error.
04:03Flash says undefined property keyPressed.
04:11So Flash is telling me there's a problem with removing this EventListener on the
04:16main Timeline, because it doesn't recognize this keyPressed function.
04:21It's not defined here;
04:22it's defined in the main Timeline.
04:25In this case, it may be better to simply run a function on the main
04:29Timeline from here.
04:31So let's just cut this code, and then type MovieClip, capital M, capital C, in
04:40parentheses, parent, after the parentheses a dot, and then we'll run a block of
04:47code called disable.
04:48Now we haven't created that yet.
04:49We'll create it right now.
04:51So disable, some parentheses and a semicolon, save the file, go back to
04:56Problems.fla, click the first keyframe of the actions layer and open up the Actions panel.
05:02In here, we'll create a function called disable.
05:05So function, space, disable, some parentheses, colon, void, some curly braces, and inside
05:13of the curly braces, paste the code that you just cut.
05:18Now test the movie.
05:19We don't have any errors.
05:21We can control the skateboarder with the keyboard.
05:25When he hits the trashcan, he falls over.
05:32Then we get this, "Cannot access a property or method of a null object reference."
05:39That's inside of the checkCollision function.
05:42So let's go there and take a look at what's wrong.
05:45See if you can find out the problem.
05:48To find it out, walk through the code, line by line, and see if you can find
05:54anything that could possibly be undefined in this code.
05:57So we have this code.
05:59It looks all right, checking against the hitTestObject which is going to be the
06:03can, we have boarder gotoAndPlay.
06:07That's not going to be null.
06:09So there won't be a problem there.
06:10Then we have parent.removeChild(this).
06:15So after the child is removed, then this code may create a problem.
06:20So we'll just select that line of code that says parent.removeChild(this),
06:25cut the code, and then paste that at the bottom of this function as the very last thing.
06:30So, we don't take off the child until after everything else is taken care of.
06:35So let's test the movie again after we save the file.
06:40Run into the trashcan, and we don't get any errors.
06:45So, whenever you're going to remove an object from the stage, make sure it's the
06:51last thing that you have that object do.
06:53Now, as you can see, things get a lot more complicated when you start working
06:58with class files, but it's much more rewarding. Because I've already created
07:04this Obstacle code,
07:06if I go back to the main Timeline, and let's say I want to make a different type
07:10of Obstacle, like this cat, I can put the cat in the way of the skateboarder,
07:15I can right-click the cat, choose Properties, check Export for ActionScript, set
07:21the Base Class to Obstacle and then click OK.
07:26Flash is telling me that the Cat class doesn't exist, and it's going to create it for me.
07:29I'll click OK, test the movie, make the skateboarder run into the cat, and it's
07:37an obstacle, just like the trashcan was.
07:40So I'll close the window.
07:42So by using these object-oriented techniques, it'll take a little bit more
07:47time upfront, but in the long run, you'll save large amounts of time in
07:51building your applications.
Collapse this transcript
Understanding packages
00:00The more you get into object-oriented programming by creating ActionScript
00:04classes, the more you're going to want to stay organized.
00:08The best way to do that is by using packages.
00:12Packages are simply folder structures for your class files.
00:15Let's take a look at Finder at my folder structure.
00:20In the same directory as my FLA file, I have a folder called com.
00:24Inside of that folder, there is a folder called lynda.
00:27Clearly this corresponds to lynda.com.
00:32The reason why I'm using a reverse domain name structure is because this is the
00:36easiest way to stay organized.
00:39At the root level, I could have a few folders, like Com, Net, Org, Me, et cetera.
00:46In each of those folders, I can have organized all the domains that I'm working on.
00:54So if I'm working on lynda.com I have a com folder, then a lynda folder
00:57inside that folder.
00:58If I'm working on Explore california.org, then I can have Explore California in my Org folder.
01:05So obviously, having the end part of the domain name at the root level makes
01:11more sense than having the middle part of the domain name at the root level,
01:14because you could be working on a .com version of a site and a .me version of
01:20the site, and it could be two different things.
01:21So the system always works because all domain names must be unique.
01:28So inside of the domain name folder, I have my class files that are related to that domain.
01:32Of course, if you wanted to, you could organize it further by having a folder
01:37called Games, for example.
01:39But for now, this will do.
01:41So in there I have my Boarder.as file.
01:44When you have your files organized into folders like this, you need to update
01:47the code in ActionScript.
01:49So let's go back to Flash, and click on Boarder.as.
01:54The code is already updated here.
01:55So it says package, and then there's a space, and then com.lynda.
02:02So I'm using a dot structure,
02:04just like flash.display.MovieClip refers to a Movie Clip. I am using com.lynda
02:10to refer to this Boarder file.
02:11That's an important step when you're working with a package.
02:16So let's go back to packages.fla. Then I'm going to connect the Boarder
02:20movie clip to that class.
02:23So right-click Boarder in the library and choose Properties.
02:27In the Symbol Properties window, check Export for ActionScript.
02:30In class, change Boarder to com.lynda.Boarder.
02:35This is an important thing to know because it's a common mistake for people that
02:39are new to this technique.
02:41You have to type out the full package name here.
02:43So type that out, click OK. You shouldn't get any error or warning messages.
02:50I should test the movie, and the boarder should move left and right.
02:59So you can stay organized by putting your files in folders using a reverse
03:04domain structure, changing your classes to support the packages, and when you
03:11connect those classes to symbols, make sure to use the full package name.
Collapse this transcript
Working with ActionScript source paths
00:00Once in a while, you'll write code so useful you want to add it to Flash and use
00:05it all the time, just like the other built-in classes.
00:08Using source paths, it's easy to do that.
00:11On the stage, I have a movie clip called boarder_mc, that's an Instance of: Boarder.
00:17Boarder is linked to com.lynda.boarder.
00:20When I test the movie, you'll see that it has keyboard interactivity.
00:26If you look at my folder structure on my computer, you'll see that in the start
00:31folder, I have the com folder at the same level as my FLA file.
00:35By using a source path, I can move all of my ActionScript classes to another
00:40location on my hard drive and have all my FLA files reference that point.
00:45So I'll create a New Finder window and go to my Documents folder.
00:50In the Documents folder, I'll create a folder called classes.
00:55Now, I'll click and drag the com folder into that folder.
00:59So now there is no other file, along with my FLA.
01:02All my files and my classes are inside of that classes folder in my documents folder.
01:06So let's go back to the Source file and then test the movie.
01:11Notice the keyboard interactivity is disabled.
01:14So that connection is broken.
01:17What I need to do is connect to that classes folder and tell Flash that
01:21that's where my ActionScript classes are.
01:24You can do this on a per-FLA file basis, or you can set this as a Flash preference
01:29so it'll work with every FLA file.
01:32Let's look at both ways of how to do that.
01:35The first one is for one FLA file.
01:38Deselect everything and go to the Properties panel.
01:42In the Properties panel, click the Edit button next to ActionScript Settings.
01:47This enables you to change a few settings related to ActionScript.
01:51For now, let's focus on Source path.
01:54Click the Source path tab and then click the Plus button.
01:58After clicking the Plus button, click the folder icon to browse for a folder.
02:03I am going to go to my classes folder and then click Choose.
02:07Remember, if you save these files in a different folder on your hard drive,
02:10you'll have to pick that folder here.
02:13So I'll click Choose and then click OK.
02:16If I test the movie again, the keyboard interactivity is restored.
02:24So let's go back to ActionScript Settings, select that Source path and remove it
02:29by clicking the Minus sign and then click OK.
02:34Test the movie again.
02:36Keyboard interactivity is disabled.
02:37So now let's look at setting up a source path for all of your FLA files through
02:45ActionScript preferences.
02:48Go to Flash > Preferences and then click the ActionScript category.
02:52In the ActionScript category, click ActionScript 3.0 Settings.
02:57In here you can add a Source path, just like you did before, by clicking the Plus
03:01button and then clicking the folder icon.
03:04Browse to the classes folder and click Choose.
03:08Notice it's there in the Source path.
03:10Keep in mind if you just opened the final version of this file, this is not
03:15going to work, because this is the setting in Flash on my computer, not on your computer.
03:20So you'll need to follow the steps in order to get it to work.
03:23So click OK, and then click OK to close Flash Preferences.
03:29Test the movie again, and the connection is restored.
03:33So I can put all of my ActionScript classes in an organized place on my hard drive.
03:39Then I don't have to worry about bringing them along with the FLA files if I
03:43ever move the FLAs. So you can see that source paths can save massive amounts
03:48of time, enabling you to reuse code seamlessly, just like code already
03:53built-in to Flash.
Collapse this transcript
6. Using Symbol Linkage to Build a Simple Drag-and-Drop Game
Viewing the finished game
00:00Let's take a look at the drag and drop game we're going to build throughout this chapter.
00:05I'll test the movie of this FLA file.
00:07We'll see it in action.
00:09So you can click and drag little elements and drop them at a specified spot on
00:16the California map.
00:20When you drop an object on the wrong spot, it snaps back to its original position.
00:27But if you drop it in the right spot, it slides into place.
00:34Throughout this chapter we're going to make this application, step-by-step.
00:40So we'll add all the interactivity from scratch.
00:42So now that you have an idea of what you're going to build in this chapter,
00:48you're ready to start creating this app.
Collapse this transcript
Viewing the FLA file
00:00Before you start writing your code for this game, let's take a tour of the file
00:04to see how the FLA is structured.
00:07You should take particular note of this if you do not have access to
00:10the exercise files.
00:13On the Stage, you'll see the different elements that are going to be made draggable.
00:17So I have one called mission, hollywood, grapes, redwood, goldengate - all one
00:30word - all lowercase, joshuatree - all one word - all lowercase, laketahoe - all one
00:38word - all lowercase and then deathvalley - all one word - all lowercase.
00:44Also, I have copies of the movie clips that you can't see, because I
00:52turned down the alpha.
00:53For example, right here, I have selected deathvalley_target, deathvalley spelled
01:00all lowercase, and then I added _target to the end.
01:04So then you can see mission_target, hollywood_target,
01:10joshuatree_target, et cetera.
01:14So in the exact spot where I want the target position to be for each movie clip
01:19I've put a copy there.
01:24Not only do I put a copy there, but I copied the movie clip, and I made it a
01:29separate movie clip altogether.
01:30So we have the LakeTahoeTarget movie clip, which is different from the
01:35LakeTahoe movie clip.
01:39So if you're creating this on your own, just create duplicates of each of the
01:42movie clips you make for the draggable objects and then put them in the right
01:46place and turn down the alpha all the way to zero.
01:51It's very important that you're using copies of the movie clips that are
01:55named _target, and that the movie clips symbols themselves are different from
02:01the draggable once.
02:03So once you have all of your files set up just like mine, you're ready to start
02:07writing the code for this application.
Collapse this transcript
Creating the DragDrop and Map classes
00:00The first step in writing the code for this app is to create the DragDrop
00:04class and the Map class.
00:07The first thing we'll do is create the Map class.
00:10I'll do this by deselecting everything and then going over to the Class area in
00:16the Properties panel and by clicking Edit class definition.
00:19This will open up a window for creating a new ActionScript class.
00:25I'll choose Flash Professional as my editor, and for the Class name, I'll type Map.
00:30And then I'll click OK.
00:32Since I clicked that button from the Properties panel for the document class,
00:38Flash already knew that I wanted to extend the MovieClip class and added the
00:44import line of code for me.
00:45I'll click the button to auto format the code, save the file. Flash
00:53automatically goes to the same directory I am in, and automatically gives me the
00:58appropriate file name, which is the same name as my class.
01:01So I click Save As, and the file is saved.
01:05Now to click the DragDrop class, I'll go to File > New, choose ActionScript 3.0
01:12Class, and then click OK.
01:16I'll choose Flash Professional for my editor and then type DragDrop, capital D's.
01:26I'll autoformat this file, and now I'll import some code.
01:32So I import flash.display.*.
01:36So import all of the display object classes.
01:39Remember, that's what that wildcard does.
01:44So one of the display classes is MovieClip.
01:45Then I'll import all the event classes.
01:48So import flash.events.*.
01:54Then I'll extend the DragDrop class for this.
01:57All of these draggable elements are just one frame, so I can use the Sprite
02:00class instead of the MovieClip class, to have optimized code.
02:07So I'll save the file.
02:10Flash will automatically name the file for me.
02:12So I'll just click Save As.
02:14One more thing I want to do is import some additional code into the Map class.
02:20So I'll go back to the Map class.
02:21I'm going to import all of the display classes and all the event classes, as well.
02:26So I can actually highlight those two lines of code I just wrote in DragDrop,
02:33copy them with Command+C or Ctrl+C, go over to the Map file, highlight the
02:38import line of code that's already there, and then paste the code with Command+V
02:41or Ctrl+V. Now again, make sure to save this file after you change it.
02:45So now we've set up the basic classes that we're going to be using for
02:49this application.
Collapse this transcript
Linking the draggable class to Library symbols
00:00Now we'll link the DragDrop class to all the library symbols that can be
00:04dragged in the game.
00:06We'll start with DeathValley.
00:08Right-click, choose Properties, check Export for ActionScript.
00:14Now remember the difference between Class and Base Class.
00:19If we were to set DragDrop as the Class, then we could not recycle DragDrop.
00:25So what we want to do is set it as the Base Class and have Flash automatically
00:29create the main class, which is going to be DeathValley in this case and
00:34GoldenGate and Grapes and so on, without us having to write that code ourselves.
00:39So let's go to Base Class, and then we'll type DragDrop;
00:43capital Ds - Capital D for Drag and capital D for Drop.
00:48So click OK and you should get a warning message but not an error message.
00:53A definition for this class could not be found in the class path.
00:56That's right, because we want Flash to create the DeathValley class for us.
00:59We don't want to create that ourselves.
01:01We are using DragDrop for the Base Class.
01:03So click OK, and then we'll just repeat this process.
01:07So for GoldenGate, right-click, choose Properties, check Export for ActionScript.
01:12For the Base Class, type DragDrop.
01:15Now you may want to copy DragDrop, since you are going to be doing it several
01:18more times, so click OK.
01:22You can check "Don't show again" if you want, so you don't have to see
01:24that message again.
01:27Go to Grapes, paste over the Base Class, click OK, down to Hollywood.
01:56So just keep moving through all of these movie clips until you've gotten them all.
02:01Make sure not to do anything to the target movie clips, though.
02:05Those are going to have different interactivity that's not defined in an external class.
02:22So you should have DeathValley, GoldenGate, Grapes, Hollywood, JoshuaTree,
02:28LakeTahoe, Mission and Redwood.
02:31Once you've done all that, you should be able to test the movie and not have any errors.
02:37So it looks like everything is working properly. So now we successfully linked
02:43the DragDrop class to all of the necessary symbols.
Collapse this transcript
Adding drag-and-drop functionality
00:00The next step in making the game is to add drag and drop functionality.
00:04Now you may already be familiar with adding drag and drop functionality
00:07through code snippets.
00:09This time we are going to write the code all by hand, so you get some more
00:12practice and become more comfortable writing the code.
00:14The first thing I am going to do is go in the constructor, and right below where
00:19it says, constructor code, I am going to type
00:28(MouseEvent.MOUSE_DOWN, and then we'll run a function called drag.
00:41So we will create the drag function now.
00:48Go down a few lines below the constructor, so right below that next curly brace,
00:53and then type function drag(evt: MouseEvent), type :void, all lowercase and
01:10write some curly braces.
01:11Now to drag an object, all you need to do is write this one line of code,
01:16startDrag, with capital D. Close out the parentheses. Type a semicolon.
01:23Now to test out the drag code, save this file and then test the movie.
01:30You should be able to drag any one of these objects if you click your mouse down, there we go.
01:34Now it looks like the only way to drop one is to pick up something else.
01:41We will look at how to fix this in just a second.
01:46Right above startDrag, let's add an EventListener so we can stop dragging as
01:52well, but instead of adding the EventListener just directly to this object, like
01:57we are doing with addEventListener,
01:59we are going to add it to the Stage.
02:00So stage.addEventListener and event is going to be MouseEvent.MOUSE_UP.
02:10Remember, that's all caps.
02:14Now we are adding it to the stage because if someone clicks and drags an object
02:18around and releases the mouse button, they might drag really fast and release
02:23the mouse over something that's not the object that are meaning to drag.
02:26Now in case that happens, we are applying this EventListener to the stage, so
02:31wherever they release their mouse, the object will drop.
02:34Just type a comma and then type drop, and then close up the parentheses, and
02:37then type a semicolon.
02:41So now let's just copy and paste the drag function, and change the name to drop.
02:49Change stage.addEventListener to stage.removeEventListener and then change
02:54startDrag to stopDrag.
02:59Save the file and test the movie. We should then be able to click and drag and drop
03:07all the draggable objects.
03:09So just in case, you should try to drag and drop each one of these. It looks like
03:16it works for every one.
03:19So now the drag and drop functionality is there, and all the objects that link
03:24to this class are draggable.
03:26Again, you can see the power of writing this code in a class file, instead of
03:31writing it in a main timeline.
03:33It's easy to recycle, and it applies to as many objects as you want.
Collapse this transcript
Saving and resetting an object's position
00:00Now that all other right objects are draggable, the next step is to be able to
00:04save and reset an object's position if it's dropped in the wrong place.
00:09To do that, we're going to have to create variables inside of this class.
00:14Remember that variables connected to an object, as these variables will be,
00:19are called properties.
00:21So we'll create our properties right below the open curly brace for
00:26public class DragDrop.
00:28So go down right there and then type 'var orig,' short for original, X then
00:38datatype it to a Number and then just type a semicolon.
00:42You can do this when you create a variable or property to tell Flash
00:45you are going to give it a value later on.
00:47So we'll just do the same thing for the Y position, origY:Number;
00:55So these values are going to hold the original X and Y positions of each of the
01:02drag and droppable objects.
01:04We'll store those values in the constructor method.
01:08Remember, this code runs whenever the object is initialized.
01:13So when the application first loads, you have access to the original X and Y
01:18positions of all of the objects.
01:21So let's capture them here, origX = x;
01:27that's the X position of the object, and then origY = y, and that's it.
01:35So we capture them there, and then when you drop the object, we'll snap them
01:40back to the original position.
01:42So scroll down, and at the bottom of the drop function, or rather the drop method,
01:51remember that a method is the name for a function attached to an object,
01:54we'll add the code.
01:56x = original X, or origX, and the same thing for Y. So y = origY.
02:06So wherever you drop it, it will snap back to the original X and original Y position.
02:11Save the file and test the movie.
02:16Click and drag, and drop the object wherever you want.
02:19It'll snap back to its original position, as simple as that.
02:32You may notice, as you are dragging these objects around, that sometimes they go
02:35under other objects.
02:39Let me show you how to fix that real quick.
02:45In the drag method, at the very bottom, add one line of code.
02:49It's going to be parent.addChild(this).
02:57So what that will do is re-add the current object you are dragging to the
03:02parent object's display list, the parent being the main timeline, and will bring
03:06that object to the front.
03:08So let's save the file and test the movie, and you can click and drag any object
03:16over any other object, and it will always be in the front.
03:21So now we have our drag and drop functionality completely set up.
03:25Every time you drop an object in the wrong place, and for now, of course, the right
03:29place, the object snaps back to its original position.
Collapse this transcript
Giving a target drop object to the draggable objects
00:00Now we will connect each of the draggable objects to a target object so we can
00:04control what happens when an object is dropped in the right place.
00:08If you remember, each object, like the Mission object here, corresponds to a
00:14target object that has the same instance name but ends with _target.
00:22Remember that when you test the movie everything snaps back to its original position.
00:33So let's set up the target object functionality now.
00:36Go to DragDrop.as, and in here we will create another property called target.
00:44Right under original y (origY) create a property called target, and you can
00:50datatype that to a display object.
00:55Remember, a display object can be any visual object.
00:59For now our targets are movie clips, but if we wanted to reuse this code with
01:02another type of visual object, this generic display object will work for that as well.
01:08So now scroll down. Find the drop method.
01:14Again, method and function is pretty much synonymous.
01:18I am calling it a method because it's inside of the DragDrop class.
01:21So right below stopDrag, I'm going to create an if statement.
01:26What I want to do is check to see whether this object is touching the target object.
01:35So I type hitTest, with a capital T, Object, with a capital O, and some parentheses,
01:43and then I pass in target.
01:45So if the object that you drop is touching the target object, which by the way
01:50we haven't set yet so don't worry about that, then we will run the code inside
01:54of these curly braces.
01:55So the first thing we are going to do is we are going to hide the object that
02:00you dropped, and instead of putting the object that you dropped into place,
02:05we are still going to put it back to it's original position. We are just
02:09going to show the target object.
02:13If that's confusing at all, just write the code and take a look at how it
02:17works in just a second.
02:19Type visible = false so we are hiding the object that you dropped and disabling
02:27it's interactivity - that's what the visible property does.
02:29Go to the next line. Then we are going to show the targets.
02:33So target.alpha = 1.
02:38So we are showing the target there, and then we will run a method inside of the map class.
02:45We haven't created this method yet, but we are going to call it 'match.'
02:48So go down to the next line and then type parent, because remember this is
02:56on the main timeline so the parent object would be the map class or the document class.
03:01So parent.match and some parentheses and semicolon.
03:08Now take a second to think about anything that could possibly be wrong with this line of code.
03:14What do we have to add to avoid getting any errors?
03:19We have to tell Flash to treat parent as a different type of object.
03:24In this instance, I am going to tell Flash to treat parent as an instance of the Object class.
03:32So instead of MovieClip, I am going to use Object.
03:35The reason why I am doing that is because object always works.
03:39Again, if I ever change around my code a little bit to reuse this, and I use a
03:44different type of class for the parent class, my code won't break.
03:49So let's save this file and jump over to Map.as. And here what we are going to
03:58do is connect each of the objects on the stage that you can drag and drop with the target object.
04:06Now before when we worked in DragDrop, we didn't connected to the target object,
04:10we just specified what happens when it hits its target object.
04:15So let's go back map.as and set up the code here.
04:19The first thing I am going to do is create a property, right below the open curly
04:24brace, under public class Map.
04:28So I'll create a new line there and type var dragdrops, all lowercase.
04:36Datatype it to be an array and then just type a semicolon there.
04:42So we are creating our property. We will give it a value later on.
04:47Scroll down into the constructor method.
04:50Remember, the constructor always has the same name as the class.
04:56Below the constructor code comment, we will give a value to dragdrops.
05:01So type dragdrops equals and then square brackets for shorthand array notation,
05:09and in the square brackets, type the following: mission, hollywood, grapes,
05:23deathvalley, redwood, goldengate, joshuatree - no space all lower case - and
05:41laketahoe - also no spaces, all lowercase.
05:44Now notice this correspond to all of the draggable instances on stage.
05:50Before you go forward, you want to make sure that you have typed these all correctly.
05:53So save this file, make sure to save dragdrop.as and then test the movie, and
06:01you shouldn't see any errors.
06:05It looks like everything is working okay.
06:08If you see any errors, just go through all the code you just typed in this movie
06:12and make sure that everything is correct.
06:16We could write code like mission. target equals mission_target,
06:23and then repeat that code for each of the objects inside of
06:27the dragdrops' array.
06:29However, now that the object are stored inside of an array, we can just create a
06:33loop and write the code once and have Flash to do all of the work.
06:38So let's take a look at how that works.
06:41Create a for loop. Just type for, some parentheses, and some curly braces.
06:48In the parentheses, we will create a variable called i, datatype it to a positive
06:54integer that's uint - all lowercase - set equal to 0, and
06:57we will run this loop as long as i is less than dragdrops.length,
07:05and then we will increment i by one each time the loop runs.
07:11So we are going to run this on each object in the dragdrops array.
07:14So before we write the code inside of the loop, I am going to add one line above the loop.
07:20I am going to create a variable called current object, so type var currentObject
07:26capital O, datatype that to DragDrop.
07:33One of the new features in Flash CS5 is that you get code hinting for
07:36your custom classes, so we have it right here.
07:38It's nice and convenient. Don't give it a value now; just type a semicolon.
07:44This is going to represent the current object in the array as we loop through it.
07:50So inside of the loop, type currentObject, capital O, equals, dragdrops, all
07:59lowercase, index i, or put i in square brackets, and then a semicolon.
08:05So each time the loop runs, the current object is going to be mission and then
08:10hollywood and then grapes, etc.
08:13So let's go down to the next line, and then what we are going to do is set
08:17the target property.
08:19So currentObject, capital O.target, see how that property
08:26comes up in the hinting window, even though w just created it a few minutes ago.
08:29It's the magic of Flash CS5. So we will type that and then space = space.
08:38And what we are going to do is set it equal to the
08:42object that corresponds to the name of the current object, which is mission for
08:47example, or hollywood for example, and then add on _target.
08:52To do that, we are going to use the get child by name method.
08:56So type getChild, capital C, By capital B, Name with a capital N, and some
09:03parentheses and a semicolon.
09:05Now here we need to type in the name as a string.
09:09So we have type in currentObject capital O, .name to get
09:15the current object's name as a string.
09:16So this is going to be mission or hollywood, but not as the object - as a string,
09:23so, text and not a movie clip, and then we need to tack on _target.
09:28So after that, type a space then a plus and space, and in quotes, type _target.
09:36So now save the file, and we'll just add a little bit more code to this Map file, and
09:42then we can test it out and see how it works.
09:44So scroll down, and right below the constructor - now this is going to be kind
09:51of confusing which curly brace to use because we have four closed curly braces in a row -
09:56so what you can do is scroll up and look right under the constructor names of
10:00public function map, and there is an open curly brace there.
10:04If you go straight down, the closed curly brace is the one that corresponds
10:10to the constructor.
10:12So we have the closing curly brace for the for loop and then the constructor.
10:15We want to put it after that.
10:17If we put it after the next closed curly brace, it would be outside of the
10:22class declaration, and if we put it after the for loop, then it would be inside
10:27of the constructor method.
10:28So we are going to define the match method as a method inside of the class.
10:33So type public, and the reason why we are typing public is so we can access this
10:38method from inside the dragdrop class.
10:42Remember, we made a reference to this function earlier in this movie, but we
10:45didn't create it yet, so we are creating it now.
10:47It's public function match, and some parentheses, colon void, and some curly braces
10:56We will add the code that this function runs later on, but for now we just want to define it.
11:02So save the file, and you should be able to test the movie without any errors.
11:09So click and drag an object to the right place.
11:13If it works for one object, it should work for every object and looks like it.
11:20Oh, that was a wrong one.
11:22You should know this as a California native, huh?
11:30So I close this window, and if you want to make one more change, you can
11:36actually add the hand cursor to appear when you roll over a movie clip that
11:40you can drag or drop.
11:41Let's take a look at how to do that real quick.
11:43Go to DragDrop.as. I am going to add one more line to the constructor method and
11:51for this line, just type buttonmode, capital M, equals true, and a semicolon.
11:58Save the file, test the movie, and there is the hand cursor.
12:11So now that we are in DragDrop.as and we have written all the code, scroll down,
12:16and we will review the drop method. So here is what this code says:
12:22if the object that you drop is touching its target, which we set in the Map
12:26class, you are going to hide the object that you dropped and show the target.
12:35So what you are actually saying when you let go of the object that you drop
12:39is the target object showing.
12:42The object that you dropped is always going to go back to its original x and y position.
12:47The reason why we are doing that is to make it easy to reset the game later on.
12:52Then what happens is we are running the Map classes match method that we just defined.
13:00So now when an object is dropped in the right place, it snaps into position, and
13:05the interactivity for that object is disabled. In addition to that, we have set up
13:11a hand cursor for when you roll over any of the interactive objects.
Collapse this transcript
Showing a Win screen
00:00Once a game is finished, we will show a screen indicating the game has been won.
00:05This screen is inside of a movie clip that I called 'win.'
00:09So if you look at the Properties panel you see it's just win, all lowercase, and
00:14there's really nothing special to it.
00:16There is a button in there, but the interactivity is all going to be on the
00:20whole movie clip, and not just on that button.
00:23So I am not going to go inside the movie clip for now.
00:25So if you don't have the exercise files, you can just put anything you want
00:31inside of a movie clip.
00:32But it should take up the full width and height of the stage.
00:35So let's go over to Map.as. In here, we will create some new properties under
00:43the dragdrops property.
00:44Create one called numOfMatches, capital O, capital M. This is going to represent
00:53how many matches the person got when they are playing the game. And so we will
00:58use that to count against the number of items inside of the dragdrops Array.
01:03So if they have a number of matches that is the same as the total number of
01:07objects in the dragdrops array, that means they dropped everything in the right spot.
01:14So in that case, they have won the game, so we want to show the winning screen.
01:18So let's datatype this to a uint, and we will set it equal to zero.
01:23Then we will cerate a variable or property called speed right under that.
01:30That's going to be a number.
01:31I will give that a default value of 25.
01:35Now speed is going to represent how fast the win screen comes up, because we are
01:38going to have it animate using ActionScript.
01:42Now scroll down to the match function.
01:48In here, type numOfMatches, capital O capital M, plus, plus.
01:55Remember, the plus plus operator increments a value by one.
01:59So it adds one to the current value of it.
02:01So each time it get a match, we are going to add to the value of numOfMatches.
02:06And just like you would expect, minus minus decrements a value by one.
02:10So it takes one away from the current value.
02:11But for now, we just need the plus plus operator.
02:14So go to the next line of code and create an if statement, and here we are going
02:19to check to see if they won the game.
02:23So they won the game when numOfMatches is equal to the total objects inside of
02:28the dragdrops array.
02:30So inside the parentheses here, we will type numOfMatches, capital O, capital M, space,
02:37equals, equals, space.
02:39Remember that two equal signs means 'is equal to.'
02:41That's the comparison operator that you use when you are using an if
02:46statement - different from the single equal sign which is used to set a value in ActionScript.
02:52So then type dragdrops.length.
02:57That signifies the amount of items that are inside of the dragdrops array, which
03:01is every drag-and-dropable object.
03:04So when we have a match for every drag- and-dropable object, then we won the game.
03:08So we are going to put the winning code right in this if statement here.
03:12So type win.addEventListener, capital E, capital L, put in some parenthesis
03:21and a semicolon, and here I am going to type Event, capital E .ENTER_FRAME all
03:28caps. This is going to run with every frame of the movie.
03:33So it's going to run 24, 30, 60 times a second, depending on your frame rate.
03:37So comma and space, and then we'll run a method called winGame, capital G, so we
03:43will define that method right now.
03:47Right below the Match method, create a new method. This time we can just type
03:54function, we don't have to type the word 'public.'
03:55So we will call it winGame, capital G, and in the parentheses, that will receive an
04:00event with the datatype of Event.
04:04Notice this parameter is blue saying it is a built-in key word in Flash, but I
04:09am using it anyway as my custom parameter, and in this case, it's actually okay.
04:15You will notice that if you had a code snippet that has an event handler, Flash
04:19will actually use this word Event.
04:21So if you don't like that, you can just simply type evt, but personally I mostly
04:28use the word event here, so your call. Colon, void, and some curly braces,
04:35and here we are going to have the win movie clip animate.
04:39So we will type win.y -= speed;
04:44so each frame, the win movie clip is going to move up - that's what the minus says.
04:49Remember, the top edge of the stage is the 0,0 position.
04:53That's going to move about 25 pixels per frame.
04:58So that's how it's going to animate.
05:00Then what we are going to do is wait until the win movie clip gets all the way
05:06to the top of the stage, and then stop it.
05:09So type if and some parenthesis and some curly braces. We are going to
05:15check to see if the win object's position is less than or equal to zero.
05:20So if win.y is less than, so less than sign, or equal to, so equal sign 0,
05:26then we are going to set its position to zero.
05:30So win.y = 0, and then we are going to stop running that inter-frame command.
05:37Just remember that takes up a lot of memory running a command in
05:42multiple times per second.
05:44So let's just highlight win.addEventListener(Event.ENTER_FRAME and copy it with
05:49Command+C or Ctrl+C. And right below where we set win's y position to 0, we will
05:54paste that code and then change 'add' in addEventListener to 'remove.'
06:00So we are going to take away that EventListener.
06:04So we are just disabling that block of code, so winGame won't continue to run
06:09after the win movie clip is in the top position.
06:13All right, so let's see this in action.
06:16But remember, before you test it, you need to save the ActionScript, or else it won't work.
06:20File > Save and then test the movie and then put all the objects in the right place.
06:27So this goes here. Look at that.
06:39All right, now there is the good job screen. You can click it and nothing happens.
06:47There we go.
06:48We successfully detected whether the player won the game.
06:53So remember, if you ever want to animate object vertically, you can add an
06:57inter-frame listener, but just make sure to remove it once the object is
07:01in place.
Collapse this transcript
Resetting the game
00:00After someone is finished with the game, they may want to play again.
00:04Let's take a look at how to do that.
00:06So let's scroll down and find the winGame method.
00:16Now if you're ever looking at these methods in here, because we're going to have to tab
00:19in so much to organize the code,
00:22if the word wrap bothers you, then you can always turn it off.
00:25But if you still want it on anyway, but you want more space,
00:29you can always just click these Minimize buttons to minimize the panels, and
00:34double-click the Timeline to get a little bit more space.
00:39So here, right below win.removeEventListener, I'm going to add
00:44win.addEventListener, capital E, capital L. This is going to be for a mouse
00:51click, and this is how we're going to reset the game.
00:53So we'll type MouseEvent capital M, capital E, .CLICK, in all caps,
00:57then a comma and space, and then clickWin capital W.
01:03So we'll have to define that method now.
01:05Right below the winGame method, make sure you're doing it below the right curly braces.
01:12So you should have two extra ones at the bottom:
01:15one to close out the class and one to close out the package.
01:19So here, type function clickWin, capital W, in parentheses,
01:24type event:MouseEvent.
01:27After the parentheses, type colon void, add some curly braces.
01:33Then what we're going to do is remove that mouse click event listener in here.
01:36So highlight win.addEventListener, copy it, paste it into clickWin and
01:43change add to remove.
01:46So we want to disable it after we've clicked it once.
01:48Then we're going to add an event listener to have the object animate downward.
01:54So actually, I'm going to copy win.removeEventListener, winGame right here.
02:00Copy that, Command+C or Ctrl+C and then paste it right below,
02:05win.removeEventListener(MouseEvent.CLICK).
02:09Here, I'm going to change removeEvetnListener to addEventListener.
02:12I'm going to leave Event.ENTER_FRAME.
02:15Then I'm going to change winGame to animateDown, with a capital D.
02:20For animatingDown, I'm simply going to select and copy the winGame method,
02:26Command+C or Ctrl+C, and paste it right below clickWin.
02:32Change the method name in the pasted code to animateDown, capital D, and then
02:38change, win.y -= speed to win.y += speed.
02:44Then change the conditional statement to be if win.y is greater than or equal
02:51to the height of the stage, which can be accessed through stage.stageHeight.
03:00When that happens, we'll set win.y equal to the height of the stage, which
03:04is stage.stageHeight.
03:05This is going to align the top of the win movie clip with the bottom of the
03:09stage, just like it is before you play the game.
03:11Then we're going to remove that event listener to animateDown.
03:15So I'm going to highlight win.addEventListener animateDown.
03:20That's on line 50 in my code.
03:21I'll copy that, and then I'll highlight win.remove EventListener and
03:27win.addEventListener in the animateDown method, and paste the code I just copied.
03:33In the pasted code, I'll change addEventListener to removeEventListener.
03:38So basically, all that we've done so far is make it so that when you click the
03:41win movie clip, the win movie clip just animates right back down.
03:45So we haven't reset all the properties of the game yet.
03:47We'll do that in just a second.
03:49First, let's test to see if this animation works.
03:51Go to File > Save and test the movie.
03:56Drag all the objects into the right place.
04:03You should be an expert at this by now.
04:09You get the win screen.
04:11If you click on it, it goes back down.
04:13So now all we have to do is reset all the objects.
04:16So let's close the SWF, and then go up to the clickWin method.
04:23At the bottom of the code, we're going to add a for loop to loop through all of
04:27the drag/drop objects.
04:29We've actually already written the for loop that goes through all the drag/drop
04:33objects at the top of this file.
04:34So let's just copy that.
04:35I'm going inside of the constructor method, and I'll copy every line from var
04:41currentObject:DragDrop to the end of the for loop.
04:46Copy that with Command+C or
04:47Ctrl+C. Scroll down to clickWin, and still inside of the method, I'm going to
04:52paste the code with Command+V or Ctrl+V.
04:56Note that I have a variable called currentObject here.
04:59I'm also declaring it here, in the constructor.
05:05Whenever you create a variable inside of a function, that variable can only be used
05:10within that function.
05:11So I can't make a reference to currentObject outside of this function.
05:14If I define the variable in the class, outside of any method, then I can use it
05:19wherever I want in the file.
05:22This existence of variables, like whether they can exist inside of a function or
05:27throughout all of your whole file, is called a variable's scope.
05:34So let's scroll down, and in clickWin, I'm going to change the line of code that
05:41says currentObject.target = getChildByName.
05:45I'm just going to erase everything before getChildByName in this line of code.
05:50So I'm referencing that currentObject's target.
05:54I want to set every target object's alpha to zero.
05:57So right after the close parenthesis here, I'll type .alpha = 0.
06:04Then I want to show all the objects that you can drag and drop.
06:06So I'll go below that, and then type currentObject.visible = true.
06:17So I don't need to position them, because I already positioned them in the
06:19drag and drop class.
06:20Remember, they just go back to their original position, but they turn invisible.
06:25So right below the for loop, I'm going to add the - one of the last lines of
06:29code to finish the game.
06:31I need to set the number of matches back to zero, because if I don't set it
06:37back, then it's going to keep going up every time,
06:39so I'm going to have a problem.
06:42So I'll type numOfMatches =0. Now let's save this file, and test the movie,
06:51and see what we get.
06:55Click and drag all the objects into place.
07:07Look at the win screen.
07:09Now I want you to pay attention to what happens when I click the win screen.
07:12Watch where the drag and drop objects are.
07:16Notice that they all come up above that win movie clip.
07:19That's a little bit awkward and that's happening, because every time we drag an
07:23object, we bring it all the way to the front.
07:25So we can fix that by putting the win object in the front.
07:32So we can actually do that right here inside of clickWin.
07:35So I'll go down one more line, and type addChild, and in parentheses, pass in win.
07:43Save this file and test it one more time.
07:50Drop all the objects into place.
07:58Watch the win screen.
08:00When you click the win screen, pay special attention to the drag drop area.
08:02So the win screen is now in front of all of those objects.
08:07Note that you can still see them because there is a little transparency.
08:10But they're not clearly in the front, fully opaque in front of the win object.
08:15So now the game is completely finished, and the person playing can click to
08:20reset the game, and play as many times as they want.
Collapse this transcript
7. Working with Bitmap Images to Create a Slideshow
Loading bitmap images from the Library
00:00There are few different ways to work with image files in Flash.
00:03Of course, you can display the images in the Timeline by importing them.
00:07You can load them in through the library at run time using an ActionScript, and
00:12you can load external bitmap images.
00:15Right now, we are going to focus on loading the image from the library.
00:20The advantage to doing this is that the image gets embedded inside of your SWF
00:24file so that you can access the file anytime you want, and you don't have to
00:28worry about the file creating additional load time.
00:32Now of course the disadvantage is that your SWF file has a bigger size, but for
00:37your application, it may be better to not have to wait to load.
00:42So right now this application has a simple preloader-type graphic on the stage.
00:46If I test the movie, you will see the animating circles.
00:49If you are curious about how this is set up, I simply have a bunch of different
00:54graphic symbols of circles fading in - it's all the same one - and then I have
00:59them starting at different frames.
01:03So, for example, this one starts at frame 21,
01:06this one starts at frame 18, et cetera.
01:08So let's go over to the library, and I will right-click alcatraz.jpg, and I
01:15will choose Properties.
01:17Here I will check Export for ActionScript.
01:19If you don't see that, just make sure to click the Advanced button in the
01:22Bitmap Properties window.
01:24When you check Export for ActionScript, you will see that the Base Class and
01:29Class fields are populated.
01:31For the Class, you don't want to use alcatraz.jpg because it has a dot in it.
01:35You want to use a name with no spaces or special characters, including dots.
01:40So type Alcatraz, with a capital A. Note that the Base Class is the BitmapData class.
01:50That's a class that holds pixel information for an object.
01:53So let's click OK, and then we will go to the first keyframe of the actions
01:59layer, and open up the Actions panel.
02:02The process of creating a bitmap from the library is pretty similar to creating
02:06a movie clip from the Library, with one significant difference.
02:09We are working with pixel data in the BitmapData class, so we need to take that
02:14data and display it in a visual manner.
02:17So type var and then a space.
02:19I will call this 'bd,' short for BitmapData, and this is going to be Alcatraz.
02:27Remember, Alcatraz has the Base Class of the BitmapData.
02:32Set that equal to a new instance of the Alcatraz class.
02:41So the next step is to display the pixel information, because right now Alcatraz
02:46is just a whole bunch of pixel data, and they are not displayed yet.
02:51So we need to display it in a container called Bitmap, so I create a variable
02:56called 'bmp,' and that's short for bitmap, datatype it to a bitmap.
03:01Again, a bitmap object is something that can display bitmap data, or in other
03:05words pixel information.
03:07Set that equal to new bitmap, and in the parentheses, pass in 'bd' for the BitmapData
03:14So the Bitmap is a display object for displaying BitmapData.
03:19So go to the next line add.
03:22If you test the movie right now, you won't see the image because it's not added
03:26to any display object container.
03:28So we need to put it on the stage first.
03:31Type addChild, capital C, and in the parentheses, pass in bmp.
03:37Test the movie, and there is the image on the stage, loaded from the library.
03:44So to load a bitmap image from the library, right-click it, choose Properties,
03:51export it for ActionScript and then your code, remember that you need to create
03:57a BitmapData object, which is going to be the pixel information for your image,
04:01and you need to put that inside of a bitmap and simply add it to the stage.
Collapse this transcript
Loading bitmap images from external files
00:00Loading images from external files allows you to change the images in your
00:04applications without having to republish your SWF.
00:08In addition, your SWF files don't get bloated by the file size of the images.
00:14In the same folder as this FLA file, I have a folder called assets.
00:20In that folder, there is a file called boats.jpg.
00:24If you don't have the exercise files, make sure you have your files set up in a similar way:
00:30an assets folder in the same directory as the FLA, and an image called
00:34boats.jpg, all lowercase.
00:36Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:41In order to load an external image file or SWF, you use a class called the loader class.
00:48So type 'var,' and then a space, and then type 'loader' lowercase, datatype it to a Loader.
00:58Flash writes the import line of code.
01:03Set it equal to a new Loader();.
01:09So loader is a class that's not built into the Flash interface, like a movie
01:13clip or a bitmap image is.
01:15But at the same time, calling this code to create a new loader is just like
01:21saying create a new empty movie clip.
01:24So that's what the New command does.
01:27Let's go to the next line, and we'll have the loader load the file that we want
01:32to load, so loader.load.
01:35In parentheses, you can see that the first parameter is request, which is a URLREQUEST.
01:41You may be familiar with URLREQUESTs already.
01:46The code snippet that links to it an external web site,= uses the URLREQUEST.
01:51So use the same URLREQUEST concept when you're loading any kind of file into Flash.
01:57So in here, I'm going to type new URLREQUEST and parentheses after that.
02:05Flash tells me I need to put a string inside of there.
02:07That's going to represent the location of the file.
02:10So I'll close out the parentheses first.
02:12So I have should have two close parentheses:
02:13one for the new URLREQUEST, and then one for the load command.
02:19Inside of the URLREQUEST parentheses, I'm going to type some quotes, and then
02:23the path of the file I want to load, which is
02:26assets/, so assets is a folder,
02:32inside of there is the file I want to load called boats.jpg.
02:38So if you don't have the exercise files, make sure you're typing the correct
02:41path to your file here.
02:43Again, this is relative to your SWF file.
02:47Now I'll go to the next line and add one more line of code.
02:52Remember, that in order to see anything on the stage, you have to add it to some
02:57sort of display object container.
02:59So add the loader to the stage, by typing at addChild, with a capital C, and in
03:05parentheses, type in loader, all lowercase.
03:09So I'll test the movie.
03:13There is the boats image on the stage.
03:15Now I'll close the preview window.
03:19Let's take a look at one more thing.
03:22Let's say we wanted to make sure that we didn't put the object on the stage
03:27until the file was done loading.
03:29So you want to check to see when the loader is done loading the boats file.
03:39To do that, go above the line of code where you have the loader load the file.
03:44This is very important.
03:45You want to make sure that this line of code is processed first.
03:48So we go in and tell Flash to add an event listener, listening for when the file is done loading.
03:54So type loader and then a dot.
03:58Now you may be thinking that you're going to type addEventListener right here,
04:03but actually, you don't add the EventListener to the loader itself.
04:06The loader actually has a property called contentLoaderInfo, and you're going to
04:10add the event listener to that object, and not the loader directly.
04:15So the key things are: add the event listener before you load the file, and
04:18you add it to the contentLoaderInfo property, and not the loader itself.
04:24So after that, I'll type a dot, and then addEventListener.
04:25The event is just going to be event.COMPLETE.
04:30This is usually used when files have done loading.
04:33So a comma, and then a space, and then type fileLoaded.
04:38Close out the parentheses and type a semicolon.
04:40Then just wrap that addChild command in a function.
04:44So type function fileLoaded, capital L. Then in the parentheses, type event:Event,
04:52with a capital E. Close out the parenthesis. Type :void.
04:57Wrap addChild in curly braces.
04:59You can autoformat if you want.
05:02Then test the file, just to confirm that the image is still on the stage, and there it is.
05:09So you can load external files using the loader class.
05:13Remember that you need to add the EventListener to the contentLoaderInfo
05:17property before issuing the command to load the file.
Collapse this transcript
Adding mouse functionality to bitmap images
00:00The bitmap class doesn't come with support for interactivity, like mouse clicks.
00:04In order to make an image clickable, you'll need to take a few extra steps.
00:09Now, think of the bitmap class in ActionScript just like a bitmap on the Stage.
00:15So, if I go to the library and I click and drag this boats image onto the
00:19Stage, like so, I select it, I can see that it's a bitmap in the Properties panel.
00:27So, when I work with a bitmap it's a little bit different than working with, say
00:32a movie clip or a button.
00:34I can't give it an instance name, so I can't add things like mouse interactivity.
00:37So that's the same as it is in code.
00:40Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:46So if I were to type bmp. addEventListener and MouseEvent.CLICK, capital M,
00:54capital E, and then CLICK in all caps, and I were to run a function called
00:59bitmapClicked, I create the function now, so bitmapClicked, capital C, in
01:08the parentheses to event:MouseEvent, capital M, capital E, colon, void, no
01:15return data here, and then inside of bitmapClicked, I'll just simply type a trace statement.
01:23So trace, in parentheses, some quotes, and I'll type clicked inside of the quotes.
01:29Now, if you test the movie and click the image, nothing happens.
01:33So we don't have that interactivity, but what can you do if you're working with
01:39a bitmap image and you want to add interactivity? Think about this.
01:43What do you do when you want to add interactivity to a bitmap on the Timeline?
01:47You wrap it inside of a button or a movie clip symbol.
01:51So I can wrap it inside of any other clickable object, and it will work just fine.
01:56For this situation, a MovieClip would work, but it's only one image, so we might
02:01as well use the sprite for the optimization.
02:04Remember, a sprite is a single frame movie clip.
02:06So right below the line of code where you created the new bitmap, let's create a
02:11variable called sprite, all lowercase, and datatype that to a sprite.
02:14We'll set that equal to a new sprite and then add it to the Stage.
02:22So addChild and sprite in parentheses.
02:27So that essentially creates an empty one-frame movie clip, and then we'll add
02:33the bitmap as a child of the sprite.
02:36So the line of code that says addChild bitmap, just change that to
02:40sprite.addChild bitmap.
02:42So, we're taking the bitmap image and putting it inside of a sprite, just like
02:46putting it inside of a MovieClip. And now instead of adding the EventListener to
02:50the bitmap, we add the EventListener to the sprite,
02:54test the movie and click, and there you have it.
03:02If you ever want to see the hand cursor, just set the buttonmode property.
03:06So, sprite.buttonmode = true;
03:11and then test the movie, and there is that hand cursor.
03:17So just like when working on the Timeline, if you want to add functionality to
03:21an object, all you have to do is wrap it inside of an object that supports that
03:26functionality, just like we did with the sprite and the bitmap image here.
Collapse this transcript
Using a loop to load multiple images
00:00When you're loading thumbnails for a slideshow, you want to make sure you're not
00:03writing the same code multiple times.
00:06Using a loop, you can write the code once and have that code repeat until all of
00:10the thumbnail images are loaded.
00:12Let's take a look at the application I have here.
00:15If test the movie, you can see an image in the background that's loaded
00:19externally and then a clear bar that's going to hold the thumbnails.
00:26The clear bar is just a movie clip that's transparent called thumbBg, so the Alpha is set to 60.
00:34Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:39So in my code, I have some import statements, and then I have all the images in an array.
00:46Notice that these are the file names of the images and not the full paths to the images.
00:52I'm adding the full paths later on.
00:54The reason why I want to do that is so I can use the file names to represent the
00:58full size image as well as the thumbnail.
01:03Next, I have variables that store the X and Y position for the thumbnails.
01:08These represent the position of the first thumbnail's X and Y coordinates.
01:14Using a loop, we'll modify these values each time an image is loaded. I'll scroll down.
01:22Here is the code to load that background image -
01:27this is the only full-size image in this application, and then after that's
01:31loaded and added to the stage, I added the thumb background, so that
01:35it's in front of the loaded image.
01:37Then I'm running a function called loadThumbs.
01:41Here is the loadThumbs function.
01:42Notice that it's empty right now.
01:44Here's where we're going to write all the code to load the thumbnails.
01:47So the first thing we'll do is create a variable called thumbLoader and data-
01:52type it to a loader.
01:54We'll give it a value later.
01:56This is going to be recycled for each of the thumbnails that loads.
02:01Let's create a loop now.
02:04Create the skeleton of a for loop;
02:06so 'for' some parentheses and some curly braces.
02:09Inside the parentheses, type var i:uint, positive integer, = 0; i < images.length.
02:25That's the amount of items that are in the images array, so it's going to run
02:28for each image, and then a semicolon and a space and then i++. Standard loop.
02:36In here, we're going to create a new thumbLoader, so thumbLoader = new Loader.
02:46Now I want to point out that it's not creating the same loader every single
02:51time, so each time the loop runs, we're actually creating a new instance of the Loader class.
02:56So we're going to have one loader for each of the thumbnail images.
03:01That's significant because if you use the same loader image then each time the
03:06loop runs it would have the same loader load the image file,
03:10so by the end of the loop, we will only see the last image that's loaded into the loop.
03:16So we need to create a new loader each time.
03:18That's what we're doing here.
03:20So then we'll load the image, thumbLoader.load, and in the parenthesis, we'll
03:26pass in a URLRequest.
03:29Before I write that, I want to make sure I close out the parentheses and type a semicolon.
03:33So go back in the parentheses and type new URLRequest and then some parentheses.
03:39Now we are going to type the path to the file.
03:43Remember, we didn't have the path to the files inside of the array, so we're
03:49going to need to write the path now.
03:51Here's the path to the thumbnail images.
03:55Put some quotes and type assets/thumbs/ - these are both forward slashes - and then
04:04after the quotes, type space plus space, and then we'll type images Index i,
04:10which is represented by i in square brackets after images.
04:14So let's go and represent each image file as we loop through this for loop.
04:20Let's go to the next line.
04:23Then we'll begin positioning the thumbs. So thumbLoader.x = thumbX, make sure
04:32that X is capitalized in thumbX, and thumbLoader.y = thumbY.
04:42Now what we're going to do is add to the thumbX property, so we'll shift each
04:47thumbnail by 85 pixels each time.
04:50So type thumbX, make sure the X is capital, + = 85.
04:58So each time the loop runs, we're going to add on to the value of thumbX in the
05:03amount of 85 pixels (thumbX += 85;).
05:07So the first time the loop runs, thumbX will be a value of 30, so the X position
05:14of the first thumbnail will be 30.
05:15Then the next thumbnail will be at a position of 30 plus 85.
05:20You can do the math in your head if you want. And then the next one will be 85,
05:25after that and then 85 after that, and so on.
05:28So let's go the next line and then add the thumbnails to the stage by using
05:32the addChild method.
05:33So addChild and in parentheses, pass in thumbLoader, with a capital L. Test the
05:40movie, and there are all the thumbnails.
05:43Now of course, they don't do anything when you click on them just yet, but we
05:46can add that functionality later on.
05:51The thing that I want you to take away is that to load multiple images most
05:55effectively, put the image names inside of an array and use a for loop to load each image.
06:03That way you only have to write the code once, and no matter how many images you
06:07have, you don't have to write additional code.
06:11You just need to add the images to your array.
Collapse this transcript
Creating a simple slideshow
00:00Now that you're familiar with all the different parts of creating an image
00:03viewing app, we'll finalize the slideshow.
00:06We'll do that by adding interactivity to the thumbnail images.
00:10Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:15Scroll down to the line of code where you add that the thumbLoader to the stage.
00:20That's inside of a loop that's inside of loadThumbs.
00:23So right below addChild(thumbLoader), I'm going to add an event listener to
00:28thumbLoader. So, thumbLoader, capital L, dot addEventListener, and the event's
00:34going to be MouseEvent.CLICK, capital M, capital E, and then CLICK is in all
00:39caps, and then I'll type thumbClicked, with a capital C.
00:46Below the loadThumbs function, type function thumbClicked, capital C, and
00:53we'll see an event with the data type of MouseEvent :void for no return data and some curly braces.
01:03So here what we want to do is create a reference from the thumbnail that was
01:06clicked to the image that we want to load.
01:10They have the same file names, but we're going to need to break apart the file
01:18name of the thumbnail to get the path to the full size image.
01:22For example, the thumbnail is in the folder assets/thumbs.
01:29The full size images are in the assets folder, so we're going to need to get the
01:35path to the file that was clicked, and
01:37then what we need to do is break apart that path and change it to load just the image.
01:46So in the thumbClicked function, let's get the URL, or the path in other words, to the
01:52image that was clicked.
01:53To do that, create a variable called path. Datatype it to a string.
01:59Here, we'll set the path equal to event -
02:03that's the event that occurred, in other words the mouse click - dot currentTarget.
02:08That refers to the object that was clicked, so it's going to be a thumbLoader.
02:15It could be for any thumb that was clicked.
02:18So we want to get that particular thumb's URL, so then we type
02:23.contentLoader, capital L, Info, capital I. Now the contentLoaderInfo comes
02:32from the loader object.
02:34Remember, the contentLoaderInfo is the object where you add the event listener
02:39to check when the file is done loading.
02:41This is the object inside of a loader that has all of the information about the
02:46loaded files, so this is how we access the URL. So type .url.
02:53If you want to see the long URL to the file, you can trace that by typing out
02:58trace and putting some parentheses and typing path.
03:01Make sure there are no quotes inside the parentheses.
03:03So test the movie and click on the thumbnail.
03:07Then you can close the window and look in the Output window.
03:10So we see file, then all of these forward slashes, and the direct location of that file.
03:17So what we really want to do is just cut off everything except for alcatraz.jpg,
03:22and then add on assets after that.
03:24So let's go back to our code, and
03:28then we'll set a new value for path, so type path = path.substr.
03:38This is short for substring. So when you take a string and you want to get a substring of that, so we just
03:44want to get a piece of this giant string.
03:47In the parentheses, we need to put the starting index.
03:51Now we want to start at the last time there is a forward slash.
03:56So there is a forward slash after thumbs, and that's the last forward slash in the
04:00file, so we want to start one after that spot.
04:05So inside of the parentheses, type path.lastIndexOf - capital I, capital O, make
04:13sure you close out the parentheses first - and inside the parentheses type some
04:18quotes and type a forward slash, so that's going to be the last index of the
04:23forward slash, so the last time the forward slash appears.
04:26Then after the closed parentheses for last index of and before the closed
04:31parentheses for substring, type space plus space and then one, so we want to
04:37start one after the forward slash, and go all the way to the end.
04:41So let's test the movie again and see what we get in the Output window.
04:45So click on an image, and now we get just the image name.
04:49Nice! So, we've successfully broken apart that string.
04:55So the next step is to have the loader, which is the main image loader, not the thumbLoader,
05:01the one we created right here on line 12 in my code, we'll have that loader load
05:08the appropriate image.
05:09So type loader.load.
05:11I'll need to create a URLRequest, so type new urlRequest, capital R, some
05:20parentheses, make sure you close out both the load command and the
05:23urlRequest command.
05:26Inside of those parentheses, we're going to start with assets, because the image
05:29is inside of the assets folder, so type assets/ and after the closed quote, type
05:36space plus space, and then type path, so the path is going to be different for
05:42any image that's clicked on.
05:43Then we're going to manipulate that string to get just the file name and then
05:48tell Flash to load the appropriate full size image.
05:52So now I'll test the movie, and we'll preview the application.
05:54Click through the different thumbnails, and the full size images will load in. Nice!
06:03Now there is only one thing missing.
06:06One thing this app can improve upon is to have the hand cursor show when you
06:10roll over the images.
06:11Let's take a look at how to do that.
06:14One problem that you'll encounter when you try to add the hand cursor is that
06:18the loader class does not support the buttonmode property.
06:21So you can't put the hand cursor in a loader.
06:26The way to work around that is just like how you would do it on the Timeline.
06:30You take the object, and you put it in something that supports the
06:33functionality you want.
06:36If you can't perform a motion tween on a text field, then just put it inside of a MovieClip.
06:39If you need to change the alpha of a bitmap, put it inside of a
06:44MovieClip, et cetera.
06:45So we're going to do that here with the sprite.
06:48Let's scroll up to the loadThumbs function, and right below the thumbLoader
06:54variable, I am going to create a new variable called Container.
06:58Datatype it to a sprite.
07:01Set it equal to a new instance of the sprite class.
07:03Remember, sprite is a one-frame movie clip.
07:06On the next line, I'll add it to the Stage using the addChild method.
07:11Then I'll set its buttonmode property to true, so container.buttonmode = true,
07:20and then instead of putting the thumb loaders inside of the main timeline,
07:25I'll put them inside of the container.
07:26So where it says addChildthumbLoader inside of the loop, I'll just add container
07:32and a dot right before addChild.
07:33So, each of the thumbnails will be loaded into the sprite container, which
07:39supports hand cursor interactivity.
07:41I actually don't even have to change the addEventListener code.
07:45I can still have the listener on the thumbLoader, but because it's inside of a
07:49sprite that has the buttonmode property set to true, the hand cursor will still show.
07:55So, we'll test the movie again,
07:57roll over the different thumbnails, click on them,
08:01they load in, and you see the hand cursor.
08:04So now the slideshow is complete, all the code is organized, and the images load
08:10in when you click on thumbnails.
08:12So just remember that if you need to get a part of a string, you can break it up
08:21using the substring method.
08:22If you ever need to add mouse interactivity, specifically showing the hand
08:28cursor, you can wrap the elements inside of a sprite and set the
08:34buttonmode property to true.
Collapse this transcript
Using Flash Player 10 color management
00:00One of the features added in Flash Player 10 was the ability to manage color.
00:05That's an important feature, because color profiles previous to Flash Player 10
00:11would always get stripped out when loaded into Flash.
00:15Since color is so important to many people, especially photographers, it's best
00:20to manage color through Flash whenever you're loading images.
00:25All it takes is one line of code.
00:28Go to the first keyframe of the actions layer and open up the Actions panel.
00:32Above all the rest of the code, create a new line of code that says
00:37stage.colorCorrection, capital C for correction, space equals space
00:45ColorCorrection, capital Cs, dot ON in all caps and a semicolon.
00:53Now you test the movie, and Flash will apply any color profiles that are attached
01:00to the image that you're loading.
01:02So just remember, you can add this simple line of code to have Flash manage
01:07color, and without it, color profiles will be stripped out.
01:11Also remember that this is a Flash Player 10 only feature.
Collapse this transcript
8. Loading and Working with External SWF Files
Loading an external SWF
00:00Just like loading external image files, loading external SWF files can keep your
00:04timelines clean and their file sizes low. So on the stage,
00:09I have a button called play_mc.
00:12Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:17The first thing we'll do is add an EventListener to this button,
00:20play_mc.addEventListener, capital E, capital L, and in the parentheses, type
00:25MouseEvent.CLICK, make sure mouse is capital M and E, and CLICK is in all caps.
00:32So we'll create a function called playClicked.
00:44It's going to receive an event with the data type of MouseEvent:void for no return data.
00:51Then in here, we're going to load the SWF file.
00:54So we'll create a variable called loader, datatype it to a Loader with a
01:00capital L. Now this may look pretty familiar if you've loaded an image file before.
01:05We're setting it equal to new Loader, so we're creating a new instance of the
01:09Loader class, basically like an empty MovieClip.
01:11Then we add it to the stage.
01:17Then we can have the loader load the SFW file by using the load method, so
01:22loader.load, and in the parentheses create a new URLRequest. Make sure to write
01:30open and close parentheses for the URLRequest.
01:33In there, type some quotes.
01:34Now we'll load a file called assets/vid2.swf. That's it.
01:43Test the movie and click the Play button.
01:50So you'll notice that the movie loads in just fine and plays just like you'd expect.
01:55However, there is one little problem.
02:01Now you notice that some of the rock is kind of hanging over.
02:05You can solve that in multiple ways.
02:09The easiest way would be take that bar at the bottom and use ActionScript to put it
02:14in front of the loader once the file is loaded. So let's do that.
02:17I'm going to select the background and then Shift+Click the Play button.
02:23I'll press F8 on my keyboard.
02:25I'll create a new MovieClip called Front, capital F. Then click OK.
02:32Give it an instance name of front_mc and then go back to my code.
02:39In my code, I'll change the addEventListener line of code to be
02:45front_mc.play_mc.addEventListener.
02:52So we'll add the event listener to the play movie clip that's inside of the front movie clip.
02:56After we add the loader as a child, we'll add the front movie clip,
03:01so addChild(front_mc),
03:05and we'll bring that movie clip to the front.
03:07Now when I test the movie, I can click the Play button and preview the animation.
03:13Now as you're watching this animation, pay particular attention to the rocks at
03:17the bottom-left of the screen.
03:19Notice they aren't coming over the player anymore.
03:22They're right in the back where they should be.
03:25Now I'll just let this animation finish.
03:34So now I'll close Preview window.
03:37It's important to note that when you load a SWF file in Flash, only movies
03:41that have the same ActionScript version can communicate with each other.
03:46So if I loaded in an ActionScript 2 SWF file, the movie would play and the
03:51interactivity would work, but I couldn't communicate and send data between an
03:57ActionScript 3 movie and an ActionScript 2 movie.
04:00So just keep that in mind.
04:02Other than that, loading a SWF works just like loading an image.
04:06Simply use the Loader class and then load the image and then load the SWF file
04:12using the load method.
Collapse this transcript
Running ActionScript code in an external SWF from its parent
00:00Sometimes, you'll want to run code inside a child SWF file.
00:06So let's say I load this movie into the parent, and I want to issue some
00:11commands to objects inside of the child movie.
00:16For example, this file, AS_From_Parent, loads in the video two (vid2) SWF here.
00:21What if I wanted to communicate with objects inside there, and access that
00:26file's properties and methods? Let's take a look at how to do that.
00:30Again, this is only possible when both files are using the same version of ActionScript.
00:37It can be a different Flash player, but you're going to need all ActionScript 3 files.
00:42If you're going to use all ActionScript 2, you should watch the ActionScript 2
00:46Essential Training title to learn about that.
00:49So let's go to our code.
00:51I'm loading in vid2 when I click the Play button.
00:56So we'll test the movie, just to see what it does, click Play, and then
01:00the animation comes in.
01:04Let's take a look at accessing the properties and methods of that movie from
01:09within this parent movie.
01:12The first thing I'm going to do is add an EventListener to the loader, so that I
01:17can detect when the movie is fully loaded.
01:19You don't want to be messing with objects that are inside of a loaded file until
01:25the file is completely loaded.
01:28That makes sense because variables and functions may not exist yet while the
01:33file is loading, so you want to make sure that it's all done before you try to
01:37access any of that stuff.
01:38So right below addChild(front_mc), I'm going to write this line of code:
01:43loader.contentLoaderInfo, remember that you add EventListeners for loaders to
01:48the contentLoaderInfo property, not directly to the Loader itself, dot
01:52addEventListener and event's going to be Event.COMPLETE, all caps, and we'll run
01:58a function called swfLoaded, swf lower case, Loaded has a capital L. So I'll
02:06create the function below playClicked, swfLoaded.
02:19So now create the swfLoaded function.
02:21Note that it will receive an event with a data type of Event, with a capital E,
02:26colon void for no return data, and then inside of this function, we'll create a
02:31variable called mainTimeline, capital T. This will represent the main Timeline
02:38of the loaded movie, which is vid2.
02:43So I'm going to datatype this as a movie clip, because the main Timeline is
02:49essentially a movie clip, which is why you need to extend the MovieClip class
02:53when you create your own document class.
02:55I'm going to set this equal to loader.content.
03:00That's how we reference the content that's loaded inside of the loader.
03:05Now if you look at the code hinting, you'll see that the datatype of content is DisplayObject.
03:10Now that's important, because Flash is going to think that you're working
03:15with the DisplayObject.
03:16So you can modify properties that all display objects have, like X and Y, but
03:21if you modify properties that are unique to say a MovieClip, then you're going to get an error.
03:27So let's look at how to resolve that.
03:28So we'll type content.
03:31Then I'll wrap this inside of a movie clip.
03:35So right before loader.content, I'll type MovieClip, capital M, capital C and
03:42then wrap loader.content in parentheses.
03:47It's worthy of note that when I say we'll wrap this in a movie clip, in this
03:52instance when you type MovieClip in parentheses and wrap something in the
03:57parentheses, that doesn't convert that to a movie clip.
04:00That just is a message to Flash that says, treat this data as a movie clip.
04:05When you see the code that says new movie clip and addChild, then that's
04:11wrapping an object in a movie clip in a different way.
04:13That's actually placing it inside of a movie clip.
04:15This is just treating the data as a movie clip.
04:17It's called typecasting.
04:19That's what we're doing here.
04:20So we'll just end this line with a semi-colon and then we can go to the next
04:25line and we can access anything we want using our mainTimeline property.
04:29So I can type something like trace(mainTimeline.totalFrames);
04:38so I can see how many total frames are in the main timeline of the loaded SWF file.
04:42So test the movie, and click the Play button.
04:47Then you can see that there are 243 frames on the main timeline of the loaded SWF.
04:57So to run code inside of a loaded SWF file, simply use the loader's content property.
05:04Depending on the type of data that you're loading, you'll need to typecast the
05:08data as a movie clip or some other type of object.
05:12In almost every case though, you can use the MovieClip class, and
05:17everything will work.
05:19If you're running into trouble, try using the Object class, but the most
05:23important thing you need to remember is that the content property gives you
05:27access to all of your loaded content.
Collapse this transcript
Running parent code in a child SWF
00:00Another important skill when working with loaded SWF files is the ability to run
00:04a parent's code from within a child SWF.
00:07In addition, the code should not cause any errors if the child SWF is
00:11published without the parent.
00:13So, let's see what we have here.
00:15I'll test the movie, click the Play button, and load the child SWF animation.
00:19What we'll do is make it so when you click the Play button and the child SWF
00:25loads, will make the Play button on the parent SWF disappear.
00:30To access the Play button, you need to be familiar with the structure of
00:34the parent FLA file.
00:36On this stage, I have front_mc, and inside that movie clip, I have play_mc.
00:42So we'll need to know that path to get to play_mc.
00:47Let's go to vid2a.fla,
00:50select the first keyframe in the actions layer, and open up the Actions panel.
00:54Before we start writing the code that we're going to use, let's do a trace
00:58statement and type in 'parent' in the trace statement.
01:02I want to point on something very significant here.
01:06Test the movie, and you'll see that the parent of the child SWF is the Stage.
01:13Remember that there is only one Stage instance in every application, and all of
01:19the highest level objects are children of the stage.
01:22So now that this movie is published, I'll return to the parent FLA file
01:27and test the movie.
01:28When I click the Play button, I can see that the parent object is now a Loader.
01:37So remember, there is only one Stage in an application.
01:40So now the only Stage instance in this application is the parent of the main
01:46timeline of the parent SWF.
01:49So to access the main Timeline of the parent SWF, we'll need to go through
01:54that Loader instance.
01:59Let's go back to the child FLA, select the first keyframe in the actions layer
02:03and open the Actions panel.
02:06One way that we can make sure that this file is being loaded as an external SWF
02:10file is to use an If statement.
02:12So write a simple skeleton of an if statement.
02:14That's if in parentheses and curly braces.
02:18Inside the parentheses, check to see if parent is a Loader.
02:24So parent space is space Loader with a capital L.
02:28The Is statement is used to check a datatype.
02:33So if the datatype of parent is Loader, then run the code in the curly braces.
02:40So in here, we'll create a variable called mainTimeline, and it will represent
02:46the main Timeline of the parent SWF file.
02:48So type colon MovieClip, capital M, capital C, and we'll set that equal to
02:54parent, which refers to the Loader, dot parent, which refers to parent of the
03:00Loader, which is the main Timeline. Type a semicolon.
03:05We're going to run into a problem because, remember, parent is a display object.
03:12So I need to tell Flash to treat parent as a movie clip. Again, this is
03:17called typecasting.
03:18So right before the first parent, I'm going to type MovieClip, capital M,
03:23capital C, and wrap the two parents inside of the movie clip.
03:28Again, I'm not creating a new movie clip here.
03:31I'm simply telling Flash to treat this particular piece of data as a movie clip.
03:36So let's go to the next line, and then we'll type
03:41mainTimeline.front_mc.play_mc.visible = false.
03:54So once the child movie clip loads, this code will run and hide play_mc.
04:02So we should be able to test the movie now without getting any errors.
04:06Everything looks good.
04:08So let's go to the main FLA file and test the movie.
04:11When we click the Play button, the button should disappear. There it goes.
04:17So, we've successfully navigated from a child SWF and manipulated objects in a parent SWF.
04:30So remember, the parent property refers to the Stage when you're in the main
04:36Timeline of a SWF file, unless that SWF file is loaded into another SWF file.
04:42In that case, the parent property of the child SWF refers to the Loader that
04:48loaded the child SWF.
04:49So you can access the main Timeline by moving it backwards by using parent command.
04:57Once you get into the main Timeline, you can edit any of objects or run any code
05:03just as if you're writing that code in the parent's main Timeline.
Collapse this transcript
Creating a timeline-based preloader to load an external SWF file
00:00There are several different ways to create preloaders.
00:03One of the most common is to use a 100-frame Timeline animation and go to the
00:09frame that corresponds to the percent of the movie that is loaded.
00:12On the stage, I have a movie clip called prog_mc.
00:16If I double-click that movie clip, you can see its Timeline is a simple 100-
00:20frame animation that shows that blue bar going across.
00:26You may also notice that on the actions layer, I put a stop action on frame one.
00:30That's so that movie just doesn't play by itself.
00:34So let's go to Scene 1, and then I have play_mc -
00:37that's a Play button, and then I have a movie clip called mask_mc.
00:42This is actually going to work as an ActionScript mask, which I will show you
00:45how to apply in just a second.
00:47Click the first keyframe in the actions layer and open up the Actions panel.
00:52So I have some code here already, but it should look pretty familiar to you by this point.
00:56I am loading a file, and I have the Play button's buttonmode set to true, the
01:03file gets loaded whenever you click the Play button;
01:06pretty standard stuff.
01:08Here is the new line of code:
01:09loader.mask = mask_mc.
01:12That's all you need to do to apply an ActionScript mask.
01:16The funny thing is it's almost easier than applying a mask in the Timeline.
01:20Just use the Mask property, and set it to a movie clip.
01:24So now that we have walked through our code, let's look at adding the
01:29EventListener for loading the file.
01:32I will add the EventListener right under play_mc.addEventListener.
01:35So we are going to add an EventListener to track the loading file.
01:41So we want to see the percentage of the SWF that's loaded and show that inside of prog_mc.
01:47So type loader.contentLoaderInfo -
01:52remember to add EventListeners to contentLoaderInfo, not to loader itself -
01:57.addEventListener, and the event is going to be ProgressEvent, capital P,
02:01capital E, dot PROGRESS in all caps.
02:03Then we will run a function called trackProgress, capital P. Close up the
02:10parentheses, and define trackProgress right below playClicked.
02:14So function trackProgress, capital P, some parentheses.
02:19Inside of the parantheses type event: ProgressEvent, capital P and capital E. It should be blue.
02:28After the parentheses, colon, void and some curly braces.
02:34In the trackProgress function, create a variable called perLoaded, short
02:40for percent loaded.
02:42After perLoaded, type a colon and declare the datatype as a Number.
02:47Right after that, type space equals space, and then we are going to get the
02:52percent loaded by dividing the number of bytes that have loaded by the number of total bytes.
02:58We can get all that data from the Event property.
03:02So type event.bytesLoaded, divided by - which is a forward slash - event.bytesTotal.
03:11Then we need to multiply that value by 100.
03:18Wrap that whole expression in parentheses. Don't forget the semicolon at the end.
03:25And then right before the first parenthesis, type Math.round.
03:34Don't type an open parenthesis; we only need one.
03:38So now I have the percent loaded and this expression here, and it's held inside
03:43of the perLoaded variable.
03:44So now we'll just send the progress movie clip to the appropriate frame:
03:50prog_mc.gotoAnd with capital A, Stop with S and in parentheses, pass in
03:58perLoaded, capital L.
04:00So now, I will test the movie, and then I will click the Play button.
04:07You can see that blue bar is loading in the background.
04:11Now, it went really fast because it's not online.
04:15So if you want to mimic the loading, you can test the movie again with the SWF
04:19open, and that will launch the Simulate Download command, and you can click the
04:26Play button from there to see the progress a little more slowly.
04:29It looks like it's working just right.
04:37I will just let it finish, and then close the window.
04:46So there is our preloader.
04:48So remember that you can access all the preload information through the
04:53contentLoaderInfo property, and the event that dispatches every time
04:58LoadProgress is made is called ProgressEvent.PROGRESS.
Collapse this transcript
Displaying playback progress of a loaded SWF file
00:00When you load a SWF into Flash, you may want to track its playback progress like a video.
00:05You may have seen earlier in this course when we did that with a single SWF movie.
00:10The process is slightly different with external files, though.
00:14In addition, we're going to be using all ActionScript, instead of using the Timeline.
00:21So if I test the movie, I can click the Play button and load the SWF.
00:28The blue bar represents the amount that's loaded.
00:31So this little element on the left side will display the playback progress.
00:40Let's close the window, select the first keyframe of the actions layer, and open
00:46up the Actions panel.
00:48In your code, find the line of code that adds the EventListener to
00:52contentLoaderInfo, copy it, and paste it on the next line.
00:56We're going to add another listener to listen for the complete event.
01:00So change ProgressEvent.PROGRESS to Event, with a capital E, dot, COMPLETE -
01:07all caps. Change trackLoad to swfLoaded, with a capital L.
01:21Now, create a function called swfLoaded.
01:25swf are lowercase, Loaded has a capital L. In the parentheses, type event,
01:32colon, Event, with a capital E. Close out the parentheses and then
01:36afterwards, type colon, void - all lowercase.
01:41After that, type some curly braces.
01:45Inside the curly braces, we're going to add another EventListener.
01:48So type addEvent, capital E, Listener with a capital L, and this time we're going
01:56to listen for the ENTER_FRAME event, because we want to track the load progress
02:02every frame of the loaded SWF.
02:04We don't want to start tracking progress until the SWF starts playing.
02:09So here we'll type Event, with a capital E, dot ENTER_FRAME in all caps and a comma
02:16and a space, and then we'll type trackPlayback. Below the swfLoaded function,
02:23create the trackPlayback function:
02:26function trackPlayback with a capital P. In the parentheses, type event
02:35lowercase, colon, Event with a capital E, colon, void for no return data and
02:42then some curly braces.
02:45Now we want to get the percent of the movie that has been played.
02:49We can get that information by typing var, space, per and then Played with a
02:57capital P. So we're holding this in a variable. Datatype it to a number and then
03:03set the value at loader.content.currentFrame.
03:11Now we're going to need to wrap loader.content in parentheses, and write
03:16MovieClip before it.
03:17You want to make sure that Flash treats that as a movie clip so we don't get any errors:
03:22so MovieClip with capital M and a capital C.
03:25Now I'm going to select that little statement,
03:29the MovieClip, loader.content, and currentFrame.
03:31I'm going to copy with Command+C or Ctrl+C and then after currentFrame, I'll
03:36type a space, and forward slash and space, and then paste the code and change
03:42currentFrame to totalFrames.
03:44That's how we get the percentage.
03:46We simply divide the currentFrame by the total number of frames, and we'll get a
03:51number between 0 and 1 that will represent a percentage.
03:56This time we're not going to round the number.
03:58So go to the next line, and we're going to specify where the Progress slider is going to be.
04:08That's the little element on the left side of the Progress bar.
04:14So type prog_mc.drag.x, space, equals, space, and then we have to determine its position.
04:25Now, in order to get its position, all we have to do is multiply the
04:30percentage that we already have in the perPlayed variable by the maximum right
04:39position of the object.
04:42Now we can get the maximum right position by starting with the width of prog_mc.
04:48So type prog_mc, and then dot width.
04:53I'll close the Actions panel, and let's take a look at this visually.
04:58So on the stage, I have prog_mc.
05:01Let's enter its Timeline.
05:03So if we were to move the drag movie clip to the amount of prog_mc.width,
05:10its left edge would align with the right edge of the Progress bar, and
05:17that's too far to the right.
05:18So we need to shift it back to the left in exactly the amount of the width
05:23of the drag object.
05:25So I can shift it right here.
05:29That's what we want it.
05:31So let's go back to our code and write that.
05:34Go back to Scene 1, select frame 1 in the actions layer and open up the Actions panel.
05:40So we'll offset that by negative prog_mc.drag.width So we'll wrap the
05:49subtraction in parentheses and multiply that value by the percentage played,
05:56which is per, and then Played, with a capital P.
06:00So when the currentFrame is the same as the totalFrames, then perPlayed will
06:06equal 1, which means drag element will be at prog_mc.width minus the width of
06:17the drag, times one, or just minus the width of the drag.
06:22So it will be in exactly the right spot at the end, and it will start out at 0.
06:28So let's test the movie and see what we've got.
06:31Click the Play button, and watch the progress.
06:34It looks like it's working perfectly.
06:50Just let it finish. Then I'll close the playback window.
06:54So notice as the movie loops around then the playback progress loops around as well,
07:00so we don't even have to recode that.
07:02So to track playback progress, reference the main timeline by getting to
07:08loader.content, and then you can access the currentFrame and totalFrames
07:13properties, and you divide currentFrame by totalFrames to get the
07:17percentage played.
Collapse this transcript
9. Working with Text
Creating plain text files
00:00Throughout this chapter we are going to be working a lot with plain text files.
00:04A plain text file is a file that does not have any formatting applied to it, and
00:10has the .txt extension.
00:14Let's look at how to create a plain text file.
00:16Of course, I can't show how to do this in every application, on every operating
00:20system, but I am just going to show you the basic rules for creating a plain
00:25text file, and you should be able to apply these same concepts in whatever
00:29application you use to create your files.
00:32If you are working on a Mac, I recommend using the built-in
00:34application TextEdit.
00:36If you are working on a PC, use the built-in application Notepad.
00:41These applications can definitely create plain text.
00:44One example of an application you absolutely do not want to use to create your
00:48plain text files is Microsoft Word.
00:51So you want to use a barebones text editor, like TextEdit or Notepad.
00:56In your text editor, you should have a Preferences area.
01:01TextEdit has Preferences under TextEdit.
01:04So I click Preferences, and then you can explain that whenever you create a new
01:09document - so click the New Document tab -
01:12that you want to use Plain text, which is different from Rich text.
01:16So I'll click Plain Text and then close the Preferences window.
01:20Now what I am going to do is create a new document.
01:23So I will close this one and then go to File > New.
01:28Notice that I have no controls for styling text.
01:31I can type, and it's plain, un-styled text, and I have no options to style the text.
01:41When you are ready to save the file, just go to File > Save, and make sure you
01:46end the file name with .txt.
01:47I am just going to cancel out of that for now.
01:51So to create a plain text file, use a barebones text editor, like TextEdit or Notepad,
02:00make sure you are working in plain text mode, not rich text mode, and save the
02:04file with the .txt extension.
Collapse this transcript
Loading text from an external text file
00:00Just like loading any other external file into Flash, using external text files
00:05gives you the advantage of updating text in an app without having to republish a SWF.
00:11On the stage, I have a text field.
00:15I've given it an instance name of info_txt.
00:18Note that I'm using Classic Text instead of TLF Text.
00:25Most the time, I like to use Classic Text because it doesn't require an
00:29additional file to be bundled with my SWF when I publish my application.
00:34I typically only use TLF Text where I want to use specific features that I can
00:39only access to that text engine.
00:42So I'm using a Dynamic Text field, and then I'll scroll down, and then I've have
00:48added a drop shadow to the text.
00:49Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:56At the top of my code, I'm going to create a few new lines. In here, I'll create
01:01a variable called textLoader, with a capital L. Then I'll datatype it to an URLLoader.
01:11The URLLoader class is used to load external data.
01:15This is more for files like text files, whereas the Loader class is used to load graphics.
01:22So we'll type space equals space and then the standard new space, URLLoader, URL
01:29is all capital, and Loader has a capital L, some parenthesis and a semicolon. = new URLLoader();
01:33So we've created that object.
01:39In ActionScript, object and instance are synonymous, so back_btn would be an
01:46instance and also an object. textLoader is an instance of an URLLoader class,
01:53just like back_btn is an instance of the simple Button Class or the MovieClip
01:58class, for example.
01:59So let's go to the next line, and then what we want to do is add an
02:06EventListener to textLoader.
02:07So type textLoader.addEventListener, and when you want to listen for when the
02:16file has done loading, it's Event.COMPLETE,
02:20just like with the Loader class.
02:21So Event, capital E .COMPLETE, in all caps.
02:26Notice that I'm not adding an EventListener to a specific property of the textLoader;
02:31I'm just adding it directly to textLoader.
02:34So this is a little bit different from when you add the EventListener to the Loader class.
02:38You add it directly to the URLLoader.
02:41So after Event.COMPLETE, type comma and a space, and then type textLoaded.
02:49So, remember that's different from textLoader, which is the object. textLoaded
02:54is the event handler name.
02:56So, close out the parenthesis and then scroll all the way down to the bottom of your code.
03:06At the bottom of your code, create a function. Call it textLoaded. And in the
03:14parenthesis, I'm going to evt, short for event, colon and then Event, with a
03:22capital E. After the parentheses, I'll type a colon and then void, for no return
03:30data, and some curly braces.
03:34Once the text is loaded, we're going to put the text inside of that text field,
03:40which if you remember, is info_txt.
03:44If you ever want to double-check that, you can click the target path icon at the
03:49top of the Actions panel, and you can review the instance names of objects in
03:53your code. So, I have info_txt.
03:57Names in parentheses are objects that don't have instance names.
04:01I'll just click Cancel there.
04:03So to set the text of a text field, you use the text field's text property.
04:10So type info_txt.text space equals space, and then we can access the text data
04:21from the URLloader object, which we named textLoader through its data property
04:29So we'll set info_txt.text equal to textLoader, capital L, dot data.
04:36Now, the last thing that we have to do is actually load the text file.
04:42We'll load that right below the addEventListener line of code.
04:50Always remember to load files after you add the EventListener, just in case the
04:55file gets loaded before the EventListener is added, because in that situation
05:00the event handler, which in this case is textLoaded, would never be triggered.
05:04So always load the file after adding the EventListener.
05:07So textLoader.load, and Flash is asking for a URLRequest.
05:15So in the parentheses, I'm going to type, new, space, URL, all caps, Request
05:22with a capital R, and then some parentheses.
05:25Then I'm going to type the string that will represent the path to the file
05:31that we want to load.
05:33In the quotes, I'll type the path of the file.
05:36The file is in the assets folders, so I'll type assets first, forward slash, bear_01.txt.
05:48So let's test the movie and preview the application.
05:51There is the text field loaded into Flash.
06:01So whenever you want to load text data into Flash, you can use the URLLoader class.
06:09Make sure to add the EventListener directly to your URLloader object before you load the file.
06:17Once the file is loaded, you can place that data wherever you like for the
06:22URLloader object's data property.
Collapse this transcript
Loading multiple text files
00:00Currently, our application loads one text file, which corresponds to one image in the slideshow.
00:07I'll test the movie to show an example of that.
00:11So I have my slideshow here, and it starts out with, "If you leave your food
00:14at night while you're camping," and I can click through the slides, but the
00:18text never changes.
00:20Let's say we wanted to load one bit of text for each slide. And one thing we
00:27could do is copy the code that loads the single text file on each frame of the application,
00:34but then if you wanted to make a change, you'd have to change the code in each
00:39frame, which wouldn't be insanely tedious in this six-frame application, but you
00:44can imagine if you had a hundred frames.
00:48Let's take a look at how to make a reusable function that can apply for each
00:52frame of the application.
00:55Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:59In the Action panel, scroll to the top of your code and look at the string
01:08that's loading a text file.
01:12The path of the file is assets/bear_01.txt.
01:20The files that I've created for this application are bear_01 through bear_06,
01:28so the only thing that's changing is that single digit.
01:32So the goal is to make something that can simply swap out that digit in each
01:38frame. The wonderful thing for this example is that the numbers perfectly
01:44correspond to the frames.
01:46So there are six images that are in a sequence and then six text files that are
01:52in a sequence as well.
01:55So if we use the current frame number, that should work perfectly.
01:59So right below this, I'm going to run a function that we haven't created yet
02:04called loadText, with a capital T. Then I'll define that function right below
02:13textLoaded, all the way at the bottom of my code.
02:17So function, loadText with a capital T, some parentheses, colon void and some curly braces.
02:25Standard skeleton of function.
02:28In here, I'm going to create a variable called fileName, and that's going to be
02:36a string, and here what I'm going to do is I'll set it equal to something that
02:43I've already written.
02:44So let's scroll up and select that line of code that loads the file,
02:50so that's line 7 in my code, textLoader.load.
02:52I'm going to cut that code, I'll scroll all the way down to the bottom, and then
02:59I'll paste that code right below var Filename:String, and then what
03:06I'll do is I'll take that string, including the quotes, cut it with Command+X or
03:14Ctrl+X and then paste it after the Equal sign.
03:18So I'm setting the file name equal to the full path of the file.
03:24Then inside of the URLRequest, on the next line of code, I'm going to use
03:29fileName instead of writing the full path of the file.
03:36So essentially, this should do the exact same thing as it did before, except that
03:40the loading code is now inside of a function.
03:44So test the movie, and it should work just fine.
03:47Okay, I'll close the Preview window, and now we'll look at making the simple
03:53changes that will make this function apply to every frame of the slideshow.
04:00What I'm going to do is at the bottom of each of the event handlers, for the
04:07mouse clicks - so forwardClicked and backClicked - I'm going to run loadText.
04:14So after the frame is manipulated, I'm going to run loadText to load a file in.
04:20So I'll do that at the very bottom of backClicked, so I'll type loadText with
04:23a capital T, and then scroll down to the bottom of forwardClicked and do the same thing.
04:30Right before that final close curly brace for that function, I'll type loadText
04:35capital T, some parentheses and semi colon, and then I'll scroll all the way
04:39down to the loadText function, where it's defined.
04:44Remember that the only thing we had to change each time is the number of the text file.
04:51So there's bear_01, and there is bear_02, and 03, et cetera.
04:57So we just need to change that single digit.
04:59So I'm going to break this string up, by typing two quotes.
05:05Now I'll type some spaces, and I'll put a space in between the ending of the
05:10first string, type a Plus sign, and then some extra space and another Plus sign.
05:19So as this reads now, it's just going to add-on bear_02.txt.
05:26So if we put the correct number inside the Plus signs, and that'll get inserted,
05:32and the right file will be loaded.
05:35As I said earlier, the correct number simply corresponds to the current frame.
05:41So if I just type currentFrame, then it should work perfectly.
05:46So let's test the movie and click the Next buttons and the Previous button,
05:53click the Next button and the Previous button, and you can see all the text files loading.
06:05So again, we're simply taking the string that ends with bear_0, and each time the
06:10frame is updated we're running loadText, we are setting that number, or the file to
06:16load, equal to the current frame.
06:19So bear_01, bear_02 depending on the frame.
06:24And at the end we tack on ".txt",
06:27and the appropriate file loads.
06:30So by adding a reusable function, like loadText here, you can use the same
06:35code to loadText files for as many frames as you want, as long as you name the
06:41text files accordingly.
Collapse this transcript
Rendering simple HTML in a text field
00:00Text fields support simple HTML code.
00:04If I test the movie, you'll see that I have some HTML code that links to
00:09explorecalifornia.org in my text field.
00:13This is a standard HTML link.
00:16If you're curious about HTML code, you can watch Dreamweaver Essential Training.
00:23Close the window, go to the first keyframe of the actions layer, and open the Actions panel.
00:28Note that I'm loading a file called bear_html.txt.
00:34That file has the HTML code inside of it.
00:37So, if you want to have Flash render simple HTML code, you can scroll down and
00:45change text in info_txt.text inside of the textLoaded function, to html all
00:53lowercased and then Text with a capital T.
00:56That's it, test the movie, and then explorecalifornia.org will be a link.
01:02So you can click that, and the web site will open.
01:09Now, Flash doesn't support all HTML, just parts of it.
01:14If you want to find more information about what specific HTML tags are
01:17supported, you can look htmlText up in Flash Help by highlighting it and
01:23pressing F1 on your keyboard.
01:27Let's take this to the next level.
01:29Let's say that when you roll over the link, you want an underline to appear.
01:34To do that, you can apply a CSS style.
01:38This works pretty close to how it works in HTML, except for we're going to use
01:43CSS using ActionScript only.
01:47So, scroll to the top of the code and above the var textLoader line of code,
01:54create a new line of code and type var space css.
02:00We'll datatype that to a StyleSheet, capital Ss.
02:06I'll set that equal to a new instance of the StyleSheet class, so some
02:12parentheses and a semicolon. So, I'm creating in the new StyleSheet, and then I'm
02:16going to apply a style to it.
02:18So go to the next line and type css.set, capital S for Style, and in the
02:26parentheses, Flash is asking for two things:
02:29styleName and styleObject.
02:32The name is like what the CSS rule would be for. So a:
02:37hover would be for a link when you hover over it.
02:40So that's a string, so I'll type a string here, two quotes, and then inside of the quotes,
02:45I'll type "a:hover."
02:48Now this is standard CSS for styling a link.
02:52Again, I'll refer you to Dreamweaver Essential Training for more
02:55information about that.
02:57After the quotes, type a comma and a space, and then we're going to create
03:02a styleObject. And this is where we specify that there is going to be an underline.
03:11To create the styleObject, type curly braces, so open and closed curly braces,
03:17and make sure to close up the parentheses and type a semicolon.
03:22Now put your cursor inside of the curly braces.
03:26These curly braces are shorthand notation for creating an ActionScript object.
03:32Inside of the curly braces, type textDecoration with a capital D;
03:38this is all one word.
03:39So textDecoration. This is not going to turn blue this time, but it is something
03:44that Flash already knows.
03:46Then type a colon, add some quotes, and in the quotes, type underline, all lowercase.
03:56So here is how you set a CSS style.
04:00If you wanted to set additional styles, you can highlight the setStyle method
04:07and then press F1 on your keyboard to read about the supported styles, and
04:11then you can add them by typing a comma after the quotes and underline and
04:16adding the additional styles.
04:18For now, we're just going to leave it at textDecoration underline,
04:21so this is only going to apply when you roll over the link, or in other words
04:25when you hover over the link.
04:27Scroll all the way to the bottom of your code, and now we'll need to connect the
04:32text field to the CSS style.
04:34So right below this htmlText line of code, we'll create a new line.
04:41So on the next line, type info_txt.styleSheet,
04:49capital S for Sheet, space, equals, space, css, and then a semicolon. That's it.
04:57Test the movie, and now when you roll over explorecalifornia.org, you'll
05:03see that underline.
05:05You can also click it to see if the link still works.
05:08Now, I'll close the Preview window.
05:10So you can have Flash render simple HTML code by changing the Text property to htmlText.
05:17Remember that Flash doesn't support all HTML, just a few specific tags.
05:23Then you can create an ActionScript StyleSheet by using the new
05:28StyleSheet command.
05:30Then you use the setStyle method to set a style.
05:34You specify the styleName as a string and then the specifics of the style inside
05:41of an ActionScript Object, which you can create like this.
05:45Finally, you set the StyleSheet property of the text field to the
05:49StyleSheet that you created.
Collapse this transcript
Creating a scroll bar for a text field
00:00You can always use a simple built- in scroll bar to scroll a text field.
00:04But using some standard math, you can create your own custom scrollbar with
00:09any graphics you want.
00:10So I'll scroll up and on the Stage I have a movie clip called drag, and then
00:16another movie clip called bar.
00:18So those are all lower case.
00:20Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:24Here we will use code to make those graphics become a scrollbar to scroll
00:30our text field.
00:31Scroll to the top of your code and right above the stop action, it's on 11 in my
00:36code, create a new lines and type the following.
00:40drag.addEventListener, capital E, capital L, and in parentheses the event's
00:46going to be MouseEvent.MOUSE_DOWN Remember MOUSE_DOWN is in all caps,
00:51separated with an underscore.
00:54And then we'll run a function called dragMouseDown.
00:55We'll scroll to the bottom of your code and below everything else I'll
01:02create that functions.
01:03So function and dragMouseDown and in the parentheses we'll type
01:09EBT:MouseEvent:void after the parentheses for no return data and inside of the
01:15curly braces, I will tell Flash to drag the drag movie clip.
01:21So drag, which is the name of the movie clip, .startDrag with a capital D. Type
01:26some parentheses and a semicolon and test the movie.
01:29So just click and drag and you can drag that movie clip.
01:34Notice that when you let your mouse go that it still drags.
01:38So we'll need to tell it to stop dragging.
01:40So go to the next line.
01:41And we'll type stage.addEventListener Event's going to be a MouseEvent.MOUSE_UP
01:49and we'll run a function called dragMouseUp, capital M, capital U. Now the
01:54reason I'm adding the event listener to the Stage and not to drag, is because if
01:59the person viewing the application moves their mouse really fast and lets go in
02:04a different spot and doesn't happen to be on drag, then we will miss that event
02:09if we attach the event to drag.
02:11So we attach it to the Stage to capture the event wherever it happens.
02:15So I'll copy this block of code, and then paste it a few lines down, and change
02:21dragMouseDown to dragMouseUp and drag. StartDrag to drag.StopDrag then I'll
02:28change stage.addEventListener to stage.removeEventListener.
02:32So I will test the movie again.
02:35Now we can drag-and-drop the drag movie clip.
02:39Now the next step is to constrain the movement of the drag movie clip.
02:44You can do this using a rectangle.
02:47Basically you give Flash a bounding box for the draggable movie clip, and it's
02:52in the shape of a rectangle.
02:53So scroll up to the top of your code and we will define that rectangle.
02:57I'll do that right above where I defined
03:00drag.addEventListener(MouseEvent.MOUSE_DOWN.
03:03So I create a variable and we will call this dragRect, data type it to
03:08a Rectangle.
03:09We will set equal to a new rectangle and in the parantheses the parameters go
03:16to X,Y, width, height.
03:19So the starting position of the rectangle that we want to be able to drag is
03:23wherever the drag is currently, which is at the top.
03:26So if you close the Actions panel, take a look at the drag movie clip, we want
03:30that origin of the rectangular area to be at the same origin as drag.
03:35So that should be nice and easy.
03:37Go back to the code and then type drag.x, drag.y. And then the width.
03:44The width is easy because we don't want to move left or right when we have a
03:48scroll bar that's vertical.
03:49So we just type in zero and in for the height we want it to go all the way into
03:54the bottom of the bar.
03:56So a good starting point is the height of the bar.
04:00So we'll type bar.height.
04:01Now before we test the movie I want to apply this dragged rectangle to the drag.
04:06So scrolling to the bottom of your code and find the startDrag line of code.
04:12On mine it's 51 and it's inside of the dragMouseDown function.
04:16Inside of startDrag we're going to pass in two parameters.
04:20The first is whether to lock the center of the draggable object.
04:24Now, here, we want to type false.
04:26If we were to put true, then it the top- left of the drag movie clip would lock
04:32to the mouse point, which would be really weird looking.
04:34So let's just say false and then a comma, and then dragRectangle.
04:40Test the movie.
04:42And now drag the scrollbar.
04:44Notice you can't drag it left or right.
04:46And if you try to scroll it all the way to the bottom, it stops at the point
04:49where the top of drag is aligned at the bottom of bar.
04:53We don't want to it to be like that.
04:54We want it to be so the bottom of bar is aligned at the bottom of drag.
04:58So we need to offset that by the height of the drag movie clip, shifting it up
05:04to the appropriate position.
05:05So to do that, scroll to the top of your code and after bar.height on the line
05:11were you create the new rectangle, subtract drag.height.
05:14Now test the movie.
05:15The scrollbar should work just right.
05:20I'll close the preview window.
05:22So all you need to do to create a draaggable scroll bar is used to startDrag
05:26command and pass in the constraining rectangle.
Collapse this transcript
Scrolling a text field
00:00If you have a custom scrollbar, the next step is to use it to scroll a text field.
00:05On the stage you'll see I have text field hat's called info_txt.
00:11Go to the first keyframe of the Actions layer and open up the Actions panel.
00:16Scroll all the way to the bottom of your code and right below
00:20stage.addEventListener and drag MOUSE_UP we'll add a ENTER_FRAME event listener.
00:26So addEventListener.
00:30And the event's going to be Event.ENTER_FRAME in all caps.
00:37We will run a function called scrollText.
00:43Once we stop dragging the scrollbar, then we'll remove that event listener.
00:47So I'll just copy and paste this code at the bottom of dragMouseUp and change
00:51addEventListener to removeEventListener.
00:55Now right below dragMouseUp let's define the scrollText function.
00:59So make sure it receives an event object with the datatype of the Event, :void
01:06
01:06for no return data, and some curly braces.
01:09To scroll the text field you need to do two things.
01:13To show the first I'll test the movie.
01:16When you drag the scrollbar you need to calculate the percentage the scrollbar
01:22has moved down and using that information you'll do the second thing, which is
01:28to scroll the actual text field.
01:30So again first step is to define the percentage that scrolled.
01:34So create a variable called percentScrolled with a capital S. We'll data type it
01:40to a number, and this is going to be a value between zero and one.
01:45So a decimal value.
01:47Now to find that, we have to divide the current position of the scrollbar by the
01:52total height that the scrollbar could be dragged.
01:55So the current position of the scrollbar is actually wherever drag.Y is.
02:01However we need to offset this by the position of the bar.
02:07So we'll subtract bar.Y. We'll put that in parentheses.
02:16If we didn't subtract bar.Y, the the percent scroll would be based on the top
02:21of the stage.
02:22We have to shift it down to the position of the scrollbar.
02:26That's why we're subtracting that.
02:28So we'll divide that value by another value in parentheses.
02:32We want to get the total height of the scrollbar to be dragged.
02:36So the maximum value.
02:39Now we are to set this actually at the top of our code in our rectangle.
02:44The height of the scrollable area is bar.height minus drag.height.
02:49So you can highlight and copy that if you want.
02:51Or you can type it all over.
02:53It doesn't matter.
02:54And put that in the parentheses after the forward slash and that's how we get
02:58the percent scrolled.
03:00Now if you just want to test to make sure you are getting the right values
03:04you can trace percentScrolled and just confirm that you're getting a value
03:08between zero and one.
03:10So you should get zero at the top and then one all the way the bottom.
03:14Or something really close to that.
03:17Because sometimes the pixels don't line up perfectly.
03:20So I'll click and drag the scroll bar.
03:21At the top I get zero and at the bottom, I get pretty close to one.
03:25I get .992 and that's good enough.
03:29So let's close the preview window and erase the trace statement.
03:33So we have the percentage that's been scrolled.
03:36Now we just need to apply it to the text field.
03:39So to do that we will reference the text field, which is in info_txt and it
03:44has a property called scrollV and that represents the vertical scroll, as you
03:51may have guessed.
03:52So we'll set that equal to the number and the number is the line number.
03:57So to find out which number we should go to, we're going to use math.round.
04:04We need to have a whole number, an integer, and in parentheses we'll take
04:08percentScrolled and multiply that by the maximum and scrolling value, which can
04:16be accessed through info_txt.maxScrollV.
04:21So we take that percentage and we multiply it by the maximum value and when we
04:30round that number, it will give us the line that we need to scroll to.
04:36So test the movie.
04:39And then click and drag the drag movie clip to watch the text field scroll.
04:48And that's it.
04:50So you can contact a scrollbar to a text field by finding the percentage that
04:54was scrolled and applying that to the scrollV property.
Collapse this transcript
Scrolling movie clips and other objects using masks
00:00Scrolling objects other than text fields is possible too.
00:04If you scroll other objects you can add special effects to your scrolling that
00:07you couldn't add otherwise, like easing and smoother animation.
00:11To see example of why you would want that, test the movie.
00:16As you stroll the text field, notice that the text field that doesn't
00:21scroll very smoothly.
00:23It scrolls one line at a time.
00:26Using the text field scrollV property, that's the best you're going to get.
00:30So you want to do is embed the text field into a movie clip and scroll the movie
00:38clip so that we get completely smooth animation.
00:42So I'll select the text field and my goal now is to makes sure that the size of
00:47text field is big enough to accommodate all the text without scrolling.
00:52So I'll click and drag the text field size down to about here and test the movie
00:58to make sure it can accommodate all the text.
01:00And it looks good.
01:01So I'll close the preview window.
01:04Add the next step is to embed the text field into a movie clip.
01:11So with the text field selected, I'm going to press F8 on my keyboard convert
01:17it to a movie clip.
01:18I'll call the movie clip Container with a capital C and give it an instance name
01:24of container with a lowercase C. So symbol, capital C;
01:30instance, lowercase C. So now I have container here.
01:35And if I test the movie, you'll see that I get some errors and that's just
01:40because I need to tell Flash where info_txt is.
01:45So if I double-click the errors Flash will take me to the right line and then
01:49instead of info_txt, I'm going to type container.info_txt.
01:56And I'll change that everywhere in my code.
01:59Except for the scrollV line of code, which I'm going to comment out for now.
02:06So that should do it.
02:07Now I should be able to test the movie without any errors.
02:10And that's good.
02:13Now what we want to do is scroll the movie clip instead of the text field.
02:18Scroll down to the bottom of your code, and what were going to do is change the
02:23code that says info_txt.scrollV.
02:26So obviously you're going to have to uncomment it as well.
02:28We'll change that to container.Y. And so we're setting the Y position of the
02:35container instead of messing with the scroll of the text field.
02:40So I'm going to erase the part that says math.round.
02:45So we have container.Y and then an equal sign and then we will
02:50type percentScrolled.
02:53Simply because we know we're going to need that percentage in calculating where
02:57to place the container.
02:58Now the next step is to figure out what the maximum scroll spot is.
03:05So if you shut the Actions panel and you select the container on the stage, the
03:11thing that we want is for the bottom of the container to be aligned with the
03:16bottom of the scrollbar.
03:17So it's about right here.
03:19So how do we get that number, other than just writing down the Y position that
03:24we see in the Properties panel?
03:27First Command+Z to undo that.
03:29So what we do is we take the height of the bar, which is the height of the
03:35visible area that we want, and we subtract the height of the container.
03:41And that's going to put the container at about here.
03:45And we just need to offset that by the position of the bar which will push it
03:50down just to the right spot.
03:52So I'm going to undo that change and we'll go back to our code and see what
03:56that looks like.
03:58So we'll take percentScrolled . After percentScrolled type an asterisk and
04:03then in parentheses, type bar.height minus container.height and after the
04:11parentheses, We're going to add on bar.Y. So this will work for our
04:18container's position.
04:19Now test the movie.
04:22Click and drag the scrollbar, and it looks like everything is just right.
04:29Except for one thing.
04:30We can still see the whole text field all the time.
04:33So what we have to do is mask it Close the preview window, close the Actions
04:40panel, and we will create an mask right above the text field.
04:44So I'll create a new layer and I'll call it mask.
04:49And I'm going to just draw a solid rectangle I like to use green for my masks
04:57in this movie.
04:58It's the color that's not being used.
05:00So I'll want to make sure that if it's showing it's really obvious.
05:04So I'm going to click and drag.
05:09Approximately the area of that shadow and then I'll release my mouse button
05:15and then what I'm going to do is just right-click the mask lauer and then
05:19click Mask.
05:21And that's it. And now I test the movie.
05:24And the scroll bar should work just right.
05:27So now we have smooth scrolling text that's based on pixels instead of lines
05:33in a text field.
05:35So to scroll any object you want, simply set the Y position of a container movie clip.
Collapse this transcript
Modifying TLF text properties through ActionScript
00:00The Text Layout Framework is new to Flash Pro, and using simple ActionScript,
00:05you can apply complex changes to TLF text fields.
00:09If you see a warning message telling you that you can't stream your content
00:12when you're using a TLF text field, you can adjust that in Advanced
00:16ActionScript settings.
00:17So on the stage here, I have a text field called info_txt, and I'm using TLF text.
00:24So I'll go to the first keyframe in the actions layer and open the Actions
00:28panel, and we'll look at some unique Text Layout Framework properties that you
00:33can manipulate using ActionScript.
00:35For the most part, you don't really have to change anything in your code when
00:40you're working with the Text Layout Framework text field.
00:42This code I have here is code that I wrote when I worked with the dynamic text
00:46field using the Classic Text engine.
00:48If I test the movie, you can see that it still works.
00:55So just make a note that the Text Layout Framework is essentially the same,
00:59except it adds a few new properties.
01:01To look at the properties, create a variable called info, and then capital TLF.
01:09Datatype it as a TLFTextField, TLF is all caps, and we'll set that equal to info_txt.
01:19Now when we refer to the text field, we'll use infoTLF.
01:22So scroll to the bottom of your code, and at the bottom of the textLoaded
01:27function, create a new line and then type infoTLF., and here you can see the
01:37unique properties for a TLFTextField.
01:39So if you want to look at all the different properties and methods you can work
01:43with with the Text Layout Framework, simply scroll through this menu.
01:47Some things that are not available on the standard text field are things
01:51like padding and margin.
01:57So for example, I have paddingBottom, paddingLeft, paddingRight, paddingTop.
02:02So let's use paddingLeft.
02:04Just double-click that, and I'll set the value equal to 10, for 10 pixels on
02:13the left side.
02:14I'll test the movie, and you can see that the text is brought in a little bit.
02:22If I increase that value to say 50 pixels, it's a little bit more obvious.
02:30So you can see I can add padding on the different edges of the text field.
02:35Of course, this is just one property that you can modify on a Text Layout
02:39Framework text field.
02:41For more information about working with the Text Layout Framework in
02:44ActionScript, you can highlight TLFTextField, where it's all blue, and then
02:50press F1 on your keyboard.
Collapse this transcript
10. Using XML with Data-Driven Components
Reviewing XML and E4X syntax
00:00XML provides an easy way to organize data for use in Flash.
00:05ActionScript 3.0 can read and write XML natively, using something called E4X syntax.
00:12Let's take a look at an XML file.
00:14If you're unfamiliar with XML, XML data is organized like the data I have on my
00:19screen and is saved in a plain text file, with the extension XML.
00:26XML stands for Extensible Markup Language.
00:29It's pretty similar to HTML, except for with XML you are allowed to create your
00:34own tags, and so I can name anything however I want.
00:38So let's take a look at this code, and I'll walk through how XML works.
00:43In XML, there are elements and attributes.
00:47This is just like HTML.
00:49So you'll see things like this images tag here.
00:52That would be considered an element.
00:55Inside of the images tag are individual image elements.
00:59So I have one representing each picture here.
01:03After all the single image tags, I have the closing tag for the main images element.
01:09This outer element is also known as the root element in XML.
01:15Elements inside of other elements are known as child elements.
01:18So image is a child of the images element.
01:23Attributes are values that are placed inside the element tag.
01:27So we have the image tag, and then it says file, equals, and then in
01:31quotes, alcatraz.jpg.
01:34Attributes are all given values using quotes, whether they are strings or
01:38other types of data.
01:39For example, I can add another attribute here called width by typing a space
01:44after the close quote, typing the width, an equals sign, and some quotes, and
01:49then I could type the width of the image.
01:51So this is the syntax for working with XML code.
01:55So using XML data, I can organize a large amount of data without having to put
02:00this code inside of my FLA that has an array, for example.
02:03That way, I can make changes outside of Flash.
02:06So let's select all this code and see how it's supported natively in Flash.
02:11Copy the code with Command+C or Ctrl+C and then tab over to Flash, select the
02:15first keyframe of the actions layer and open up the Actions panel.
02:19If you don't have access to the exercise files, you can use any file you like
02:23for this example, even a blank file.
02:25Paste in the code, and here we can actually hold the XML data, written just like
02:30this, in a Flash variable.
02:32So right before the less than sign for images at the very top of my code, I am
02:36going to type var and space.
02:38I'm going to call this images, then capital XML, datatype it to XML, and type
02:44space equals space, and that's it.
02:46This data is understood by Flash as is.
02:52So now if I do a simple trace statement, like trace(imagesXML), I can see that
02:57data in the Output window without getting in there.
03:00So I can bring XML data right into Flash without having to change it at all.
03:05This native reading of XML data is known as E4X, which is short for
03:11ECMAScript for XML.
03:14ActionScript is what's called an ECMAScript language, and that's why it works
03:19with ActionScript 3.0.
03:20So again, XML allows you to organize data for your apps and can be manipulated
03:26in ActionScript as native code.
Collapse this transcript
Loading an XML file
00:00Loading files into Flash allows for smaller SWF file sizes and easier
00:04adjustments to your applications.
00:06XML is no exception.
00:07Let's take a look at how to load an external XML file into Flash.
00:11Select the first keyframe of the actions layer and open up the Actions panel.
00:15If you've loaded in a text file, then an XML file is exactly the same process.
00:20So we'll create a variable called imagesXML, in all caps. Datatype it to XML.
00:25We'll give this variable a value later on, once the data is loaded. And I
00:30am creating a variable here so that we can access it anywhere in our code.
00:34Go to the next line and create a variable called xmlLoader, with a capital L.
00:40Datatype it to URLLoader. Set it equal to a new instance of the URLLoader class,
00:45exactly like what you would do with loading a text file.
00:49Go to the next line.
00:50Add an EventListener to the XML loader;
00:52xmlLoader.addEventListener. Event is going to be Event.COMPLETE, which means the
00:57file is finished loading. Type comma and a space.
01:00I'll run a method called xmlLoaded.
01:03Go down a few lines and create the xmlLoaded function.
01:07Make sure it receives an Event object, which I'm going to call evt, with the
01:12datatype of Event, close up the parentheses, type colon, void, and some curly
01:17braces. And here we'll set the value of imagesXML equal to the loaded data.
01:24Remember that you can access the loaded data through the URLLoader's data property.
01:29So type xmlLoader.data.
01:32Now, what we're going to do is make sure that Flash treats this data as XML,
01:37because the code as it is could cause errors.
01:40Data loads in similar to a string.
01:43So we want to make sure that this data is not a string, but XML.
01:47So to do that, I am going to type new, space, XML before xmlLoader.data.
01:52XML is going to be in all caps, then I'm going to wrap xmlLoader.data inside of parentheses.
01:58After that, we'll go to the next line, and we'll trace(imagesXML).
02:03And before we test the movie, we'll add one more important line of code to load the file.
02:07Make sure you do that below where you add the EventListener.
02:11So we'll go down right to the next line, under adding the EventListener, and type
02:14xmlLoader, capital L, dot load, and in parentheses, we'll create a new URLRequest.
02:22URL is in all caps, Request is with a capital R, and in the parentheses after
02:26URLRequest, we'll type a string.
02:28So type quotes and then type the path to the XML file, which
02:33is assets/images.xml.
02:37Test the movie, and you should see the XML data inside of the Output window.
02:46So to load an XML file, load the data just like you would load a text file.
02:51So use the URLLoader class.
02:54Once the data is loaded, pass in the data property of the URLLoader inside the
03:00parentheses of a new XML object.
03:03So make sure to tell Flash to treat that text data as XML data.
03:08So to load an XML file into Flash, use the URLLoader class, just like when you
03:13load a text file, and then set the loader's data property as the value inside
03:19of a new XML object.
03:21That way Flash knows for sure that that text data can be treated as XML data.
Collapse this transcript
Using dot syntax to access XML data
00:00Once you've loaded XML data into Flash, you can work with that data using dot syntax.
00:06That's part of the benefit of XML being native in ActionScript 3.0.
00:10Here if I test the movie, you can see the XML data in the Output window. Let's take a look.
00:20So we have the images root element and then inside of there are image child
00:26elements, and each one of those elements has an attribute called file that has
00:31the file name of an image.
00:33Now, let's say we want to access the file name of each image.
00:38Let's go back to our code and see how to do that.
00:43We'll just leave this code in a trace statement for now.
00:47We'll look at applying it to an actual image later on.
00:49So after imagesXML, you can type a dot and then type image.
00:55Now if you test the movie right now, here is what you'll get.
01:01Let's look in the Output window.
01:05This actually gives us a list of all the child elements named image.
01:11So if we go back to our code, imagesXML represents that outer XML element.
01:19It has a child called image.
01:21When we reference image, Flash gives us a list of XML objects.
01:28The datatype of this list is called XMLList. It looks like this:
01:33XML in all caps, and then List with the capital L. If you want to know
01:37everything you can do with an XMLList, highlight it, and press F1 on your keyboard.
01:42For now, we're going to look at something basic.
01:46Let's say we wanted to only access one object in a XMLList.
01:50After image, we can use square brackets, just like when you're working with an array.
01:55Remember that when you're working with an array, the first index is 0 and not 1.
02:01So type in 0, test the movie, and then we don't get anything.
02:07Now, that actually is not a bad sign.
02:11The reason for that is that the image element doesn't have any child elements,
02:16or child data at all.
02:18So that's what we're actually seeing here. When we reference that image,
02:21we're seeing any children or any data that's inside of that image element, and
02:26right now there's not anything.
02:28The data that we want to access is inside of an attribute of that element.
02:34So again, we're accessing the first image element, and when we reference that
02:38image by itself, we don't see anything just yet.
02:41After the close square bracket, type a dot and then type an at symbol,
02:46this stands for attribute, and then type in the attribute name that you want to access.
02:52That's called file.
02:55So inside of the image element, there is an attribute called file, and this
02:59is how you access it.
03:01Test the movie, and there is the data that we're looking for.
03:06So to review, when you create a new XML object with loaded data, that XML object
03:12represents the root of the loaded data.
03:16So in this case, it would be the images element.
03:21When you access child elements that have the same name, Flash gives you an
03:26XMLList, which is different from an XML object.
03:30You can access single elements in that list by using square bracket notation and
03:36an index number, just like when you're working with an array.
03:40Once you have access to an element, you can access attributes of that element by
03:44typing a dot and then by typing at symbol and the attribute name.
03:49So by using this line of code, we're able to access specific names of files that
03:55we want to work with.
03:56So remember that E4X enables you to work with XML data using dot syntax.
Collapse this transcript
Using XML data to populate a DataGrid component
00:00Flash's built-in components give you an excellent option for displaying data.
00:04Since these components already have many interactive and organizational features,
00:09you can use them to quickly prototype data-intensive applications.
00:13On the stage, I have an instance of a data grid component.
00:16You can access the components through the Components panel, which you can find
00:20at Window > Components.
00:22So you can just drag and drop a data grid onto the stage.
00:26So I close that menu and note that I've named the component grid, all lowercase.
00:32Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:37Now let's say I wanted to put the image names inside of the grid.
00:42One reason you may want to do this is to create a menu so that you can click
00:45images that you want to view.
00:46So, to do that, I'm going to use my XML data that I've loaded.
00:50In the XML loaded function, I have a trace statement that has
00:53(imagesXML.image(0).@file).
00:58I don't want to delete this line of code.
01:00I just want to disable it for now.
01:01So type two forward slashes at the beginning of that line of code.
01:05On the next line, we're going to create a loop to loop through the data.
01:10So create the skeleton of a for loop.
01:13Here, just create a standard loop.
01:16So var space i:unit =0;
01:22i <, and then what we're going to do is run it for each image inside of the XML file.
01:28Now remember, if we type imagesXML.image, we get an XML list. And I mention
01:36that it's like an array.
01:38Well, just like an array, I can get the length of an XML list to find out how
01:43many items are inside of it. So I type .length.
01:46Now, the difference between an XML list and an array is that length is actually
01:51a method and not a property when you're working with an XML list.
01:54So you need to type some parentheses.
01:55Just type a semicolon and a space and then i++.
01:59So this will loop through each image inside of the images XML file.
02:06Now to populate any data-driven component, including the data grid, you need to
02:11create an instance of the data provider class.
02:14So I'll do that right above the loop.
02:15So create a variable called componentProvider, capital P. Datatype it to a
02:25DataProvider and then set it = a new instance of the DataProvider class.
02:30So after that, type some parentheses and semicolon.
02:34Then each time the loop runs I'm going to add the XML data inside of the
02:39componentProvider, and then after the loop, we'll set the data inside of the data grid.
02:44So in the loop, we'll type componentProvider, with a capital P, .addItem, capital
02:50I, and then we'll put in the current XML object in the data grid.
02:55Now the way that you do this is first we'll close out the parentheses and then
03:00inside of the parentheses, type open and closed curly braces.
03:04So this is a shorthand notation to create an ActionScript-generic object.
03:10So what we do here is we put a name and a value pair separated by a colon.
03:16So the name is going to be Images.
03:20After that, type a colon, and then we're going to give the value.
03:24For the value, we want to put the file name.
03:28Now, we've actually written the code in that trace statement,
03:30so if I highlight imagesXML.image(0).@ file inside of the trace statement, I can
03:37copy that code paste it after the colon, and
03:41then change the 0 to an i so it will work for each image as the loop iterates.
03:48Now the last thing to do, once the dataProvider is all set up, is to connect it to
03:53the component on the stage.
03:54So under the loop, create a new line of code, and type grid.dataProvider =
04:04componentProvider, with a capital P. So test the movie, and then you should see
04:12all of the images inside of the data grid.
04:15So I have a scrollbar, I can click and select each of the images, and I can even
04:19click images at the top to organize the data.
04:23Now, the way that the data grid works is the name Images came from the value
04:29that we put right here.
04:31So if you wanted to create additional categories, we could type a comma after
04:36file, and then we can create another category here.
04:39I'm not going to do that now, but using that technique, you can simply follow
04:43that colon separation for name value pairs, and add more information.
04:47It's also important to note that this name Images here can only have one word,
04:53and no spaces or special characters.
04:55So just keep that in mind when you're working with the component.
04:58The same dataProvider technique follows for any of the ActionScript data-driven components.
05:05So if I wanted to, I can use the same code to apply to, say, a list.
05:09Let's see how that works.
05:11We'll open the Components panel. Make sure it's on the component layer.
05:20Scale that up just a little bit,
05:22so it's about the same size as the data grid.
05:24I'll give it an instance name of list.
05:27I'll go back to my code, copy of the grid.dataProvider line of code, paste it on
05:33the next line, change grid to list, and test the movie.
05:38Notice that it doesn't work, but it does have the right amount of fields.
05:43Now the list is a little bit pickier than the data grid is.
05:46With the data grid, I can select any name I want for the header of the images.
05:52So I can call it Images here.
05:54With the list, I need to use a specific term, which is label, and that's how it
05:59label an item in a list.
06:01This label pattern is what most of the data-driven components are looking for.
06:05So you set the label in the dataProvider and you test the movie, and then it
06:09should work properly.
06:10So now you see that the list is populated, and the data grid is also populated,
06:14but that title field is now label.
06:16Of course, I can click the label field in the data grid to organize the data.
06:21As I click the list and the data grid get updated at the same time.
06:26That's because as I click that, the dataProvider is being manipulated, and since
06:31these are both linked to the same dataProvider, the data for both is updated.
06:36So the data component allows you to display data and organize it in a variety of ways.
06:42To use the data grid, you need to create an instance of the DataProvider class
06:46and use its addItem method to put the right objects inside of the DataProvider.
06:52Then you need to connect that object to the data grid using the data grid's
06:56dataProvider property.
06:57If you want to use the same technique with other components, make sure that you
07:01use the label property when you call addItem.
Collapse this transcript
Using XML data to load image files
00:00XML data can be used to power nearly every type of application.
00:05Here is an example where XML can power a slideshow viewing application.
00:09On the stage, I have several instances of the UILoader component.
00:15The main image loader is called mainLoader, with a capital L.
00:18Then I have the thumbnail images: thumb0 - all lowercase, thumb1, thumb2, thumb3, et cetera.
00:28Now, if you're creating your own files, make sure that you start with the 0 and not 1.
00:33We're going to have these numbers correspond to indices of our XML list.
00:39Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:43I've actually already written the code for loading the thumbnails.
00:47Let's take a look at how it works.
00:49Outside of the loop, I created a variable called thumbLoader, and inside of the
00:53loop, I set thumbLoader = a typecasted UILoader. And then inside of there, I'm
01:00grabbing the thumbnail, and there is going to be one thumbnail for each
01:05iteration of the loop.
01:06So it's thumb + i. So it's starts at thumb0, and then thumb1, et cetera.
01:13That thumbnail is going to load the appropriate thumbnail image.
01:18So we have images.XML.image(i).@file.
01:23So it's loading that file.
01:25Then I set its buttonmode property to true, and I added an EventListener to each
01:30thumbnail loader so that it runs a function when you click on it.
01:33So let's take a look inside the thumbClicked function.
01:36The code that we're going to write is going to apply to the main image.
01:39In order to get the image to load, I need to find out the number at the end of
01:44the thumb that was clicked.
01:45We can start by accessing the thumb's name.
01:48So create a variable called thumbName with a capital N. Datatype it to a
01:52string and the value will be evt, which is the event object .currentTarget, which
01:59will refer to the object that was clicked,
02:01so thumb0, thumb1, thumb2, et cetera .name, which will give us the name of
02:06the object as a string.
02:08Now we need to extract the last number of the thumbnailed name and treat it as a number.
02:15So create a new variable on the next line called thumbIndex, capital I. Datatype
02:23this to unit a positive integer.
02:26We'll set it = thumbName.substr, in the parentheses pass in 5.
02:37Now, I got 5 because each thumbnail's name has six letters;
02:41thumb has five letters and then the additional number makes six.
02:48We're going to referencing a position of a character in a string, the first
02:51one starts at zero.
02:52So then the number would be index 5, because the last letter of thumb would be index 4.
02:59So 0, 1, 2, 3, 4, and then 5 for the number.
03:07Now in order to tell Flash that this is a number, I'm going to write uint before
03:11thumbName and then wrap thumbName.substr in some parentheses.
03:17So Flash will treat that as a number.
03:19Then we'll create one more variable that will represent the full path to the
03:25image we want to load.
03:26So we'll call this fullPath, with a capital P, datatype it to a string, and
03:33we'll set the value.
03:34We'll start off with assets and a forward slash.
03:38So type some quotes to create a string and then type assets and a forward slash.
03:44After the quotes, type a Plus sign.
03:46Then we're going to get the data from our XML object.
03:50To get that data, I'm going to copy code that already exists in this frame.
03:55In line 20 on my code, I'm loading the thumbnail images.
03:59In that code, I'm going to select the portion of code that starts with images.XML
04:06and ends with attribute file.
04:08So the code will be referencing the XML.
04:11Copy the code with Command+C or Ctrl+C, and then paste the code after the Plus
04:15sign in the thumbClicked function, in the line of code with a semicolon and
04:20replace i in the pasted code with thumbIndex.
04:25Before we go any further, let's review what these three lines of code are doing.
04:29thumbName is a string, and it's holding the name of the thumbnail that was
04:34clicked. thumbIndex is a positive integer.
04:38It's holding the last character of the thumbName string. fullPath is the path to
04:45the full-size image.
04:46We're getting the correct full-size image through the XML data, by passing in the
04:52thumbIndex as the index number of the image XML list.
04:58Now let's add one more line of code to this function.
05:00We're going to have the mainLoader load the image file.
05:05So type mainLoader.load and then in parentheses, create a new URLRequest. Inside the
05:14parentheses for URLRequest, type fullPath, with a capital P.
05:19Test the movie, and you shouldn't get any errors, and you should be able to
05:23click the different thumbnails to load the different images into Flash.
05:27So the slideshow is complete, and all of the data is powered by XML.
05:32So if you ever wanted to change the files that are loading into the app, you can
05:36make the change without having to republish your SWF file.
Collapse this transcript
11. Creating an Audio Player
Loading audio from the Library
00:00If you want to have audio in your applications and not have to worry about
00:04whether they will be loaded when you need them, then loading audio from the
00:08library is an excellent option.
00:11When you load audio from the library, it gets embedded inside of your SWF
00:14file, so you guaranteed that it's there when your ActionScript code is ready to be accessed.
00:20In order to use a file from the library, go to the library and right-click the
00:24audio file you want to use;
00:26mine is called BGLoop.mp3.
00:29Then click Properties.
00:31After that, check the box to export for ActionScript, and just take .mp3 off the
00:36end of your file name.
00:38So I have BGLoop, with B,G, and L capitalized.
00:43If you're using a different file, note the name of the class here.
00:47Make sure you don't use spaces or special characters, and that the class name
00:51starts with a letter.
00:52So click OK, and you'll see BGLoop under Linkage in the library.
00:57Go to the first keyframe of the actions layer and open up the Actions panel.
01:02Create a variable called song. Datatype it to whatever you class you set in library,
01:08so I'm going to type BGLoop, with a capital L, and then create a new instance of that class.
01:13So new BGLoop, capital B, capital G, capital L. After that, all you have to do is
01:21use the play method. So song.play();.
01:26I'll test the movie.
01:29(Music playing.)
01:31So you can hear the background music playing.
01:34So after you've exported your song for ActionScript, create a new instance of
01:38that class, and then use the play method to play the song.
Collapse this transcript
Loading audio from external files
00:00If you want to keep your SWF file size low, and are loading multiple songs, it
00:05is best to store that audio in external files.
00:08Before we look at loading audio, let's look at the assets onstage.
00:11I have a button called play_btn.
00:16There is a button underneath it called pause_ btn, all lowercase, just like the Play button.
00:22There is a movie clip called slider that's going to show the playback progress.
00:28Behind that is a movie clip called bar.
00:32If I hide my mask and lock it, and hide the slider layer as well, you can
00:39see this blue block
00:41that has an instance name of load_ progress, and it's going to display the
00:45progress of the loading file.
00:48Then I have mute_btn.
00:50So, all of my instances are on the main Timeline for this application.
00:54So, let's go to the first keyframe of the actions layer and open up the Actions panel.
00:59The first thing we're going to do is reference the audio file that we want to load.
01:03We'll hold that inside of a URL request variable.
01:06So create a variable called audioFile, and we'll datatype it to a URLRequest.
01:13Set that equal to a new instance of the URLRequest class, and inside of the
01:18parentheses for the URLRequest constructor, type some quotes, and then type the
01:23path to the file you want to load.
01:25The file I want to load is in assets/bg.mp3.
01:32I mentioned earlier that this is a constructor, this new URL request line of code.
01:37If you remember when you created your custom class, you had a function, or method,
01:41in there that was the same name as your class.
01:45That's the constructor.
01:46And that's exactly what we're doing here when we use the New command.
01:49We're running the constructor method of the URLRequest class.
01:52In other words, we're initializing it.
01:54Go to the next line and create a variable called song, and this will be a sound datatype.
02:01Create a new instance of the sound class after that.
02:04So new Sound();. Go down a few lines, and we'll add an EventListener to the song.
02:10So type song.addEventListener, and this is going to trigger when the file is done loading.
02:16So we're going to type Event.COMPLETE. Make sure Event has a capital E and
02:20Complete is in all caps.
02:21After that, type a comma and a space, and type songLoaded, with a capital L.
02:26On the next line of code, we'll type song.load, and in parentheses, we'll pass in audioFile.
02:33In the past when we used the load method, we created the URL request inside
02:38of the parentheses.
02:39For this example, I held it in a variable outside of that.
02:42That way, if you ever want to reference it later, you have easy access, and
02:46you don't have to retype the code.
02:48Also, note that you always add the EventListener before you load the file.
02:53So, let's go down a little bit and define the songLoaded function.
02:56So type function songLoaded, with a capital L, evt:Event, for the datatype,
03:06colon, void for no return data, and then inside of the curly braces, we're
03:10going to play the song.
03:12So just type song.play, some parentheses and a semicolon, and that's it.
03:18So you should be able to test the movie, and hear the song playing. (Music playing.)
03:20(Music playing.)
03:25So to load an external sound file, create a new instance of the sound class and
03:30run that class's load method, passing in the file you want to load.
03:34Remember to add the EventListener before loading the file and then use the
03:38play method once the file is loaded to play the sound.
Collapse this transcript
Playing, pausing, and stopping sounds
00:00The sound class lets you load and play audio files, but if you need more
00:04control over playback, including pausing and stopping sounds, you'll need to
00:09work with other classes.
00:11So right below the new Sound line of code, which is line 6 in my code, I'm going to
00:16go to the next line, create a new variable called songController, with a capital C.
00:22And this object will give us more control over working with the sound.
00:26Datatype it to SoundChannel, capital S, Capital C. Don't give it a value yet;
00:32just type a semicolon.
00:33The next thing we're going to do is add event listeners to the Play and Pause buttons.
00:38We'll start with the Play button.
00:39Scroll down to songLoaded, and delete the line of code that says song.play.
00:44We'll make the sound play later on when you click the Play button.
00:47So type play_btn.addEventListener, capital E, capital L, and in parentheses, type
00:55MouseEvent, capital M and a capital E, .CLICK in all caps, comma and space, and
01:00then type clickedPlay, with a capital P.
01:04Now we'll just copy and paste this line of code for the Pause button.
01:08So select it. Command+C or Ctrl+C to copy. Go to the next line. Command+V
01:13or Ctrl+V to paste.
01:14Change play_btn to pause_btn in the pasted code.
01:20Then change clickedPlay to clickedPause, with a capital P. Now we'll define the
01:25clickedPlay function.
01:26So go down a few lines, below the songLoaded function, and create the clickedPlay
01:31function. Make sure to type a capital P. In parentheses, type evt:MouseEvent, with
01:37a capital M and a capital E, :void for no return data, and then inside of the
01:42function, we're going to play the song,
01:44except instead of typing song.play, we're going to type songController = song.play().
01:53So, we're setting the controller object equal to the playing sound.
02:00That way, we get more control over working with the sound,
02:03because the SoundChannel class is basically a controller for sound, and the
02:07Sound class simply loads and plays sound.
02:11Before we add more code into the play function, let's just click and drag to
02:15select this function, and copy and paste it a few lines below.
02:19In the pasted code, change clickedPlay to clickedPause, and then delete the line
02:24of code that says songController = song.play();.
02:27Instead, type songController. stop, and some parentheses.
02:34So, stopping a sound is something you can't do without using the
02:37SoundChannel class.
02:39So let's test the movie, and we can click to play the song.
02:42(Music playing.)
02:48You'll notice that if you keep clicking, the sound will start all over
02:51again from the beginning.
02:52Now, we don't want that to happen,
02:54so we'll disable the Play button and only show the Pause button after you click it.
02:58That way before you can play the song, you have to stop it first.
03:02So in clickedPlay, right above songController = song.play, type the following code:
03:09play_btn.visible = false, and on the next line type pause_btn.visible = true.
03:19Select these two lines of code, copy them with Command+C or Ctrl+C, and paste
03:24them at the top of the clickedPause function, above songController.stop.
03:29In the pasted code, switch false and true, so change false to true and true to false.
03:37So that way, when you click the play button, the Play button will hide and the
03:40Pause button will show, and then when you click the pause button, the Play
03:44button will show and the Pause button will hide.
03:47Also, by using the visible command, the functionality will be disabled, so you
03:51can't play the song multiple times.
03:53Test the movie and click to play and stop the song.
03:57(Music playing.)
04:02Notice that after you click Pause, and you click Play again, the song
04:06starts from the beginning.
04:07So what if you wanted to just pause instead of stop?
04:10Let's take a look at how to do that.
04:12Scroll up to the top of the code, right below var songController:SoundChannel,
04:18which is on line 9 in my code. Create a new variable called resumeTime, capital
04:22T. Datatype it to a number, and set its value at zero.
04:27This is going to represent when the sound should resume.
04:30Scroll down and then go to the clickedPause function, right above
04:34songcontroller.stop. Create a new line of code and type resumeTime =
04:41songController.position.
04:46That represents the position of the sound when you pause it.
04:49So we'll save in that resumeTime variable, and then apply it when we play the song.
04:55So go to the clickedPlay function and click inside of the parentheses after
04:59song.play, and just type resumeTime.
05:01So the song will start from the resumeTime, which at the beginning is going to
05:07be zero, or at the beginning of the song.
05:09So when you click the Pause button, the resume time is captured, and when you
05:15click Play again, it's applied, so the sound continues from that point.
05:19So test the movie, click Play and Pause and Play again.
05:23(Music playing.)
05:28So you can see that the pause functionality works.
05:31So using the SoundChannel class, you have more control over your audio by being
05:35able to add, stop, and pause functionality.
Collapse this transcript
Muting all audio with the SoundMixer.stopAll method
00:00Have you ever been surfing the web and been surprised by an unexpectedly loud site?
00:05This surprise can quickly turn into anger if you can't find a way to control the volume.
00:09When you have audio on the web, providing a Mute button is always a plus.
00:14In your code, scroll down to the songLoaded function.
00:17In there, copy and paste one of addEventListener lines of code.
00:21So you should have three lines of code in there.
00:24Change pause button or play button, which ever one you chose, to mute_btn.
00:31Change the name of the function, clickedPause or clickedPlay, to clickedMute.
00:38Scroll down in your code and create the clickedMute function.
00:42I'll do this at the very bottom of my code.
00:45So function clickedMute, capital M, and parentheses evt:MouseEvent, capital M,
00:52capital E, close out the parentheses :void, make some curly braces, and inside
00:58of there, you can mute audio by typing soundMixer, capital M, .stopAll, capital A,
01:08curly braces, and some parentheses, and a semicolon.
01:12So test the movie, and when you play this sound, you can click the mute button to mute the audio.
01:17(Music playing.)
01:22So with the soundMixer.stopAll, you can stop all ActionScript sounds.
Collapse this transcript
Tracking load progress
00:00Just like any other file loaded into the Flash player, you can track the load
00:04progress of an audio file.
00:06On the Stage, I am going to track the load progress and display it by using this
00:11movie clip Load_Progress.
00:13There is one thing you should know about this movie clip:
00:15its full size is the whole width of that background bar.
00:20So if you look in the Transform panel, I have 3.2% for the Scale Width, and if I
00:26change that to 100%, you will see it's much wider.
00:32So I am going to undo that change, and what we are going to do is set its scale
00:36X property to scale to the full width of this bar.
00:40So let's go to the first keyframe of the actions layer and open up the Actions panel.
00:44Scroll all the way up to the top of your code, and right below
00:48song.addEventListener Event.COMPLETE, create a new line of code and type
00:53song.addEventListener and type ProgressEvent.PROGRESS.
00:58This refers to the progress of the file loading, not the progress of the file
01:03playing. And then type a comma and space, and trackLoad, with a capital L.
01:10Scroll down, all the way to the bottom of your code, and create that function.
01:15So function trackLoad.
01:19In parentheses, type evt:ProgressEvent with a capital P and a capital E. After the
01:25parentheses, colon void, all lowercase, some curly braces.
01:30Now here, we are going to scale the movie clip.
01:34So type load_progress -
01:36that's that Load_Progress movie clip - dot scaleX with a capital X. We'll set
01:41that equal to the percent loaded.
01:44Remember that we can get the percent loaded by dividing the total bytes loaded
01:49by the total amount of bytes.
01:51So type evt.bytesLoaded / evt.bytesTotal, with a capital T.
01:59Close it out with a semicolon and test the movie.
02:02You will see that bar animate to the right across the stage.
02:07Now, this is a pretty small file, but you can test the movie again, and you can
02:13see it loading from the left to the right.
02:15If you want to confirm that it's working properly, because you can't see it, you
02:19can always trace the value of load_progress.scaleX.
02:24The Output window will show you that it's scaling bit by bit as the file loads.
02:28So to track load progress, add a listener for ProgressEvent.PROGRESS to your
02:33sound object, and then you can display visual feedback in the EventHandler.
Collapse this transcript
Displaying sound position
00:00Most audio players you see provide some feedback about the playback progress of a song.
00:05Let's take a look at how to add that functionality to our app.
00:09Scroll down to the clickedPlay function.
00:12At the bottom of that function, type addEventListener, with a capital E,
00:16capital L, and in the parentheses listen for Event.ENTER_FRAME, in all caps.
00:23After that, type a comma and a space, and then we will run a function called
00:27trackPlayback, capital P.
00:29Remember that when you listen for the ENTER_FRAME event, Flash runs that code
00:33multiple times per second.
00:35So if possible, you want to disable that code when you are not going to need it.
00:39So let's copy this line of code, select it,
00:42Command+C or Ctrl+C to copy, and then at the bottom of clickedPause, we will
00:47paste that code and change add to remove.
00:50That way when the sound is not playing,
00:52we won't be listening for the ENTER_FRAME event.
00:54So now we will define the trackPlayback function.
00:58Scroll to the bottom of your code and create the trackPlayback function.
01:01Make sure the playback has a capital P. You will receive an event object with
01:06the datatype of Event, with a capital E, colon void for no return data.
01:12Then in the curly braces, we will write the code to display playback progress.
01:17The first task is to get the percentage that has been played.
01:20So create a variable called perPlayed, with a capital P, datatype it to a number,
01:25and set it equal to the current position of the song divided by the total
01:30duration of the song.
01:31So to get the position, you go through the SoundChannel class, and our
01:35SoundChannel instance is called songController, capital C. After that, type a
01:40dot, and then type position.
01:42So we will divide that by song.length.
01:46So you get the length of the song through the sound class, and our instance of
01:51the Sound class is named song, and you get the position of the song through the
01:55SoundChannel class, and our instance is called songController.
01:59So that will give us the percentage that has been played.
02:01Now, we are going to position the slider based on that percentage.
02:04Let's type slider.x, space, equals, space.
02:08Now, we know we are going to multiply the percent played by something.
02:12So just type perPlayed, with a capital P, space, asterisk, and a space.
02:16Now, the way we want to multiply it is so that when perPlayed is equal to 0,
02:21then the slider is all the way at the left edge of the bar, and that when it's
02:24equal to 1, which is when the whole song has been played, the slider goes all
02:29the way to the right edge of the bar.
02:31So here, I'll write some parentheses, and then type bar.width - slider.width.
02:38Remember, we always subtract the width of the object for this reason.
02:42I close the Actions panel and reveal the slider.
02:45If I had it equal to bar.width, then the maximum position would be right here at
02:50the edge of the bar. But since I am setting it equal to bar.width -
02:54slider.width, the right edge of the slider will be aligned with the right edge
02:58of the bar at the end.
03:02Now, we need to take this whole value right here that we are going to have,
03:06and we need to offset, that because if we don't, the playback progress will
03:10look a little bit weird.
03:12Just to see an example, type a semicolon at the end of this line and test the movie.
03:17Click the Play button, and then watch the slider,.
03:19(Music playing.)
03:24Notice that it starts at the left edge of the stage;
03:27watch where it ends.
03:28(Music playing.)
03:37There is a little empty space after the end of the song, which is why it's still moving.
03:42So we have a little bit of space.
03:45It looks like this has shifted to the left a little bit.
03:47Let's take a look at why that's happening.
03:50The reason why is because we are just multiplying percentage played by the width of the bar.
03:56We are not offsetting the position by the X position of the bar.
04:00So if we offset the position by adding a value at the very end, so we'll add on
04:04bar.x, then we'll shift that whole movement to the right and the amount of the X
04:09position of the bar, so that when percentage played is 0, and 0 times whatever
04:15this value is is going to be 0, then the 0 position of the slider is going to be
04:20aligned with the left side of the bar.
04:22Let's test the movie, preview the animation.
04:25Click the Play button, and watch the slider.
04:27(Music playing.)
04:40So the slider goes all the way to the end and then stops.
04:46So to provide playback progress, you'll have to write the custom code yourself
04:51by adding an ENTER_FRAME EventListener.
04:56Inside the ENTER_FRAME event, you can get the percentage played through the
05:00SoundChannel's position property and the SoundClass's length property.
Collapse this transcript
Adjusting volume
00:00We've already looked at muting an audio file using the
00:04soundMixer.stopAll method,
00:06but let's say you want better control over the volume of your file.
00:11To do that, we can use the sound transform class to manipulate volume precisely.
00:17Right before the soundMixer.stopAll method, type two forward slashes.
00:22This is inside of a clickedMute function, by the way, which is on line 41 in my code.
00:27Go down to the next line and then create a variable called volControl, with a
00:31capital C. Datatype it to a SoundTransform object.
00:36We'll set it equal to songController.soundTransform.
00:41So we're taking that sound transform property in song controller and storing it in a variable.
00:47Let's go down a few lines, and then we will adjust the volume control object.
00:52So we will type volControl.volume = 0.1.
00:59So instead of muting it completely, we are going to get it really quiet.
01:02Then to apply that volume transformation, go down a few more lines and then type
01:08songController.soundTransform = volControlled;
01:17to end the statement.
01:18So once you manipulate the volume of a sound transform object, you need to reapply
01:23that to the sound transform property of your sound channel object.
01:27So let's test the movie and then click Play and click the Mute button to
01:31hear the sound muffled.
01:32(Music playing.)
01:38It looks like it's working.
01:40So let's say we wanted to make that more of a sound toggle button.
01:45So instead of just muting the sound, we want to toggle the sound on and off.
01:49So right above volControl. volume = 0.1, create an if statement.
01:54Grab volControlled.volume in some curly braces, like so, and in the if statement,
02:04type volControl.volume == 1. So, two equal signs and then 1.
02:11So if the volume is at 1, and by the way volume goes between 0 and 1,
02:17anything below 0 or above 1 will be distorted.
02:20So if that's the case, then we're going to change volumeControl.volume to 0.1.
02:26We will create an else statement and set volControl.volume = 1.
02:33So we'll toggle back and forth between muffled at 0.1 and full volume at 1.
02:38So let's test the movie. Preview the animation.
02:41(Music playing.)
02:49So now you can click Mute button and toggle the volume to be full volume or muffled.
02:55Now of course, if you wanted to mute this all the way, you can set volume at 0.
02:59So I'll test the movie one more time, and you can see this in action.
03:02(Music playing.)
03:08So that completes our video player.
03:09By using the SoundTransform class, you can manipulate the volume of objects.
03:14Just make sure that after you manipulate the volume, you reset the
03:19SoundTransform property of the SoundChannel object.
Collapse this transcript
12. Working with Video
Touring the FLA file
00:00There are multiple ways to play back video in the Flash Player.
00:04You can use an entire ActionScript interface, called NetStream and NetConnection,
00:09or you can use the FLVPlayback component and have a visual interface.
00:14For this application we are going to go with the FLVPlayback component.
00:17Don't think you're missing out though, by not doing ActionScript version, because
00:21the FLVPlayback component opens up a whole new world of interactivity when you
00:25start using ActionScript to control it.
00:27Before we start building the app, let's take a look at this FLA file.
00:31I've already added some code to it and set up a pretty complex
00:35organizational structure.
00:36So we'll go through each piece.
00:38On the Stage, in the main Timeline, I have an instance of the FLVPlayback
00:43component called flv_mc.
00:47I'm linking to a source movie that's in the assets folder, and the file is called bear.f4v.
00:54I don't have a skin for the component.
00:56Also, note that I added four cue points.
00:58Each of the cue points has a parameter.
01:03I named the parameter 'caption' in each case.
01:07And there in the Value section, I set some text.
01:11The text describes what's going on in the movie.
01:14So you can click through the different cue points and see that I have a caption
01:17parameter and then a different value in each one.
01:21The names of the cue points are CuePoint1, CuePoint2, CuePoint3, and CuePoint 4,
01:25so I kept those generic.
01:27In front of the video, I have a movie clip called captions_mc.
01:32This will display the caption text for the cue points.
01:35If I double-click the movie clip, I can enter its timeline, and you'll see that
01:39I have a text field called caption_txt.
01:43I use a dynamic text field, embedded the font, and have a drop shadow on it.
01:47Let's go back to the main Timeline and look at some of the other elements.
01:52This movie clip is called playPause_mc.
01:55It's an instance of a movie clip called PlayButton.
01:58If I double-click it, you can see its timeline, where there's a movie clip named
02:02pause_mc and another movie clip named play_mc.
02:07Back to the main Timeline. Then I have the progress bar.
02:10I call this progbar_mc.
02:15Inside of that movie clip, I have a background bar that I called bg_mc, and then
02:21the little slider is called controller_mc.
02:25The volume button is called volume_mc.
02:29Double-click it to enter its timeline to see that there's another movie
02:33clip that's hidden.
02:34That movie clip is called bar_mc.
02:36If I scroll up the alpha, you can see it has a slider on it.
02:40I will leave the alpha, just so we can take a tour of his movie clip.
02:43But the way this is going to work is that when you roll over the Volume icon,
02:48this bar_mc movie clip will show and when you roll out, it will hide again.
02:53So let's double-click bar_ mc to enter its timeline.
02:56So I have a bar in the background and then an element that I call drag at the top.
03:01I've already added drag interactivity for the slider.
03:04You can see the code in the first keyframe of the actions layer.
03:07If you don't have access to the exercise files, make sure to copy this code down.
03:11I have a variable called scrollPercent, and then I have the standard drag and drop code.
03:16Add an EventListener to start dragging, and I start drag with a
03:19constrain rectangle.
03:21We talked about this earlier in this course, when we built the scrollbar for the
03:25text field. Add an EventListener to the stage to listen for MouseEvent.MOUSE_UP.
03:30Then while dragging, I added an EventListener to listen for the ENTER_FRAME
03:33event, and I removed it when we stopped dragging.
03:36In the ENTER_FRAME event, and I set the scrollPercent to the percentage that was scrolled.
03:41Since this code here gives a value of zero at the top when you're scrolling and
03:46one at the bottom, I inverted the value, because we are going to do the opposite
03:51of the scrollbar when we do the volume slider.
03:54The full value needs to be at the top, and the minimum value needs to be at the bottom.
03:58So to offset that, I subtracted scroll percent from 1.
04:02So close this window and return to main Timeline.
04:05There is a movie clip called info_mc.
04:07That's just a simple graphic.
04:11Finally, we have some code on the main Timeline.
04:14Select the first keyframe of the actions layer and open up the Actions panel.
04:18In here I'm adding EventListeners to volume_mc for MOUSE_OVER and MOUSE_OUT and
04:24then info_mc for mouse CLICK.
04:27I set buttonmode to true for info_mc and volume_mc, so that you can see hand cursor.
04:32I've defined those functions here.
04:35So I have toggleCaptions.
04:37This is going to happen when you click on the caption button, that little i, and
04:39what it's going to do is toggle the visibility.
04:42I set the visible property = to !captions_mc.visible.
04:48Exclamation point means 'not' in ActionScript,
04:51so what it does is it sets the visible property to the opposite of what it currently is.
04:57So if it's visible, then it will set it to be not visible, and vice versa.
05:02The volumeOver and volumeOut functions are pretty straightforward.
05:06When you roll over the volume movie clip, the bar will show, and when you roll
05:11out, the bar will hide.
05:13Note that I'm using alpha instead of visible.
05:16That's because I don't want to disable the interactivity of the Volume bar.
05:20Before we test the movie to preview what we have to start with, I will close the
05:24Actions panel, and then go back into the volume movie clip, select the volume
05:29slider, and turns its alpha back to 0.
05:31Now let's test the movie, preview what we have.
05:36I can click the Captions button to show and hide captions movie clip up here.
05:40I can roll over and roll out to show and hide the volume movie clip.
05:45When I roll over I can click and drag the slider.
05:49So once your FLA is set up just like mine is,
05:51if you feel comfortable to structure the file, you are ready to start
05:55programming your video player.
Collapse this transcript
Controlling video playing and pausing with ActionScript
00:00Using some simple code, you can connect Play and Pause buttons to an
00:04FLVPlayback component.
00:06So here we're going to connect custom buttons to control the
00:09FLVPlayback component.
00:10The great thing about the FLVPlayback component is that you get a shortcut for doing this.
00:15Let's take a look at how it works.
00:18On the Stage, I have my movie clip, playPause_mc.
00:22If I enter its Timeline, you'll note that I have a movie clip called
00:26play_mc, and then pause_mc.
00:31Now if you structure a movie clip like this, and name a movie clip play_mc and
00:37another one pause_mc, something magical happens with the FLVPlayback component.
00:43Go to the first keyframe of the actions layer and open up the Actions panel.
00:47Right below, where we are setting buttonmode for info and volume,
00:50I'll create a new line of code.
00:52I'll type flv_mc.playPauseButton = , and we'll set that equal to playPause_mc.
01:10So after that, just type a semicolon, and test the movie.
01:14Check out the button functionality.
01:14(Music playing.)
01:25So that one line of code made it so that it shows the hand cursor when you
01:29roll over the Play button or the Pause button, that when you click the Play or
01:33Pause button, the appropriate button hides, and the appropriate button shows,
01:38and the Play and Pause buttons both work just as you'd expect them to, to control the video.
01:43This is a pretty nice shortcut when you're working with the
01:45FLVPlayback component.
01:47So by using the Play/Pause button property, you can add custom Play and Pause
01:52buttons to your FLVPlayback component with only one line of code.
Collapse this transcript
Working with ActionScript cue points to add closed captioning
00:00Using the Flash CS5 cue point system, you can add ActionScript cue points to your FLV files.
00:07Remember, as we looked at earlier, when you select an FLV file, you can add cue
00:11points through the Properties panel.
00:13Remember that each cue point has a parameter called caption with a value that
00:18we're going to place inside of that text field.
00:20Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:25At the bottom of all lines of code that say addEventListener, I'm going to add a
00:28new line and a new EventListener.
00:32We'll add the EventListener to the FLVPlayback component.
00:35So flv_mc.addEventListener() and a semicolon.
00:43Now the event that we're going to listen for is the
00:47MetadataEvent.CUE_POINT event.
00:50Now you'll just have to remember this, because even though ActionScript cue
00:55points are embedded into a video's metadata, you are still going to treat them as
01:00if they are metadata in the code.
01:02So Metadata, only a capital M, Event with a capital E, should turn blue .CUE_POINT all
01:12caps, and then a comma and a space.
01:16We'll run a function called cuePointReached.
01:21So above the toggleCaptions function, I'm going to define the
01:25cuePointReached function.
01:26Make sure it receives an event with a datatype of MetadataEvent :void, no
01:35return data, then some curly braces.
01:39Now we are going to set the text of the text field inside of captions_mc.
01:44So it's captions_mc.caption_txt -
01:50that's a text field inside of the movie clip - .txt = evt.info.
01:58Notice in the code hinting area, info property is an object.
02:02That gives us the metadata.
02:04Inside of the metadata, there is another property called parameters.
02:09So type .parameters.
02:11The code hinting looks a little bit weird right here, because the code hinting
02:15for the Object datatype is just these few things, because the Object datatype
02:20can hold any number of parameters that you can create whenever you want.
02:25So you can't get code hinting for the Object datatype.
02:28So after parameters, then we type the name of the parameter that we want to
02:32access, which is caption.
02:34So we type .caption. Type a semicolon.
02:39Now you should be able to test the movie,
02:42click the Play button, and watch the captions appear.
02:45(Music playing.)
02:55Of course, if you want to, you can click the i button and show and hide the captions.
03:01If you continue on, everything will still work properly.
03:04(Music playing.)
03:16So once you've set up your cue points, you can use them for just about anything
03:20you want, including closed captions.
03:22To use them, you listen for the MetadataEvent.CUE_POINT event.
03:28You can access the data through the info property.
03:31You can access the parameters through that object's parameters property.
Collapse this transcript
Displaying video playback position
00:00Now we'll connect the progress slider to the FLVPlayback component to show
00:05the playback progress.
00:06Unfortunately, there is not an amazing shortcut that's as simple as adding
00:12the Play/Pause button.
00:13So we'll have to write the code ourselves.
00:15Let's go to the first keyframe of the actions layer and open up the Actions panel.
00:20In here, I'm going to add an EventListener in the addEventListener section of
00:24code, right below flv_mc.addEventListener.
00:29So here I'll add an EventListener, and just type addEvent, with a capital E,
00:34Listener, with a capital L, and the event is going to be Event.ENTER_FRAME in
00:42all caps, and we'll run a function called trackPlayback.
00:54Let's define the trackPlayback function right above the
00:56cuePointReached function.
00:58So create a function called trackPlayback, capital P. In the parentheses, write
01:06an event object with the datatype of Event, capital E, :void, for no return data.
01:13We'll track the playback right here.
01:16The first thing we'll do is create a variable called per and then Played, with a
01:19capital P. Datatype it to a Number.
01:22We'll set it equal to a value in just a second.
01:26We can get the playback percentage through the FLV object, so
01:31flv_mc.playheadPercentage.
01:35Playhead is all lowercase.
01:38Percentage has a capital P. It should be blue.
01:41Then we're going to divide that value by 100.
01:45The playheadPercentage property gives you a whole number between 1 and 100.
01:50We don't want a whole number.
01:51We want between zero and one.
01:53So we simply divide that by 100.
01:54That's what we'll get.
01:57Now we'll place the movie clip to show the progress.
02:01Go to the next line, and then we'll type progbar_mc.controller_mc.x = perPlayed,
02:16capital P, times, asterisk, and then in parentheses, we're going to write the
02:25maximum X position that we want the controller to go.
02:29Remember, that's going to be the whole width of the progbar minus the width of the controller.
02:34So, in here, type progbar_mc.width space - space progbar_mc.controller_mc.width.
02:52So we'll test the movie, and preview the animation.
02:58Click the Play button to play the movie, and watch the progress slider
03:02move across the screen.
03:03(Music playing.)
03:24So notice, it starts at the right place and ends at the right place.
03:29So by using the playheadPercentage property, we can get the position of the
03:33movie that's being played in the FLVPlayback component.
03:36We divide that by 100, and wee can use that information to display the progress of the movie being
03:42played back.
Collapse this transcript
Adjusting video volume
00:00Now we'll use the volume slider to control the volume in the
00:04FLVPlayback component.
00:06Let's start by adding some code to the volume slider.
00:10I'll double-click the volume movie clip on the main Timeline and then
00:14double-click the volume bar, bar_mc, inside of the volume control movie clip.
00:21Inside that movie clip, I'll select the first keyframe of the actions layer and
00:25open up the Actions panel.
00:28In here, I'm going to add one line of code.
00:31If you ever want to create your own custom event, it's as simple as calling the
00:36method called dispatchEvent.
00:39So let's say I want to announce that the scrollbar has been updated.
00:45So every frame, I want to tell anyone who wants to listen that the scrollbar is updating.
00:53So to do that, I'm going to go right below scrollPercent = 1 - scrollPercent;
00:59so that's in sendUpdate, at the bottom of the code.
01:01I'll go right below that line of code and then type dispatchEvent, with a
01:08capital E, some parentheses and a semicolon, and inside the parentheses, I'm
01:13going to create a new event.
01:16So I'll type new Event, with a capital E, some parentheses, and inside of
01:24those parentheses, for the new event, I'm going to tell Flash which type of
01:28event I want to send out.
01:31For this, I'm going to use a built-in event that's made for sending out
01:35information like updates.
01:38The event is called Event, with a capital E, .CHANGE in all caps.
01:43So, let's go back to the code in the main Timeline.
01:46If you want to take your shortcut, you can open up the toolbox, and then you can
01:51find Scene 1 actions : frame 1, and click that.
01:56Now, I'll close the toolbox.
01:59I'm back in my code, on the main Timeline.
02:02In this code, I'll go to my addEventListener area, and on line 8, right after
02:08addEventListener(Event.ENTER_FRAME), I'm going to go to the next line, and I'm
02:13going to add an EventListener to the bar.
02:16So type volume_mc.bar_mc. addEventListener, capital E, capital L. And in the
02:29parentheses, I'm going to listen for the event that I executed in there,
02:33which was Event.CHANGE.
02:35So Event, with a capital E, .CHANGE, in all capitals. Type a comma and a space.
02:41We'll run a function called volslider, with a capital S, Updated, with a capital U.
02:48Now, we'll define the volsliderUpdated function.
02:52I'll do this right above the track playback function.
02:55So create a function there called volsliderUpdated, capital S, Updated with U,
03:02some parentheses, evt:Event, with a capital E, close out the parentheses, colon
03:10void, some curly braces, and inside of there, I'm going to set the volume of the
03:15FLVPlayback component.
03:18Type flv_mc.volume = volume_mc.bar_ mc.scrollPercent, with a capital P.
03:31Remember that's the variable that was inside of bar_mc.
03:35So we're accessing a custom variable that we created inside of bar_mc.
03:41So we're just setting the volume equal to the scrollPercent of the scrollbar.
03:47Let's test the movie, and use the volume slider.
03:52So play the movie. As I drag the volume slider down, the volume goes down.
04:00As I drag it up, it goes up.
04:02(Music playing.)
04:16I love that.
04:18So now I'll close the playback window, and we've completed our video player.
04:23So to adjust volume on an FLVPlayback component, use its volume property.
04:28If you want to send out a custom event, then use the dispatchEvent command,
04:34create a new event inside the parentheses, and inside the new event parentheses,
04:40dispatch any event you like.
Collapse this transcript
13. Flash CS6 ActionScript Features
Using new code snippets for AIR and mobile
00:00Flash added a few extra snippets for AIR and mobile applications.
00:06You can find them in the Code Snippets panel.
00:08If you scroll down in the Code Snippets panel, you will see Mobile Touch Events,
00:12Mobile Gesture Events, Mobile Actions, AIR for Mobile, and AIR.
00:17You can expand these to see the various snippets that you can add in, including
00:22in the AIR section, Click to Read from a Text File.
00:25So you will see that we have many new and useful actions for building mobile and AIR apps.
00:33As an example, we will click on the skateboarder on the stage and then in the
00:38Code Snippets panel, I am going to double-click in the Mobile Actions section, the
00:42Move with Accelerometer snippet.
00:45Once the Actions panel opens with the snippet inside,
00:48you will see all the code.
00:51Now if I were to test this on a mobile device, I would see that the kid on the
00:55screen moves along with accelerometer data.
00:59So, in two clicks I can do something as complicated as moving an object on the
01:04screen based on the accelerometer.
01:07Using the new AIR and mobile features, you can more quickly develop
01:10applications in Flash.
Collapse this transcript
Viewing the new code snippets HUD
00:00In addition to having more snippets to work with, the Code Snippets panel also
00:05gives you more information about each individual snippet.
00:09If I open the Code Snippets panel and single-click any snippet, you will notice
00:14on the right side you will see an info button and then a code button.
00:19Now this heads-up display, or HUD, gives you information about each code snippet.
00:26So if I click the little information icon then I can see information about the
00:30code snippet in plain English.
00:33If I click the code icon, I can see an example of the code, and then of course
00:38click the button to insert the code into my app.
00:41Now of course this works for any of the snippets existing in the Code Snippets
00:46panel, so you can quickly get the information. And you don't have to worry that
00:50you accidentally put the wrong one in before committing to it.
00:53So you can click through all of them, make sure you are selecting the right one,
00:57and then click to insert.
00:59And if you want to hide this Code Snippets heads-up display, you can just click
01:03the Options button and it will go away.
01:07The new heads-up display serves you by enabling you to speed up your workflow
01:12so you make less mistakes and you can use code snippets more efficiently.
Collapse this transcript
Loading assets with the new ProLoader class
00:00The ProLoader class was introduced to overcome problems that occurred when you
00:06would use the loader class to load a SWF file that contained a TLF text field.
00:11Let's look at how the Loader class works.
00:14Select a skateboarder on the stage and in the Code Snippets panel,
00:18double-click the Click to Load or Unload SWF or Image snippet.
00:23The code is then written and if you scroll through it, you can see that
00:28it's pretty simple.
00:30The ProLoader class works a lot like the Loader class.
00:33You create a new instance and you load in a URL request.
00:36So let's change that URL request to something local.
00:39TLF in all caps .swf.
00:43There is my URL, and then I am just going to test this in the Flash Player, and so
00:49I have my skateboarder on the screen and I can click him and the SWF file loads,
00:54including a TLF text field.
00:57So using the ProLoader class, you can load SWF files that contain TLF text fields.
01:03If you are interested in learning more about the ProLoader class, you can look
01:07it up in Flash Help.
Collapse this transcript
Understanding Flash Player premium features
00:00As a Flash instructor, I'm often asked, Whatdo you think about the future of Flash?
00:06And I've been saying for a long time that Flash is an excellent platform for developing games,
00:12particularly browser-based games.
00:15Adobe has published a document that explains the future of Flash, where they plan on moving
00:22the Flash platform. Unsurprisingly, they're going to be focused on video and gaming.
00:29In the gaming realm, there is a new feature called Stage 3D, which you can read about @Adobe.com/devnet/games.html.
00:40Stage 3D is a new hardware-accelerated way to develop 2D and 3D games. Now just at this
00:48site, you can see a lot of resources for developing games using Flash. Stage 3D is just one of
00:55them. Some of these features are free and some of them are Premium Flash Player features.
01:02So if you scroll down a little bit, you will see a section called Game development in detail.
01:08In here there is a section called Gaming engines, 3D and Stage 3D. So I am going to
01:13click that. Here we will see a list of some first-party and third-party engines for creating
01:21games. So there are many lists and links to download the frameworks.
01:24You could see that Adobe is very committed to gaming using the Flash platform. But I
01:30encourage you to go to Adobe's devnet and to look at the different gaming engines they
01:35have available and see how much easier it is now to create a game with Flash. So if
01:42you love Flash, now is the best time to get into developing Flash games, because there
01:48are more tools than ever to make high-quality games using the Flash platform.
Collapse this transcript
Conclusion
Goodbye
00:00Well, you've reached the end of this course.
00:03I hope you feel more comfortable using ActionScript in Flash CS5.
00:09If you're eager to learn more, there are plenty of ActionScript titles in the
00:12Online Training Library.
00:14So, 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 98,648 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,896 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