Unit 18. ActionScript Examples (I)

Examples of use of the ActionScript code


We are going to show the most characteristic uses of code ActionScript in the different objects existing in Flash MX 2004. This way we’ll understand it better and why not: we’ll save much effort having many of the already prepared codes here to use in our movies (and with the guarantee that they actually work). We’ll show the code and next, its explanation.

ActionScript Code in BUTTONS


  The Buttons (Button object for Flash MX 2004) are always very helpful when we want our movie to interact with the user.

All the codes shown in this section MUST be inserted inside of the Button's type object (Button) which we are working with. For it, we'll select this button and later we will open the Actions Panel, making sure that this does reference to our button.

Let's see some of them:

on (release) {



This action causes that when releasing a button we go directly to the Frame number 15 of our movie.

The first line indicates to Flash that “releasing the button" will do what is between the keys "{...}".

This code is characteristic of the buttons and allows us to control the pressing. Depending on what we place between the parenthesis, the action associated to the button will take place in one or another moment. In this case, we have written, "release" that means that the action will take place, after pressing our button and releasing it.

The second line is the action that will be taken place. In this case the Flash reading head will move to the frame 15.

Example of the code to the left. We get started from a movie with 15 frames and one action stop() placed in each one of them. We've placed the code in the button "Go to the Frame 15".

on (release) {

getURL("http://www.teacherclick.com", "_blank");


This action causes that by releasing a button a new Web page opens in our browser by default and displays the page www.teacherclick.com .

The first line has the same function that in the previous case

The second line is the call to the Web browser itself. The parameters indicate the URL address that we want to show and the window mode that the Web page will contain. In this case, a new independent window of the present one will be opened.

on (release) {



This action causes that by releasing a button the object properties are modified.

The first line has the same function that in the previous case

In the second line we can see the property ._width (width), soon this call is going to access to this property. We see that there is an operating assignation (=); we are going to assign a determined width to an object.

So by releasing the button we are going to modify the width of the object r1, that will be of 350 px.


ActionScript Code in MOVIE CLIPS


  The Movie Clips (MovieClip object) are movies inside of movies. They can have AS code within themselves (like the buttons), although usually some other external code (located in frames) makes reference to them. To make the code contained in movie clips valid, these must have some clip-associated event (Load, KeyDown etc) that determines when this code will be executed (when loading the clip itself, when pressing some key...)

Let's see some of these codes:


loadMovieNum("teacherClick.swf", 0);

This AS code located inside of a frame causes that our Flash movie looks for a movie called "teacherClick.swf" IN THE ROOT FOLDER and loads it in the level 0, that is to say, in the same level that the current movie.

The root folder is the folder containing the main movie (the movie that has this code).


loadMovieNum("myphoto.jpeg", 0);

This code allows us to load in a frame an image existing in our root folder with extension JPEG in a dynamic way.

If we had the image in a folder called "Images" inside of our root directory, we could accede to it in the following way:

loadMovieNum("Images/myphoto.jpeg", 0);

The image must be in format JPEG, the format JPG wouldn't respond to this current call.

The following action must be included inside of a button, because it'll happen when clicking on it (in this way the example is more practical)

on (release) {



This action causes that by releasing a button we go to the frame 2 of a certain Movie clip. The frame of the main movie WILL NOT VARY.

The first line is commented previously

The second line is the action that will be taken place. Flash looks for an object called r1 of movieclip type and moves its reading head up to the frame 2. The reading head of the main movie is completely INDEPENDENT of the movieclip reading head and it will not be altered.

We have a movie with different 2 frames.

There are the button and the corresponding movie clip (r1) in the first frame. The movie clip has in its timeline an animation, initially stopped. Let's see what happens if it is acceded to its frame 2.

We aren't concerned about content of the the second frame because the action located to the left will not take us up to there. The reading head of the main movie is INDEPENDENT.

Legal warning: Authorised on-line use only. It is not allowed the use of these courses in companies or private teaching centres.
© aulaClic. All rights reserved. Reproduction in any form whatsoever is prohibited.