How to Add Action Scripts in Flash

There are a number of ways you can add ActionScript code to a Flash file. For example, you can store ActionScript in files that are separate from your Flash source file, then import them when the Flash file is exported and viewed. However, the most straightforward technique is to include ActionScript directly within the Flash document. Using ActionScript in Flash is not generally difficult for basic functions. Even with no programming experience, you can learn to include small amounts of code and build on these as your Flash skills develop.

Instructions

    • 1

      Create a new document in Flash by selecting "File" and "New." Create a new layer in your Flash movie by pressing the "Insert Layer" button at the left-most end of the Timeline near the top of your screen, or choosing "Insert," "Timeline" and "Layer." The new layer appears in the Timeline area. Rename it by double-clicking the part with the text "Layer 2" on it, then entering "actions" to indicate that this is where your code will go.

    • 2

      Open the Actions panel in your document with the new "actions" layer selected. To open the Actions panel, press "F9" or choose "Window" and "Actions." The Actions panel opens, displaying a white area where you can enter code. Click within this area and enter the following test code:

      trace("Hello");

      Export and test your movie by choosing "Control" and "Test Movie." The Output window opens, with the "Hello" text string you entered written out to it. This is a tool for testing your ActionScript code, so you can write anything you like using a "trace" statement, and it appears in the Output window.

    • 3

      Add a Symbol to your file. Close the Actions panel by clicking at its top. Select "Layer 1" on the Timeline and create a shape on the central white stage area. Use the drawing tools on the left of your screen to create your shape. For example, draw a rectangle by clicking the "Rectangle Tool," then clicking and dragging an area on the stage. Once your shape is drawn, select it by double-clicking it, then convert it to a Symbol by choosing "Modify" and "Convert to Symbol." Choose a name and select the "Movie Clip" radio button, then press "OK."

    • 4

      Give your Symbol an Instance Name. With your new Symbol still selected on the stage, open the Properties panel by choosing "Window" and "Properties." In the "Instance Name" text field, enter "mysymbol_mc" or any other name you prefer, as long as you use the "_mc" suffix. Giving your Symbol an Instance Name allows you to refer to it in your ActionScript code, so the name should be unique within your file.

    • 5

      Add interactivity to your Symbol. Select your Actions layer in the Timeline, then open the Actions panel again. Enter the following code, amending it if necessary to reflect the Instance Name you chose:

      //set up a listener method for clicking

      function symbolClicked(event:MouseEvent):void

      { trace("Symbol clicked!"); }

      //add the listener to the symbol

      mysymbol_mc.addEventListener(MouseEvent.CLICK, symbolClicked);

      Test your movie by exporting it again and clicking the symbol. You should see the text "Symbol clicked!" written to the Output window.

Tips & Warnings

  • Using the correct ActionScript Instance Name suffixes such as "_mc" allows Flash to prompt you with suggested code while you type.

  • Keeping ActionScript code in various locations can make development difficult, but using a dedicated layer can help.

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured