ActionScript

From Academic Kids

ActionScript is an ECMAScript-based programming language used for controlling Macromedia Flash movies and applications. Since both ActionScript and JavaScript are based on the same ECMAScript syntax, fluency in one easily translates to the other. However, the client model is dramatically different: while JavaScript deals with windows, documents and forms, ActionScript deals with movie-clips, text fields and sounds.

ActionScript achieved something resembling its current syntax (retroactively named ActionScript 1.0) in Flash 5, the first version of Flash to be thoroughly programmable. Flash 6 broadened the power of the programming environment by adding many more built-in functions and allowing more programatic control of movie elements. Flash 7 (MX 2004) introduced ActionScript 2.0, which adds strong typing and object-oriented features such as explicit class declarations, inheritance, interfaces, and encapsulation. ActionScript 1.0 and 2.0 share the same compiled form within Flash SWFs.

Features of the Flash ActionScript implementation that JavaScript programmers may find interesting:

  • Everything is designed to be asynchronous; callbacks are ubiquitous, but Event objects do not exist.
  • The XML implementation has been solid since Flash 5. Flash can send and receive XML asynchronously.

ActionScript code is frequently written directly in the Flash authoring environment, which offers reference and syntax highlighting. In this case, the source code is saved along with the rest of the movie in a .fla file. It is also common for ActionScript code to be imported from external text files via #include statements. In this case, the external files are often given .as extensions, but this is not a universal convention.

In ActionScript 2.0 there can be classes, and also, a library item (a movie clip) can be associated with a class. Classes are always written in external text files, and these files must have the .as extension.

The ActionScript 2.0 compiler is notoriously slow, often taking several minutes to compile around 100 classes.

Contents

History

Flash Player 2 : First version with any scripting support. Actions included gotoAndPlay, gotoAndStop, nextFrame and nextScene.

Flash Player 3 : Expanded basic script support with the ability to load external SWFs (loadMovie).

Flash Player 4 : First player with a full scripting implementation (called Actions). The scripting was based on a slash based syntax and contained support for loops, conditionals, variables and other basic language constructs.

Flash Player 5 : Included first version of ActionScript. This was a prototype based-language based on ECMAScript (http://www.ecma-international.org/publications/standards/Ecma-262.htm), and allowed full procedural and object oriented programing.

Flash Player 6 : Added an event handling model, and support for switch.

Flash Player 7 : The compiler in the corresponding release of the Flash authoring tool, Flash MX 2004 (http://www.macromedia.com/software/flash), supported ActionScript 2.0 which syntactically was a more class-based language. However, it compiled down to ActionScript 1.0 byte code, which could be run by existing Players dating back to Flash Player 6. Flash Player 7 offered some new features such as CSS text and performance improvements. The new object-oriented features of ActionScript 2.0 are based on the ECMAScript 4 Netscape Proposal (http://www.mozilla.org/js/language/es4).

Coding style

Information bellow is a summary from Macromedia's ActionScript Coding Standards. For a more complete explanation, please see the full document. http://www.macromedia.com/devnet/mx/flash/whitepapers/actionscript_standards.pdf

The following techniques are not required, but contribute towards more efficient, or at least more easily understandable code.

Naming

Naming involves capitalization of code elements. Function names and variables should begin with a lower-case letter; objects should be capitalized. The first letter of each subsequent word should also be capitalised in both cases.

  • Components or objects: ProductInformation, MovieController
  • Variable or property: userName, myHtml, rawXml

The Flash code editor features code completion only when variables are named according to a specific format. This involves appending the variable type to the end of the variable name.

Supported suffixes for code completion.
Object type Suffix string Example
Array _array myArray_array
Button _btn myButton_mc
Camera _cam myCamera_cam
Color _color myColor_color
ContextMenu _cm myContextMenu_cm
ContextMenuItem _cmi myContextMenuItem_cmi
Date _date myDate_date
Error _err myError_err
LoadVars _lv myVar_lv
LoadConnection _lc myConnection_lc
Microphone _mic myMicro_mic
MovieClip _mc myMovieClip_mc
MovieClipLoader _mcl myLoader_mcl
PrintJob _pj myPrint_pj
NetConnection _nc myConnection_nc
NetStream _ns myStream_ns
SharedObject _so myObject_so
Sound _sound mySound_sound
String _str myString_str
TextField _txt myTextField_txt
TextFormat _fmt myFormat_fmt
Video _video myVideo_video
XML _xml myXML_xml
XMLNode _xmlnode myNode_xmlnode
XMLSocket _xmlsocket mySocket_xmlsocket

Commenting code

Commenting code is always recommended. Comments should document the decisions made while building the code, telling the story of what it attempts to do. A future developer should be able to pickup the logic of the code with the assistance of the comments.

var clicks = 0;    // This is a simple comment
/*
This is a multiline comment.
....
....
*/

Some common methods for indicating important comments are:

// :TODO: more work to be done here
// :BUG: [bugid] this is a known issue
// :KLUDGE: this bit isn't very elegant
// :TRICKY: lots of interactions, think twice before modifying

Timeline layout

  • Don't use default layer names (Layer 1, Layer 2, etc.), provide your own intuitive labels.
  • Groups layers together in folders, where it makes sense.
  • Place ActionScript layers at the top of the stack, to easily locate all the code on the timeline.
  • Lock layers currently not in use.


Scoping variables

_parent.myVar.blah = 100;  // Use relative addressing like this
_root.myClip = 200;        // Avoid absolute addressing as much as possible
_global.myVar = 300;       // _global variables are available to all movies within the player

Keep actions together

  • Whenever possible, all code should be placed in one location, making it easier to find and debug. Preferably frame 1 of the timeline. (However if a preloader is used, code may need to be placed on frame 2)
  • Where large blocks of code are present, split the code into logical sections with comments as headers.

Avoid attaching code to Movie Clips or buttons

Attempt to write event code on a frame rather than on the object itself, unless it is purely a function call.

// CODE ON BUTTON - Not Recommended
on (release) {
    play();
}
// CODE ON FRAME - Recommended
myButton.onRelease = function() {
    play();
}

ActionScript version 2.0 supports linking MovieClip instances to ActionScript classes by editing the "Linkage" settings, so that you can use external classes like this:

class SomeClip extends MovieClip {
    function moveTo(x:Number, y:Number) {
        this._x = x;
        this._y = y;
    }
}

The classes will be automatically instantiated.

External links

See Also

es:ActionScript fr:ActionScript he:ActionScript nl:ActionScript pt:ActionScript ru:ActionScript zh:ActionScript

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools