Você está na página 1de 13

About scripting in ActionScript

You can start writing simple scripts without knowing much about ActionScript.
All you need is a goal; then its just a matter of picking the right actions. The best
way to learn how simple ActionScript can be is to create a script. The following
steps attach a script to a button that changes the visibility of a movie clip.
To change the visibility of a movie clip
1 !hoose "indow # !ommon $ibraries # %uttons& and then choose
"indow # !ommon $ibraries # 'ovie !lips. (lace a button and a movie clip on the
Stage.
2 Select the movie clip instance on the Stage& and choose "indow # (anels # )nstance
(roperties.
3 )n the *ame field& enter testMC.
4 Select the button on the Stage& and choose "indow # Actions to open the Actions
panel.
5 )n the +bject Actions panel& click the Actions category to open it.
6 ,ouble-click the set(roperty action to add it to the Actions list.
7 .rom the (roperty pop-up menu& choose /visible 01isibility2.
8 .or the Target parameter& enter testMC.
9 .or the 1alue parameter& enter 0.
The code should look like this
on (release) {
setProperty ("testMC", _vs!le, "alse)#
$
10 !hoose !ontrol # Test 'ovie and click the button to see the movie
clip disappear.
ActionScript is an object-oriented scripting language. This means that actions
control objects when a particular event occurs. )n this script& the event is the
release of the mouse& the object is the movie clip instance test'!& and the action
is set(roperty. "hen the user clicks the onscreen button& a release event triggers a script
that sets the /visible property of the object '! to false and causes the object to become
invisible.
You can use the Actions panel to guide you through setting up simple scripts. To
use the full power of ActionScript& it is important to understand how the language
works the concepts& elements& and rules that the language uses to organi3e
information and create interactive movies.
This section e4plains the ActionScript workflow& the fundamental concepts of
object-oriented scripting& .lash objects& and script flow. )t also describes where
scripts reside in a .lash movie.
%!o&t plannn' an( (e!&''n' s)rpts
"hen you write scripts for an entire movie& the 5uantity and variety of scripts can
be large. ,eciding which actions to use& how to structure scripts effectively& and
where scripts should be placed re5uires careful planning and testing& especially as
the comple4ity of your movie grows.
%efore you begin writing scripts& formulate your goal and understand what you
want to achieve. This is as important6and typically as time consuming6as
developing storyboards for your work. Start by writing out what you want to
happen in the movie& as in this e4ample
7 ) want to create my whole site using .lash.
7 Site visitors will be asked for their name& which will be reused in messages
throughout the site.
7 The site will have a draggable navigation bar with buttons that link to each
section of the site.
7 "hen a button is clicked& the new section will fade in to the center of
the Stage.
7 +ne scene will have a contact form with the users name already filled in.
"hen you know what you want& you can build the objects you need and write the
scripts to control those objects.
8etting scripts to work the way you want takes time6often more than one cycle
of writing& testing& and debugging. The best approach is to start simple and test
your work fre5uently. "hen you get one part of a script working& choose Save As
to save a version of the file 0for e4ample& my'ovie9:.fla2 and start writing the
ne4t part. This approach will help you identify bugs efficiently and ensure that
your ActionScript is solid as you write more comple4 scripts.
About object-oriented scripting
)n object-oriented scripting& you organi3e information by arranging it into groups
called classes. You can create multiple instances of a class& called objects, to use in
your scripts. You can use ActionScripts predefined classes and create your own.
"hen you create a class& you define all the properties 0characteristics2 and methods
0behaviors2 of each object it creates& just as real-world objects are defined. .or
e4ample& a person has properties such as gender& height& and hair color and
methods such as talk& walk& and throw. )n this e4ample& ;person< is a class and
each individual person is an object& or an instance of that class.
+bjects in ActionScript can contain data or they can be graphically represented on
the Stage as movie clips. All movie clips are instances of the predefined class
'ovie!lip. =ach movie clip instance contains all the properties 0for e4ample& height&
/rotation& /totalframes2 and all the methods 0for e4ample& gotoAnd(lay& load'ovie&
start,rag2 of the 'ovie!lip class.
To define a class& you create a special function called a constructor function;
predefined classes have constructor functions that are already defined. .or
e4ample& if you want information about a bicycle rider in your movie& you could
create a constructor function& %iker& with the properties time and distance and
the method rate& which tells you how fast the biker is traveling
function Biker(t, d) {
this.time = t;
this.distance = d;
}
function Speed() {
return this.time / this.distance;
}
Biker.prototype.rate = Speed;
You could then create copies6that is& instances6of the class. The following code
creates instances of the object %iker called emma and hamish.
emma = new Biker(3, !);
hamish = new Biker(", !);
)nstances can also communicate with each other. .or the %iker object& you could
create a method called shove that lets one biker shove another biker. 0The
instance emma could call its shove method if hamish got too close.2 To pass
information to a method& you use parameters 0arguments2 for e4ample& the
shove method could take the parameters who and howFar. )n this e4ample emma
s*oves *a+s* 10 p,els-
e++a.s*ove(*a+s*, 10)#
)n object-oriented scripting& classes can receive properties and methods from
each other according to a specific order; this is called inheritance. You can use
inheritance to e4tend or redefine the properties and methods of a class. A class
that inherits from another class is called a subclass. A class that passes properties
and methods to another class is called a superclass. A class can be both a subclass
and a superclass.
%!o&t t*e MoveClp o!/e)t
ActionScripts predefined classes are called objects. =ach object allows you to access
a certain type of information. .or e4ample& the ,ate object has methods ("or
e,a+ple, 'et0&ll1ear, 'etMont*)& that allow you to read information from the
system clock. The Sound object has methods ("or e,a+ple, set2ol&+e, setPan)
that allow you to control a sound in a movie. The 'ovie!lip object has methods
that allow you to control movie clip instances ("or e,a+ple, play, stop, an(
'et345) and get and set information about their properties ("or e,a+ple, _alp*a,
_"ra+esloa(e(, _vs!le).
'ovie clips are the most important objects of a .lash movie because they have
Timelines that run independently of each other. .or e4ample& if the main
Timeline only has one frame and a movie clip in that frame has ten frames& each
frame in the movie clip will still play. This allows instances to act as autonomous
objects that can communicate with each other.
'ovie clip instances each have a uni5ue instance name so that you can target
them with an action. .or e4ample& you may have multiple instances on the Stage
(for e#amp$e, $eft%$ip and ri&ht%$ip) and only want one to play at a time. To
assign an action that tells one particular instance to play& you need to use its name.
)n the following e4ample& the movie clips name is $eft%$ip'
$eft%$ip.p$ay();
)nstance names also allow you to duplicate& remove& and drag movie clips while a
movie plays. The following e4ample duplicates the instance cart)tem to fill out a
shopping cart with the number of items purchased
on%$ip()ent($oad) {
do {
dup$icate*o)ie%$ip(+cart,tem+, +cart,tem+ - i, i);
i = i - .;
} whi$e (i /= num0er,tems1ur);
}
'ovie clips have properties whose values you can set and retrieve dynamically
with ActionScript. !hanging and reading these properties can change the appearance and
identity of a movie clip and is the key to creating interactivity. .or e4ample& the
following script uses the set(roperty action to set the transparency 0alpha setting2 of the
navigation%ar instance to :9
set1roperty(+na)i&ationBar+, 2a$pha, .);
6o7 s)rpts "lo7
ActionScript follows a logical flow. .lash e4ecutes ActionScript statements starting
with the first statement and continuing in order until it reaches the final statement
or a statement that instructs ActionScript to go somewhere else.
Some actions that send ActionScript somewhere other than the ne4t statement are
if statements& do...while loops& and the return action.
An if statement is called a conditional statement or a ;logical branch< because it controls
the flow of a script based on the evaluation of a certain condition. .or e4ample& the
following code checks to see if the value of the number variable is less than or e5ual to
:9. )f the check returns true 0for e4ample& the value of number is >2& the variable alert is
set and displays its value in an input te4t field& as in the following
if (num0er /= .) {
a$ert = +3he num0er is $ess than or e4ua$ to .+;
}
You can also add else statements to create a more complicated conditional statement. )n
the following e4ample& if the condition returns true 0for e4ample& the value of
number is ?2& the statement between the first set of curly braces runs and the
alert variable is set in the second line. )f the condition returns false 0for
e4ample& the value of number is ?92& the first block of code is skipped and the
statement between the curly braces after the else statement runs& as in the
following
if (num0er /= .) {
a$ert = +3he num0er is $ess than or e4ua$ to .+;
} e$se {
a$ert = +3he num0er is &reater than .+;
}
$oops repeat an action a certain number of times or until a certain condition is
met. )n the following e4ample& a movie clip is duplicated five times
8 0#
(o {
(&pl)ateMoveClp ("+yMoveClp", "ne7MoveClp" 9 , )#
ne7:a+e 8 eval("ne7MoveClp" 9 )#
setProperty(ne7:a+e, _,, 'etProperty("+yMoveClp", _,) 9 ( ;
5))#
8 9 1#
$ 7*le ( <8 5)#
=ntro(&)ton
*ever before has a new programming language received so much attention and become
so popular so 5uickly. )n the first year of its e4istence& @ava took the "eb by storm and
became its adopted programming language. Since then& @ava has become the language of
choice for developing both )nternet and intranet applications& and is used for both
business and consumer software development. The @ava phenomenon has captivated the
imaginations of programmers around the world and is leading the way toward the ne4t
era of distributed application development.
@avaAs appeal lies in its simplicity& its familiarity& and the careful selection of features that
it includes and e4cludes. @ava was not designed by a government committee or a cli5ue of
academics. )t shares its spirit with ! more than any syntactical similarities. )t is a
programming language that was designed by programmers for programmers.
>*e ?ava 0o&n(atons Classes
(robably the single most important new feature added to @,B :.C is version :.: of the
@ava .oundations !lasses 0@.!2. @.! is a set of A()s for building the 8D)-related
components of @ava applets and applications. @.! :.: was released separately from the
?@A n 0e!r&ary o" 1998 so that they could be used with the then-current @,B :.:. @,B
:.C integrates @.! :.: as a !ore A() and adds the @ava C, and ,rag and ,rop A()s. The
A()s included with @.! include the following
The Abstract "indowing Toolkit
Swing
@ava C,
,rag and ,rop
Accessibility
These five A()s are introduced in the following subsections.
%!stra)t Bn(o7n' >oolCt (%B>)
)f youAve programmed in @ava before& you know about the A"T. )t provides the capability
to create platform-independent& 8D)-based programs and is a very important contributor
to @avaAs popularity. Any programmer who has written programs using the arcane A()s of
'icrosoft "indows immediately appreciates the clarity& simplicity& and power of the
A"T. *ot only is the A"T a better A() for developing "indows applications& it is a
better A() for programming window-based applications on platforms ranging from 'otif
to +SEC.
The A"T of @,B :.C has been augmented with many new classes and interfaces that add
drawing& printing& and image-processing capabilities& and support the Accessibility& ,rag
and ,rop& and @ava C, A()s.
D7n'
+f all the new capabilities provided by the @.! :.:& one A()& referred to as Swing& has
far-reaching conse5uences for @ava programmers. Swing is the code word used by the
@avaSoft programming team for the ne4t generation of the A"T. Swing e4tends A"T by
supplying many more types of 8D) components& providing :99F pure @ava
implementations of these components& and allowing the appearance and behavior of these
components to be easily tailored.
The new components that are included with Swing include everything from tabbed panes
and fancy borders to sliders and spinners. These new components& in and of themselves&
make Swing an outstanding addition to the @ava A(). The Swing !omponent 8allery&
located at *ttp-EE/ava.s&n.)o+Epro(&)tsE/")Es7n'(o)F)&rrentE)o+p_'al.*t+l& e4hibits
some of these new components. Swing also comes with a great demo program named
SwingSet.
The Swing components are :99F pure @ava. This means that they donAt depend on the
native windows implementation to support them. )t also means that Swing components
are available and consistent across all platforms. Although Swing components are
implemented in terms of the underlying A"T& these components do not use A"T
components. )n fact& many of the traditional A"T components& such as buttons& lists& and
dialog bo4es& have been reimplemented as Swing components. %ecause of this& the A"T
components behave more consistently across different platforms and are capable of
providing additional features not supported by their native windowing platforms.
The most talked about feature of Swing is its support for pl&''a!le looC an( "eel
(P5G0). )f you like to customi3e your desktop& dabble in new color schemes& and do
what it takes to make your windows fit your tastes and needs& ($G. is for you. The
Swing ($G. architecture makes it easy to customi3e both the appearance and the
behavior of any particular Swing control or any group of those controls. Swing also
comes with several predefined $G.s& including the default 'etal $G.& the 'otif $G.&
and the "indows $G.. $G.s for 'acintosh and other platforms are also being
developed.
%))ess!lty
The Accessibility A() is a @.! A() that has been added to @,B :.C. )t provides support
for the use of assistive technologies with other @.! components. Assistive technologies&
such as screen magnifiers and speech recognition systems& are intended for use by
disabled users& but are also valuable tools for the average non-disabled user. These
technologies provide non-standard ways of interacting with software applications. The
Accessibility A() of @,B :.C allows software developers to comply with the .ederal
Hehabilitation Act and Americans with ,isabilities Act.
The Accessibility A() consists of classes and interfaces for incorporating accessibility
features into applets and applications. These classes and interfaces are provided in the
java.awt.accessibility package.
?ava 2@
)f you develop any kind of graphics-related software& youAll appreciate the new @ava C,
A(). This A() provides comprehensive support for two-dimensional drawing& image
processing& graphics rendering& color management& and printing. )t consists of an imaging
model that supports line art& te4t& images& spatial and color transformations& and image
compositing. The model is device-independent& allowing displayed and printed graphics
to be rendered in a consistent manner. The @ava C, A() is incorporated into the java.awt
and java.awt.image packages.
@ra' an( @rop
+ne of the nicer features of most windowing environments that has been conspicuously
missing from @ava is support for drag and drop. Drag and drop is typically used to
organi3e desktops& manage files& open documents& and e4ecute applications. The ,rag
and ,rop A() allows the @,B :.C to provide platform-independent support of drag and
drop. )t supports drag and drop within @ava applications& between @ava applications& and
between @ava and native platform applications. The ,rag and ,rop A() is implemented
in the java.awt.dnd package and is supported by classes and interfaces in other @.!
packages.
Ht*er :e7 Capa!ltes
%esides integrating @.! :.: as a set of !ore A()s& the @,B :.C provides a number of
other new capabilities. These capabilities are covered in the following subsections.
?ava =@5
The !ommon +bject He5uest %roker Architecture (CH4I%) is a standard approach to
developing distributed objects for use in distributed object-oriented systems. !+H%A
was developed by the +bject 'anagement 8roup 0+'82& a consortium of software
companies and other organi3ations. The capability to use @ava objects within !+H%A is
referred to as @ava ),$ and has been incorporated into @,B :.C. @ava ),$ provides an
A() and a set of tools for interfacing @ava objects with !+H%A objects& and for
developing !+H%A objects in @ava. @ava ),$ also includes a @ava +bject He5uest %roker
0+H%2 and an +H% name server.
>*e Colle)tons %P=
The !ollections A() is a set of classes and interfaces that provide an implementation-
independent framework for working with collections of objects. This A() consists of
eleven classes and eight interfaces that have been added to the java.util package. These
classes and interfaces provide support for generic collections& sets& bags& maps& lists& and
linked lists. These classes and interfaces can be easily e4tended to provide support for
custom object collections.
>*e ?ava J,tensons 0ra+e7orC
@,B :.C provides the capability to e4tend the !ore A() classes. =4tensions are
implemented as @ava Archive 0@AH2 files that are installed in a particular directory or
downloaded from a DH$..
4e"eren)e H!/e)ts
Heference objects& introduced with @,B :.C& store references to other objects. They are
similar in function to ! and !II pointers& but do not provide access to specific memory
addresses. The java.lang.ref package provides si4 classes that implement reference
objects. These classes also provide the capability to notify a program when a referenced
object is subject to garbage collection. This capability enables reference objects to be
used to implement object-caching mechanisms.
Pa)Ca'e 2erson =(ent")aton
(ackage version identification is also a new capability that was introduced with @,B :.C.
)t allows applets and applications to obtain version information about a particular @ava
package. This version information enables large comple4 applications to evolve over
time& with some application packages being upgraded independently of others. The new
(ackage class provides methods for obtaining package version information. This version
information is stored in the manifest of ./ar files..
=np&t Met*o( %P=
The )nput 'ethod A() is an addition to the @,BAs internationali3ation support that
enables te4t-editing components to receive foreign language te4t input through input
methods. )t is designed to support large character sets& such as !hinese& @apanese& and
Borean. An input method lets users enter thousands of different characters using
keyboards with far fewer keys. Typically& a se5uence of several characters is typed and
then converted to create one or more characters.
Jn*an)e+ents
)n addition to the new capabilities identified in previous sections& the @,B :.C provides
significant enhancements to A()s& tools& and language features that were introduced in
@,B :.: and :.9. These enhancements are covered in the following subsections.
De)&rty
The security model enforced by the @,B has evolved from @,B :.9 through @,B :.: to
@,B :.C. The model enforced by @,B :.C is both more secure and more fle4ible than that
of preceding @,B releases. )t eliminates security flaws found in previous @,B versions.
)ts fle4ibility has been enhanced because it provides users with the capability to specify
security policies simply by editing the security permissions contained in their policy te4t
files. )n addition to policy improvements& the @,B :.C provides enhanced support and
tools for working with digital certificates.
?avaIeans
The @ava%eans support provided with @,B :.C includes the 8lasgow @ava%eans release.
The 8lasgow release adds the runtime containment and services protocol& support for
drag and drop& and the @ava%eans activation framework. The runtime containment and
services protocol provides beans with the capability to interoperate with other beans and
to learn information about their e4ecution environment. The new drag and drop support
provides beans with a more complete graphical user interface capability. The @ava%eans
activation framework allows beans to be selectively instantiated and used to support
dynamic program re5uirements
4e"le)ton
Heflection support was introduced in @,B :.:. Heflection enables classes& interfaces& and
objects to be e4amined and their public fields& constructors& and methods to be discovered
and used at runtime. These capabilities are used by @ava%eans& object inspection tools&
@ava runtime tools such as the debugger& and other @ava applications and applets. @,B :.C
provides the capability to identify a field& method& or constructor as suppressing default
@ava language access controls. This permits reflection to be better used with the more
fle4ible @,B :.C security model.
%&(o
@,B :.: provided the capability for applets to play audio files that were in the Sun Audio
0AD2 format. @,B :.C provides a new sound engine that allows audio files to be played
by both applets and applications. The sound engine also provides support for the 'usical
)nstrument ,igital )nterface 0'),)2& the 'icrosoft "indows audio file format 0"A1=2&
the Hich 'usic .ormat 0H'.2& and the Audio )nterchange .ile .ormat 0A)..2.
?%4
@ava Archive 0@AH2 files were introduced in @,B :.:. These files provide the capability
to store multiple files within a single archive file. @AH files help you organi3e applets&
applications& beans& and class libraries and provide more efficient use of network
resources. @,B :.C @AH enhancements include improved tools for working with @AH
files and new classes for performing @AH file input and output.
4M= an( DeralKaton
The Hemote 'ethod )nvocation 0H')2 A() was introduced in @,B :.:. )t provides the
capability for @ava objects e4ecuting on a local computer to invoke the methods of
objects that e4ecute on remote computers. +bject seriali3ation is used to pass objects as
parameters and return values in the remote method invocations.
The H') A() has been significantly enhanced in @,B :.C. The Hemote +bject Activation
framework supports remotely activated objects and object references that persist across
multiple object activations. Seriali3ation improvements provide the capability for
alternative objects to be written to and read from streams in support of seriali3ation.
?@IC
@,%! provides the capability to access databases from @ava. According to @avaSoft&
@,%! doesnAt stand for anything. Jowever& it is sometimes associated with K@ava
database connectivity.K @,%! was introduced in @,B :.:. @,B :.C includes an improved
version of the @,%!-+,%! bridge driver and support for @,%! C.9. (art L& K,atabase
(rogramming&K
:atve =nter"a)e
The @ava *ative )nterface 0@*)2 provides the capability for @ava objects to access
methods written in languages other than @ava. )n @,B :.C& the @*) includes new
capabilities for controlling the manner in which native methods interact with the @ava
1irtual 'achine.
Per"or+an)e
The overall performance of the @,B tools has been greatly improved. .irst and foremost
is the inclusion of a just-in-time 0@)T2 compiler with the @,B. +ther performance
enhancements include the use of native libraries for some performance-critical !ore A()
classes& improvements to multithreading performance& and reduction in memory usage for
string constants.
=+portant 5an'&a'e C*an'es
)n @,B :.C& the stop02& suspend02& and resume02 methods of the Thread class have been
deprecated because of errors and inconsistencies that may occur as the result of their use.
)nstead of using the stop02 method& it is recommended that threads monitor their
e4ecution and stop by returning from their run02 method. A thread may determine that its
e4ecution should be stopped as the result of monitoring the state of a shared variable. )n
addition& it is recommended that threads suspend and resume their own e4ecution as the
result of monitoring interface events& such as the value of shared variables. The wait02
and notify02 methods of the +bject class should be used to cause a thread to wait on
changes to the value of a shared variable.
563(' )n addition to changes to the Thread class& other classes& interfaces& and
methods have been deprecated in @,B :.C. A complete description of these
changes is included with the @,B :.C documentation. A deprecated A() element
is one that is still supported for backward-compatibility but is being phased out of
future @,B versions.
>ools C*an'es
The tools provided with @,B :.: have been improved in @,B :.C& and new tools have
been added. The new keytool and javasigner tools replace the javakey tool of @,B :.:.
*ew tools included with @,B :.C include the following
rmid--Hemote activation system daemon
keytool--'aintains a database of key pairs and digital certificates
javasigner--Dsed to sign @AH files and verify the signatures of signed files
policytool--=dits the local system security policy
tnameserv--)mplements the !+H%A !ommon +bject Services 0!+S2 *aming
Service
0or 1o&r An( %ttenton
Time Taken to %io ,egrade
%anana (eel ? to M "==BS
(aper %ag ?9 ,ays
!otton %ag > 'onths
"oolen Sacks : Year
"ood > to :9 Years
$eather Shoe M9 to >9 Years
Tin !an >9 to :99 Years
Aluminum C99 to >99 Years
(lastic %ag : 'illion Years
8lass %ottle Dnknown
Thermacoal 8od +nly Bnows

Você também pode gostar