7/30/2019 Introduction to ActionScript
1/16
Introduction to ActionScript
What is the ActionScript?
The ActionScript is the programming language that Macromedia Flash has used fromthe very beginning, and that Flash 8 uses too. In general, we can say that the ActionScriptwill 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 completetutorial. Refining the understanding of this language is up to the reader. We recommend to
follow the wonderfulHelp 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 inFlash 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 theapplication 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 alanguage 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 thedifferences 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 understandsoon functions asActionScript 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.
http://www.ecma-international.org/http://www.ecma-international.org/7/30/2019 Introduction to ActionScript
2/16
We are going to see many of these functions in this tutorial, but before we recommend toassimilate well certain concepts related to programming. For it, take look our basic theme
.
The Actions Panel
In Flash, the Actions Panel is used to program scripts with ActionScript. It means, thateverything what we introduce in this Panel will be reflected later in our movie. We mustknow 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 thatgives 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 foldersclassifying 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.
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 insertingbreak 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
http://www.teacherclick.com/flash8/b_17_1_1.htmhttp://www.teacherclick.com/flash8/b_17_1_1.htmhttp://www.teacherclick.com/flash8/b_17_1_1.htmhttp://www.teacherclick.com/flash8/b_17_1_1.htmhttp://www.teacherclick.com/flash8/b_17_1_1.htm7/30/2019 Introduction to ActionScript
3/16
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 ifthere were no CODE in it. Lets keep in mind this and review the code conscientiously.
The uso of the Assist mode is much more simple. To use it press the Script Assist buttonand 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.
The Actions
The Actions are predefined functions of ActionScript, in other words:Flash creates them, and we must only use them like it is indicated. We mustnot even define functions with the same name. The important thing is that they
are ready for being used and that they ease the use of this programming
language and, moreover, makes us start programming quicker.
Like in the previous case, we will explain the most important Actions.
For a more complete reference, we recommend to look at the program help.
We'll define the actions writing its head (name + parameters with a
generic name) to explain later the meaning of each parameter .
Actions - Movie Control
These actions are used, as its name indicates, to control the stream of our
7/30/2019 Introduction to ActionScript
4/16
movie, this means, to indicate to Flash at every moment what frame has toshow, when has to stop, where it must continue etc... Let's see them and we
will understand it better:
gotoAndPlay / goto: This action will be, probably the most used when
producing your movies. The action consists in moving the reading head to the
frame that we indicate to it. The reading head determines what frame of ourmovie is reproduced at every moment. If, for example, we move it from the
frame 1 to the 25, which we will see suddenly the frame 25 and the movie will
continue playing from there.
Use:
gotoAndPlay(scene, frame):
scene: It is name of the scene to which we want to send the reading head. It
must go between double quotes.
frame: Number or name of the frame where we want to send the reading
head. If it is a name, it must go in double quotes, if it is a number, ITDOESN'T.
Example:
gotoAndPlay("Scene2", 7); --> This action takes the reading head to frame 7
of the scene called "Scene2".
Play: It begins the reproduction of the movie, generally because something
has stopped it.
Use:Play();
It does not have Parameters.
Stop: It stops the reproduction of the movie. It is possible to use it in a
frame, when we want to stop the movie in it (because it is a menu, for
example).
Use:
Stop();It does not have Parameters.
Actions - browser/Network
These actions have several functions, we'll describe the most important:
fscommand: This action is able to execute certain very powerful
7/30/2019 Introduction to ActionScript
5/16
commands. The possible commands that it admits are:
- fullscreen: It makes our movie to take the full screen mode. It is very
useful for presentations in CD-Rom, for example.
- allowscale: It controls the change of parameters of the objects inserted in
the movie when the user stretches the edges of this one (or the page Web inthat it is) We want to maintain the proportions? This command allows us to
control it.
- showmenu: If you have seen the menu that appears when pressing the right
button of the mouse on a movie Flash, surely you have thought about making
it disappear... Executing this sentence correctly, we will be able to hide it.
- trepallkeys: It serves to detect all the keys pressings during the
reproduction of our movies.
All these alternatives, share its use mode, let's see it:Use:
fscommand("command","true / false")
command: The command to execute (fullscreen, allowscale, etc...)
true / false: Here we must write true or false, according to our will to
deactivate or to activate the option.
Example:
fscommand("fullscreen", "true"); -> It activates the full screen mode.
getURL: This action is used to open a Web browser and the Web page we
would like to.
Use:
getURL(url , window , "variables")
url: Web Address we want access (a window will be opened).
window: OPTIONAL Parameter. Mode in which we want to open the
window (in the current window (_self), in another new (_blank) etc...)
variables: OPTIONAL Parameter, can have several. If the page allows it (ifit is ASP, PHP etc...) we can send to it variables.
Example:
getURL("http://www.teacherclick.com", "_blank");
loadMovie / loadMovieNum: This action allows to load new movies of
Flash or images dynamicaly in our movie (the movie will be loaded only when
7/30/2019 Introduction to ActionScript
6/16
we indicate).
Use:
loadMovieNum(url , level / destination, variables)
url: Absolute address where the SWF movie or JPEG image is located
level / destination: Level where we will load the movie, considering that thebasic level is the 0. Each superior level is located in front of the previous one
and takes the control.
variables: OPTIONAL Parameter. We can send variables.
Example:
loadMovieNum("MyMovie2.swf", 0) --> We loaded the movie
"MyMovie2.swf" in the main level. We did not send variables.
Actions - Conditions
These actions serve to control the logic of the movie. We can say that they
allow "to communicate" with Flash to indicate him what it must do before
different situations.
For example, now that we know many Actions, but we do not know how tosay to Flash "If this happens, do something, and if not, do the other...". Let's
see how to say it:
if ... else: If we suppose that the literal translation of if is "if..." and the one
of else is "else ...", we suddenly find out that it really is saying: "if(passes a
condition) {do this} else {do the other} "Let's see its Use to understand it better:
Use:
if (condition) {sentence1...} else {sentence2...}
if: It indicates that the action that comes next is conditional
condition: It indicates a condition that MUST to be fulfilled to make theactions indicated in "sentence1" happen. If these are not fulfilled, then what
happens is the thing specified in the actions indicated in "sentences2".
In order to fulfill the condition, it must have a result oftrue, or the one that
is the same thing, 1. The importance of the comparison operators and the value
that give back comes from here.
sentences1: Set of actions that will happen if the condition is evaluated like
true. If there is more than 1, they must be included all BETWEEN THE
KEYS
else: It specifies the alternative if the condition is evaluated as false. It
7/30/2019 Introduction to ActionScript
7/16
is OPTATIVE. If it doesn't exist, and the condition is not fulfilled, nothing
would happen, because we have not specified it.
sentence2: Set of actions that will happen if the condition is evaluated likefalse. If there is more than 1, they must be included all BETWEEN THE
KEYS
Example:
if (x == 2) {gotoAndPlay(6); } --> If variable x is equal to 2, then we jump
to the frame 2, if not, we don't do anything
if (y > 7) { Stop(); } else {gotoAndPlay(1); } --> if the variable and is greater
than 7, we stop the movie, if not, we return to the frame 1.
Examples of handling Sounds with ActionScript
Although the object idea usually does not make us to think about a sound, in Flash 8the sounds are also objects and we can handle them using ActionScript properly. Let's see
some very common codes and one complete example of how to use them:
/* Code 1 */
music = new Sound();
music.loadSound(" sound.mp3" ,false);
These 2 lines load a sound and give it name.
The first line specifies to Flash that we plan to create a new object of Sound type and that
it will be called "music".
The second line loads a sound from our hard disk called "sound.mp3" and loads it insideof the music object. The second parameter "false" indicates that it is an "event" sound, and,
therefore, it will be completely loaded before beginning to playback.
/* Code 2 */
music.stop();
This code stops the "music" sound at once.
/* Code 3 */
music.stop();
music.start(0,99);
The first line as we have already seen stops "the music" sound.
The second line causes that "the music" sound begins to playback (start) from its initial
position (the 0 indicates the time from the start in seconds) and it repeats 99 times (this is
7/30/2019 Introduction to ActionScript
8/16
called loop or circle)
/* Code 4 */
music.stop();
music.start(0,0);
This code stops "the music" sound and reinitiates it later, reproducing it only one time.
Now as we already learn to control the sounds by ActionScript, see an example that join
all seen previously.
In this example:
- We have only one frame with 3 buttons. In this frame we've inserted the Code 1.
- The 3 buttons have different functionalities:
- In the red button, it is inserted the Code 2
- In the blue button, it is inserted the Code 3
- In the green button, it is inserted the Code 4
Examples of ActionScript in abstract objects. The MATH object
As we already know, the nonvisible objects also are controlled by ActionScript. We
plan to see some examples of the Math object operation and how to take advantage of it.
x = Math.random();
The "random" Math object Method generates a random number between 0 and 1. In this
case, we store the result in variable x to be able to use it later...
There are many applications of this methods to generate secret keys, passwords, lottery
numbers etc...
x = Math.round(4,3);
The "round" Method ROUNDS OFF the introduced parameter eliminating its decimal
part.
In this example x takes a value 4.
x = Math.max(5 , 2);
7/30/2019 Introduction to ActionScript
9/16
The Method "max" takes the maximum value between 2 numbers.
In this example x takes a value 5.
The Math object is very useful and saves much work, because there is many operationsthat respond to some of their methods and we do not have to implement them. It is enough
to look for it in the manual and use them.
Creating a loader or preloader
We plan to analyze a loader or preloader code to finish strengthening ourActionScript knowledge:
The loaders or preloaders are only necessary when the movies acquire a considerable sizeand it is impossible to display the movie without having it totally loaded (because it blocks
itself, incomplete parts appear etc...). We suppose then, that weve a movie of 150 frames.
We will reserve the 3 first to create our loader. The movie begins from the Frame 4 ...
NOTE: Next to each line we will insert comments (text between the symbols /* and */)
that are lines that Flash recognizes as such and that it ignores at the time of code executing(as though if they did not exist). They are used to clarify and explicate the code. We will
change their color so that they stand out still more. Evidently they are dispensable in the
code that we will insert finally in our movie.
/* Frame 1 */
bytes_total = getBytesTotal(); /* We find the size of our movie with the Action
"getBytesTotal()" and we store it in the variable bytes_total. */
/* Frame 2 */
bytes_loaded = getBytesLoaded(); /* We find the bytes that we have loaded in memory
until the moment. We assign its value to the variable bytes_loaded*/
if (bytes_loaded >= bytes_total) { /* This is the logic of the loader. If we keep the same
number of bytes or more bytes than the movie occupies loaded in memory, we execute thefollowing line*/
7/30/2019 Introduction to ActionScript
10/16
gotoAndPlay(4); /* If we've arrived until here it is because all the movie is loaded in
memory (bytes_loaded >= bytes_total) and we can begin to see the movie. Weexecute gotoAndPlay(4)that will take us up to the frame where the movie begins. */
}
else { /* If we've notyet loaded all the movie */
percentage = ((bytes_loaded/bytes_total)*100); / * We find out the percentagethat we've
obtained dividing the bytes_loaded by the bytes_total and multiplying by 100*/
txt_exit = Math.floor(percentage)+"%"; / * We store in the variable "txt_exit" the
percentage that we have, and then we add the "%" symbol. In the main movie we'll have "a
txt_exit" thatwil l show us the percentageof the movie that we get loaded at every moment
*/
}
/* Frame 3 */
gotoAndPlay(2); /* If we arrive to the frame 3 this is because not all the movie is loaded,
otherwise we would be already on the frame 4. As it isnt loaded yet, we return to the
previous frame to see if it already exists (by means ofgotoAndPlay(2);). We'll repeat this
as many times as necessary for that the user's computer to load it into the movie memory. */
Summarizing:
Frame 1: The total number of bytes taken by the movie is calculated in the Frame 1.
Then we go to the Frame 2.
Frame 2: Every time that we accede to the Frame 2, our ActionScript code finds out the
bytes that we have loaded in memory and compares them with the total number of bytes
(that were in the Frame 1 and they do not appear because they do not vary). lf all the movie
is already loaded we go to frame 4 and begin to play the movie, if not go to the frame 3
Frame 3: The Frame 3 will repeat to send the reading head to the frame 2. Making this
cycle, we give time for the computer to load the movie bit by bit, until the moment when itis completely loaded and we go to the Frame 4. The calculation of the percentage is a
"garment" that we afford ourselves, because we easily find out how much movie is loadedand show it on the screen in an elegant way (i.e. in percentage) making the delay to be less
boring for the user.
Frame 4: Here the movie begins... (It will never come back to any of the previous
frames).
7/30/2019 Introduction to ActionScript
11/16
The Objects
The Objects, as we've already seen in the basic theme, are instances of a certain
class. It means that they are representatives of an already defined class.
So, these are objects, to understand it better we will see some examples: a button, a
movie clip, a graphic or a sound... or in other words, almost EVERYTHING is anOBJECT in Flash.
We are going to see the most used objects in Flash and a brief description of each
one of them.
As it has already been explained in the basic theme, each object has a set ofProperties (that we will see later) and some Methods and events, that give
functionality to the objects. When a component of Flash turns into an object,
automatically it acquires at once all the properties defined by Flash for that object
and it responds to the methods and events that it has defined. You can find a list with
all the properties, methods and events of the objects in the Panel Actions.
"Button" Object
The objects Button have 4 states, as we have already seen in the corresponding unit
and they respond to special methods like "OnRollOver", "OnPress"... that will allow
things happen when the user clicks these buttons, passes over them etc...
When we want a designed image behave as a button, it will be enough to convert itinto button (like we explained in the corresponding unit) and we'll be already able to
use the typical events of a button.
"MovieClip" Object
When we need to create a Flash movie inside of another, but we don't want to have
2 separated files, we will have to create a movieclip object. One of its properties is
especially useful and that is that thay have a timeline that runs INDEPENDENTLY
from the timeline of the main Flash movie, which allows us to create as complex andindependent animations as we want (we can create as many movie clips as we want
within others, for example).
"Sound" Object
The sound objects are hidden, and thus, we'll not be able to see how they remain in
the frames, similar to what we would make with a button or a movie clip. We'll have
to control them, therefore, from the Actions Panel and by using ActionScript. Theyhave multiple special methods, very powerful and useful, we can stop a sound, create
a loop, apply sound effects to it etc...
For example, we could create an object of sound type and later make the buttonreproduce the sound when being pressed. (In the following unit some example of
sounds use will be seen).
7/30/2019 Introduction to ActionScript
12/16
"Mouse" Object
The object mouse is one of the Flash objects that is already defined by Flash,
because it referes to Windows mouse (that the user will handle when seeing our
movie). If we use it, we will be able to access the properties of the Windows mouse,
associated effects, detection of its position etc...It is worth underlining that its operation is NOT similar to other objects, because
we can create as many objects as we want and do with them what we want, but the
object Mouse is unique and it acts indepently of our movie. We can say that it is "anexternal" object that lets other parts of the Operating system interact with our Flash
movie. Therefore, it is very powerful.
"Math" (Mathematics) Object
It is one of the multiple "abstract" objects of Flash, neither it is visual, nor it seemsto corresponds to anything existing in the system (like the "Mouse" object). Its
function is very important, because it allows us to use mathematics formulas easily.
In the following theme we will see some example of its use.
"String" Object
It is another peculiar object, because it corresponds to a data type. Strings are
sequences of characters. If we define a sequence of characters like an object of String
type, we'll be able to use the methods implemented by Flash: to select subchains ofletters, search for a certain letter in a word, format the word characters to uppercase
and lowercase etc..
The Properties
http://mostrar%28%27http//www.aulaclic.org.es/flash8/secuencias/s_17_4_1.html',660,500);http://mostrar%28%27http//www.aulaclic.org.es/flash8/secuencias/s_17_4_1.html',660,500);http://mostrar%28%27http//www.aulaclic.org.es/flash8/secuencias/s_17_4_1.html',660,500);http://mostrar%28%27http//www.aulaclic.org.es/flash8/secuencias/s_17_4_1.html',660,500);7/30/2019 Introduction to ActionScript
13/16
The Methods usually are specific in each object, and studying them would require anew complete tutorial, (we recommend to consult the help incorporated in Flash when you
have doubts), but there are enough properties of the objects that are common to many of
them. We are going to see which ones are the most used and what they represent.
In order to use the properties, it is necessary to enter the object name followed by a point( . ) and later the property and its value. The properties always begin with a script down
(_). Some properties can be written without the name of the object to which they refer
ahead, in this case, they will refer to the main movie.
_alpha
It refers to the opacity object that it affects. The opacity can be defined as the non-
transparency. So that 100% of transparency are equivalent to a "0" value of opacity (fully
transparency) or a "0" value of alpha (fully opaque).
_framesloaded
It is the number of frames in a movie clip instance or in the main movie that the systemhas loaded in memory. (If it is used without an object name then we obtain the loaded
frames of the main movie). It is very useful to create loaders or "preloaders"
_totalframes
It returns the number of frames in a movie clip to which it refers. If no name is usedahead, it returns the number of frames in the current Flash movie. It is also used in the
creation of loaders (in the following unit well see these properties)
_height
It returns the object height, in pixels. For example, if we have a movie clip called
"Clip1" and we write "Clip1._height" we will get the height of Clip1. In the same way wecan change it just by doing: Clip1._height=100; (the height of Clip1 will become 100
pixels)
_width
The property is identical to the previous one, but it returns the width of object, in pixels.
_visible
It determines whether the object is visible in our movie or not. If its value is equal to 1,then it is visible, when it is equal to 0, it becomes hidden. It is very useful to make movie
parts disappear at certain moments. For example, if we want that while pressing a button
disappears the movie clip called "Clip2", we will do this: ....... Clip2._visible = 0; ......._x
With this property we obtain the x coordinate of an object, relative to the X-axis. It
serves to find out the position or to assign it dynamically (during the execution of our Flash
movie)
_y
7/30/2019 Introduction to ActionScript
14/16
With this property we obtain the y coordinate of an object, relatively to the Y axis. It
serves to find out the position or to assign it dynamically (during the execution of our Flash
movie).
We will see an example of the use ofActionScript.
First of all, and in order to be able to refer us to a symbol in our movie, we have to give
it an instance name (to which we will refer when coding). For this, select the symbol and
open the Properties Panel.
Replace the text for a name chosen by you. The object will be ready to
treat it.
Suppose we have a rectangle and we have name it r1, we will write this code (associated
to a button) in the Actions Panel to change the width of the former.
on (release) {
r1._width=350;}
Where r1 is the call to the object, _width is the property we want to change and 350 thenew value of the property.
7/30/2019 Introduction to ActionScript
15/16
RAJIV GANDHI COLLEGE OF ENGINEERING AND
TECHNOLOGY
NAME :- U. SUDHA LAKSHMI
REG_N0 :- 12MC0249
DEPARTMENT:- MCA
SUBJECT:- WVP
TITLE:- FLASH 8.0 ACTION SCRIPT
DATE:-03.09.2013
7/30/2019 Introduction to ActionScript
16/16