Introduction to ActionScript 2

What is the ActionScript?

The ActionScript is the programming language that Macromedia Flash has used from the very beginning, and that Flash 8 uses too. In general, we can say that the ActionScript will allow us to make everything what we propose with Flash 8, since it gives absolute control over everything related to a Flash movie, absolutely everything.

However, in these two themes we are only going to see a small introduction to ActionScript that will serve to set the bases that will allow us to begin working with ActionScript. Teachinfg programming with ActionScript would require another complete tutorial. Refining the understanding of this language is up to the reader. We recommend to follow the wonderful Help included in Flash 8.

All that belongs to this chapter makes reference to 2nd version of ActionScript, the latest version of this programming language presented by Macromedia and later incorporated in Flash MX 2004.

General characteristics of the ActionScript

As we've already commented, the ActionScript is the programming language typical for Flash, like Lingo is for Macromedia Director, for example. The ActionScript is based on the ECMAscript specification ECMA-262, as other languages like Javascript.

The ActionScript is, as its name indicates, a scripting language, this means that to obtain results the creation of a complete program will not be necessary, normally the application of ActionScript fragments code to the existing objects in our movies allow us to achieve our objectives.

The ActionScript is a programming language oriented to objects (OO), and therefore has similarities with languages such as the used in Visual Microsoft BASIC, Borland Delphi, etc... although, evidently, it does not have the power of a language purely OO derived from C or Pascal like the previous ones, each version approaches more to a language of this type. The version 2.0 recently released in Flash MX 2004 is more powerful and "is more OO" that its previous version 1.0

The ActionScript is very similar to the Javascript; if you know Javascript, the syntax and the style of ActionScript will seem imediately familiar to you. You can find the differences between both languages in the help that accompanies Flash.

