Written by Amanda Kern
Why learn actionscript?
According to Macromedia here are just a few things you can accomplish once you've gained actionscript knowledge:
- Provide a personalized user experience
- Achieve greater control over movie clips and their properties
- Animate elements in your movie programmatically—that is, without using the timeline
- Get data in and out of Flash to create forms, chat programs, and more
- Create dynamic projects that respond to the passage of time or the current date
- Dynamically control sound volume and panning
For beginner's actionscript can be a new frustrating experience that often becomes a hurdle to fully grasping Flash. Remember, it is much like learning a foreign language. Much like it's easy to feel out of place in a country where you don't know the native language, actionscript can be tough to learn. According to Macromedia's article on "Actionscript 2.0's Best Practices" they found that: "Typically, you spend 80% of your development time debugging, troubleshooting, and practicing general maintenance, especially on larger projects. Even when you work on small projects, you'll spend a significant amount of time analyzing and fixing code." Yes, I know this is very reassuring, take this knowledge to allocate enough time for Flash projects - which as you'll come to find doesn't involve just development but also LOTS of testing.
Getting Started with Actionscript
In Flash there are three main ways we can apply actionscript to our movies:
- By typing directly into the actions panel
- By using script assist from the actions panel
- By using behaviors (generally only enables you to add common actions). To apply behaviors use the behaviors panel.
There are three main ways to access the actions panel:
- Using the menu choose Window > Actions
- Option + F9
- With a frame selected choose the circle containing an arrow on the right side of the properties inspector
Your actionscript can be applied to two different sections in Flash:
- Objects (i.e. buttons, movie clips, components). By applying a script directly to an object the user generally must interact with the object for something to occur.
- Frames. When applying actions to a frame it will tell your movie to complete a certain task once reaching a specific frame the movie.
Behaviors are available for many of the most common scripts you might use in Flash. They are super simple for beginner's to use. Always pay attention to whether you are applying actionscript to an object or a frame. If you apply the script to the incorrect location it won't function properly.
Using the Actions Panel
To start familarizing yourself with actionscript we might want to make sure you can navigate your way around the actions panel. So let's take a look at a few points to note about the actions panel that will help make your first experiences a bit easier.
Important points & terminology to note about Actionscript
Let's take a bit of time to point out some important points that will help when we use actionscript.
- Instance Names are names we can give to our symbols so that we can refer to them with actionscript. It's recommended that you use suffixes such as _mc (for movie clip), _txt (for text), _btn (for buttons), because it will assist tremendously with code hinting. Instance names should be named without spaces, or special characters, also never begin with numbers.
- Don't name variables, functions, labels or instances keywords that flash uses such as delete, else, static, new, void, etc.
- A Semicolon is similar to a period in a sentence, and appears at the end of most lines of script. completes an action.
- Curly braces signify an action or a set of actions. Basically it tells Flash what to do when specific events in your Flash movie occur.
- Dot syntax is Flash's way of putting objects, methods, and properties together to tell a Flash movie how to complete specific tasks. Flash uses "dots" or periods between objects, methods, and properties.
- Parentheses are generally used to set parameters, also referred to as arguments or values
- Quotation marks denotes text information. It is the only way for flash to differentiate from numbers and words. Text is often referred to as strings.
- Operators - =, <, >, +, -, *, etc. Connects 2 elements of scripts in various ways.
- Events are things that occur in your movie that control aspects of your movie. Generally events are things the user must do while interacting with your movie. example = (release)
- Event handlers are special actions that manage events such as on and onClipEvent.
- Methods are what make your actions "go", they can be compared to verbs in a sentence - it's what Flash will actually do. Examples are gotoAndPlay, getURL, and play.
- Comments are notes to yourself or others that might edit your project.
//is for one line
is more than one line */
- Formatting in Flash usually consists of indenting and spacing to help make things easier to read as you script.
- Case sensitivity means that if you name something it should always be referred to in the same "case" as named. For instance, naming a frame label "animation" is different from "Animation". It also refers to some of Flash's scripts such as gotoAndPlay or getURL. You must capitalize gotoAndPlay or getURL or they will not function.
Breaking down a script
As you're probably beginning to notice, actionscript is a really complex language to learn. It'll help if we take a closer look at a script you'll probably use often.
Do make sure you note all the syntax to include dots, parenthesis, curly braces, quote marks, and semi-colons. Neglect the syntax and you'll quickly get frustrated with actionscript. Actionscript rewards those that are great at proofreading!
So what makes Flash complex is not just actionscript - it's mastering all those timelines you'll be using! Remember, we can target movie clips or buttons if we give them an instance name - this can quickly become confusing if we don't get a better understanding of timelines. So let's take a look at how Flash refers to timelines:
- _root is the main timeline of your project. This is the default timeline you use in Flash. And yes, you DO need the underscore prior to root.
- this refers to the current timeline. If you are attempting to control objects in a timeline you are working in use this. Don't ask why there's not an underscore - just be sure to omit when using this.
- _parent refers to nested movie clips. As you begin to use multiple movie clips it's highly likely that you'll start nesting movie clips and you just might need to refer to a movie clip that is holding a nested movie clip. _parent refers to the movie clip that is nesting another movie clip.
Troubleshooting your scripts
It's impossible to develop a Flash project without errors. Errors are often typos, case sensitivity related, or forgetting to give your movie clips instance names. What's great is that Flash will alert you if it notices any syntax errors. Let's take a closer look:
So what do you do when your script goes wrong (because it will at some point)? Patience is certainly the best medicine for debugging Flash movies. Here are a list of some common errors for beginners:
- Pay attention to exactly where you are applying your script. Beginner's often apply their script they intended to use on a button on a frame instead. Flash does seem to highlight objects when a frame is selected so it's easy to think your button is "active" when it's really not. The best advice is to verify at the top of the actions or behaviors panel prior to adding your script. You'll know you've added script to a frame if an "a" appears.
- Give Movie Clips you are trying to target an instance name. To assign an instance name select and add the instance name in the property inspector.
- Use Frame labels to target rather than Frame numbers. It's easy to get mixed up if you use frame numbers because you'll likely need to move the timeline around.
- Use consistent naming conventions. By naming consistently you'll reduce the risk of errors. Remember, no spaces, special characters, don't name as keywords, and don't start with a number.
- Case Sensitivity. If you use uppercase always refer to it the same when writing your script.
- Name a frame label or instance name only once. If you think about it if you had two movie clips with an instance name of "gallery_mc" Flash wouldn't have a clue how to figure out which movie clip was which.
- Don't forget all the syntax. That means quotes, parenthesis, curly braces, semicolons, etc. If they're needed don't leave them out.
When troubleshooting it's easy to get frustrated. Make your best attempt to solve problems but even the best developers will advise you that sometimes the best solution is to take a break and walk away from the script and come back to it.