Adobe 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.

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.

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 that you create. The stop action is ActionScript you need to know about when you start using Flash so you can stop looping SWF files when necessary.

Before you add the code, you need to 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 a way of targeting the button from the timeline. The first step is to assign the invisible button an instance name, and then you add code that targets that button using its name.

  1. Select the invisible button on the Stage.

  2. Open the Property inspector (Window > Properties), and find the Instance Name text box in the Property inspector.

  3. Type inv_btn into the Instance Name text box.

    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 you can use underscores. Also, your instance names are case-sensitive.

  4. Select Insert > Timeline > Layer to insert a new layer, and then rename the new layer to 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 event 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 a lot of additional information on learning the ActionScript language in the Flash 8 help. In Flash, choose Help > Flash Help and find Learning ActionScript 2.0 in Flash in the Table of Contents.

  7. Select File > Save to save your progress before moving on.

    After you finish saving the file, proceed to the following exercise, Test the application.