In most cases, it will not be necessary "to program" indeed, Flash places at our disposal an impressive collection of "functions" (we'll understand soon functions as ActionScript code that makes a certain function) already implemented that do what we looked for. It will be enough to place them in the suitable place.

We are going to see many of these functions in this tutorial, but before we recommend to assimilate well certain concepts related to programming. For it, take look our basic theme

Introduction to the Programming with ActionScript

We've catalogued this theme as "basic", but the suitable word would be "necessary". Although the objective of this theme is not teaching programming, we can't overpass certain concepts related to it; they must be known to be able to understand the ActionScript. Then, we are going to explain some fundamental concepts, necessary to approach the programming by ActionScript.

Script: Code written in any programming language that implements a certain task. For example, to go to the next frame when pressing a Flash button, the necessary ActionScript code is a script.

Variable: We are going to explain the interpretation of the variable term in programming by a simple example.

If we have the expression:      x = 3;

We have that x is a variable that takes the value 3. It is called variable because it can change its value at any moment during the execution of our Flash movie. All the data, which are normally handled are variables (except some constants, for example, the number 3). So, in this way, a name is a variable (of a string of characters type), a last name would be also a variable, as well as the address, the telephone etc...

Expression/Sentences: Set of operators, constants, variables or actions. They cause a determined result or a certain action and ALWAYS must be ended by ';' (point and comma).

Function: A function is a portion of code that has a certain name and that does a concrete operation. For example, the following definition of function:

function IncreasesX(x) {

x = x + 1;

}

In this function we take the variable value "x" and add "1" to its value. If we want to execute it in some place of our movie, it would be enough to write: "IncreasesX(variable)".

The definition of the function (that is the code shown above) must be written BEFORE calling it.

Action: Flash calls Action to the built-in functions that do not need predefinition to be used. Therefore it is enough to write the call to the function. These actions will be used mostly because they are extremely easy in use and very powerful. The actions appear in the Panel Actions and they can be inserted in our code by a simple click of mouse.

For example "gotoAndPlay(3)" is an action that causes that Flash starts executing the frame 3.

Now that we are familiar with the function definition, we'll also have to understand, that Flash has defined somewhere the function "gotoAndPlay" (as we have done previously with the function "IncreasesX") and written a code that causes the commented effect. Luckily all this has not to worry us, Flash is in charge of everything. It is enough to know the actions and how to use them.

Parameter: A parameter is a variable that is an input or an output for an action or a function. See an example to clarify this definition:

We have the following definition of function:

function Sum5(p_input, p_output) {

p_output = p_input + 5;

}

Let's imagine that we have a variable x that it is equal to 7 and another variable y that we want to be equal 7 + 5. We would execute the function "Sum5" in the following way: Sum5(x, y). We are going to see what exactly makes the function with the parameters x and y.

When executing "Sum5(x, y)", Flash searches for the function definition that are called Sum5, it will immediately find the definition that we've written above, (that must be written in some part of our movie BEFORE the execution of "Sum5(x, y)"). When it is done, Flash verifies that the executed function COINCIDES with the head of the definition; this head includes the name of the function and all that comes with the parenthesis. In our case, the head would be "Sum5(p_input, p_output)"

The NAME of the function and the number of parameters MUST coincide, these ones will be variables or values separated by comas.

As we have 2 parameters in each side, everything coincides and is passed to execute the function. The executed function will be "Sum(x, y)", and so its execution will be:

function Sum5(x, y) {

y = x + 5;

}

After executing this function, y will take value of x plus 5. That is just what we wanted.

The variable 'x' has acted as an input parameter, because it contributes a data to the function, the value ' y' starts being passed into the function, but it is an output parameter because of being modified INSIDE the function and then returned back.

Object Oriented Programming (OOP): ActionScript is an OO language; this means that the information is organized in groups designed as classes. When we want to use a class in our movie, we use an instance of it, named OBJECT. The objects, and thus, the classes have Properties (characteristics) and Methods (behaviors). Let's give 2 examples to clarify this:

The Person object has:

        - Properties: Name, age, height...

        - Behaviors: speak, run, jump...

- the Movie Clip object has:

        - Properties: color, width, height...

        - Behaviors: gotoAndPlay, Stop, Play, LoadMovie...

Obviously the first object is just didactic; the second object is an object of Flash, and probably the most important...

We can find the objects also in the Actions Panel, its use is easy. Let's see an example:

We have the object Clip1, that is an instance of the MovieClip Class and so far, it has the same Properties and Methods.

- Clip1._height = 20;

With the previous line, we are saying to Flash that the object Clip1 has a height of 20 (Flash will immediately modify it in the movie). The syntax of Flash establishes that the separator "._" must exist, it is not worth describing why. It will be always, thus we'd better remember it.

- Clip1.Play();

This action will implement the Play method, which belongs to MovieClips, and causes the timeline start from the Clip1. You must notice that here the separator is not "._" but "."

We already know the Flash 8 "basic" concepts. If we think about that, we can modify the height of a movie object with only 1 line IN RUNTIME MODE (and not while we created it as up to now).

We will be able to give more live to our animations and to obtain a total interactivity with the user. We will be able to obtain to a complete multimedia movie!

The knowledge and understanding of these concepts are not a requirement to start programming with ActionScript, an intuitive programming is a very common practice in this type of languages.

In this tutorial we recommend to know what is being made at each moment and what every item means; the results will constantly justify the initial effort during our learning in ActionScript, with clear understanding all the above and some work, we will soon become expert programmers and then the intuition will stop being useful...

The Actions Panel

In Flash, the Actions Panel is used to program scripts with ActionScript. It means, that everything what we introduce in this Panel will be reflected later in our movie. We must know clearly from the beginning that the Actions Panel can make reference to Frames or objects, so that introduced ActionScript code will affect only to what is refered in the Panel. For example, in the below image, it is possible to distinguish that the Actions Panel refers to Frame 1 of the Layer 1.

The Actions Panel is divided in 2 parts, on the left we have a help facilitated by Flash that gives us access fastly and very comfortably to all the actions, objects, properties etc... that Flash has predefined. These elements are divided in folders, that contain more folders classifying effectively everything what Flash places at our disposal. To insert them in our script it will be enough to double click the chosen element.

Later we'll see in detail the different elements from this Panel.

Click to enlarge
Click to enlarge

On the right side we have the space to place our script, here will appear what we are inserting. It also includes utility tools, like the search of words, the possibility of inserting break points, the tool to Review Syntax and the Flash help for ActionScript.

The Actions Panel of Flash 8, in contrast to the one Flash MX 2004 one, has two edition modes, so it has much more in common with the Expert Mode of Flash MX and its Wizard Mode.

This freedom type is total in the Expert mode and therefore, it is also the possibility of commenting errors to ensure that our script is correct, while entering Expert Mode an icon will appear as: When pressing it Flash reviews our code looking for possible errors, indicating us, in its case, the line that presents the error and in what it consists.

It is a common error to waste hours looking why our movie is not working correctly while the reason is that a syntax error disabling all the code existing in a frame, that acts like if there were no CODE in it. Let's keep in mind this and review the code conscientiously.

Click to enlarge
Click to enlarge

The uso of the Assist mode is much more simple. To use it press the Script Assist button and select the commands in the left that you want to include. Their options will be displayed on the right and you only will have to fill them in order of the command to work propperly.

Even though it is recommendable to check the code so we get used to it and then will be a matter of time you could write your own code without the assistant.


Close    To Top
  • Prev Article-Flash:
  • Next Article-Flash:
  • Now: Tutorial for Web and Software Design > Flash > Basic > Flash Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction