Beauty & Self
Books & Music
Food & Wine
Health & Fitness
Hobbies & Crafts
Home & Garden
News & Politics
Religion & Spirituality
Travel & Culture
TV & Movies
Where To Put Your ActionScript Code in Flash
|In this tutorial, we will begin to explore the many ways to use ActionScript in your FlashR project. I'm not talking about the many things that you can make happen with ActionScript. I'm talking about how you can add ActionScript code to your Flash movie. |
In the beginning there was Flash. When you created a Flash project, you created an fla file. Then, when you were ready to publish your Flash to the web, you compiled your fla file into an swf file. However, after a few years people began to use ActionScript to enhance their Flash movies. At this point, the question was where to put the ActionScript. At first, programmers added their code inside the fla file. Now that Flash and ActionScript have grown up a little, most programmers find that it is more efficient to keep the ActionScript separate from, but part of, the Flash movie.
Add Code to the Timeline
The most basic method, and the method used most often in the past, was to combine ActionScript as an internal part of the Flash fla file. When writing ActionScript within the Flash movie, you can place the ActionScript on any frame of the timeline. Although there is no established rule about which frame to use, most programmers find it helpful to place their ActionScript code in the first frame on the timeline. To make things even easier, programmers usually create a special layer for their ActionScript, sometimes called the "Actions" layer.
This is the method that I use for most of my beginner's tutorials because it is less complicated than the other methods. For example, in a previous tutorial, we learned how to write ActionScript code that would draw a circle onto the stage.
|var myCircle:Shape = new Shape();|
myCircle.graphics.drawCircle(100, 200, 50);
Let's add this code to a new Flash movie. Start a new Flash project and name it RedCircle.fla.
That's easy enough. But it would be more efficient if we could somehow keep our ActionScript code separate from our Flash fla file. This next method gets us one step closer to our goal.
- First we add a new layer to the Timeline and call it the "Actions" layer.
- Then, we select the first frame of the Actions layer and open the Actions panel.
- Finally, we add the code to the Actions panel and test our movie.
Using an Include Statement
This second method of associating ActionScript with your Flash project is not much different from the method that we just discussed. Basically, we will move the ActionScript from the fla file to a separate file and add an include statement to our Flash movie which will tell Flash to read the ActionScript from that external file.
Now we have our ActionScript in an external file but we are still using the Timeline in our Flash fla file. In the next tutorial, we will take the next step in our journey toward modular OOP code.
- Open your RedCircle.fla file. Click on Frame 1 of the Actions layer and open the Actions panel. Select all the code, right-click and choose Cut from the pop-up menu. We will paste this code into our external ActionScript file. But first we need to create this file.
- From the Menubar, click File – New. From the New Document dialog box, choose New ActionScript file. You will get a new panel, called "Script-1", open in the workspace. Paste the code into this panel.
- Click File – Save As and save this new file as "DrawCircle.as". Remember to save both your RedCircle.fla and DrawCircle.as files in the same folder.
You will notice that you now have two tabs at the top of the workspace. One tab is for your RedCircle.fla file and one is for your DrawCircle.as file. Now that we have our ActionScript code in an external file, we need to tell our Flash movie to read the code from this file. We will use an include statement to do this. Click on the RedCircle.fla tab to go back to the Flash movie file.
- Click on Frame 1 of the Actions layer and open the Actions panel. Add the include statement.
- Test your movie.
| Related Articles | Editor's Picks Articles | Top Ten Articles | Previous Features | Site Map
Content copyright © 2013 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact