Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Using Flash: Writing Simple Actions

Macromedia Flash – Writing Simple Actions

You need to add some simple ActionScript to your banner in order for the invisible button to open a website or send information about how many clicks the banner has received.

There are several different places you can add ActionScript in a Flash document. You can select an instance, and add ActionScript that attaches directly to that instance. To access the code, you would need to find and select that instance again. You can also add ActionScript to a frame (or multiple frames) on the Timeline. It’s a good idea to add all of your code to a single frame on the Timeline because it’s much easier to find, update, and organize when you’re working on a file. Do not attach your ActionScript to instances.

Note: You can also keep your ActionScript in external class files that import into the FLA file you’re working on. This is sometimes the best way to organize your ActionScript, particularly when you work on larger projects. This topic goes beyond the scope of this tutorial, however.

Notice how your Join Us motion tween continually loops when you test it. By default, the playhead on the Timeline loops if you have content on more than one frame. Therefore, if you have content on several frames in a movie clip or on the main Timeline, it will play and loop forever. You can stop the playhead from looping by adding a single line of ActionScript. If you add this ActionScript to a frame, the playhead stops when it reaches that frame:


You don’t need to add this ActionScript to your banner. However, you will need to add this ActionScript to other FLA files you create. The stop action is an example of ActionScript that you should be aware of when you use Flash so that you can stop looping SWF files when necessary.

Before you add the code, give the button a unique instance name. The instance name enables you to target the button with ActionScript code. If you don’t name the button, your code doesn’t have any way of targeting the button from the Timeline. The first step is to assign the invisible button an instance name. Then you can add code that targets that button using its name.

  1. Select the invisible button on the Stage.
  2. Open the Property inspector (Window > Properties > Properties) and find the <Instance Name> text box in the Property inspector.
  3. Type inv_btn into the <Instance Name> text box.

    Note: An instance name is different from the symbols name (which is what you type in the Name text box in the Convert to Symbol dialog box). An instance name cannot have spaces or special characters but can contain underscores. Also, instance names are case-sensitive.

  4. Select Insert > Timeline > Layer to insert a new layer and then rename the new layer as actions.
  5. Open the Actions panel (Window > Actions) and then select Frame 1 of the Actions layer.
  6. Type the following ActionScript into the script pane (the editable text field) in the Actions panel:

    inv_btn.onRelease = function(){
    getURL("", "_blank");

Notice how you target the inv_btn instance in the first line of code. The event is the onRelease in your ActionScript code, which refers to the action when a user clicks and then releases the mouse over the inv_btn instance. Then you tell the button to open a particular web page ( in a new window (_blank) using the getURL() method. Obviously, you would replace this URL with whatever website you want the banner to open. If you want the banner to open the website in the current page, replace _blank with _self.

This is a simple piece of ActionScript code that reacts to a button click. There is much more information available about the ActionScript language in the Flash documentation (F1). Refer to the documentation’s Table of Contents for books and reference guides on ActionScript. A good place to start is Learning ActionScript 2.0 in Flash.