Your continued donations keep Wikipedia running!    

ActionScript

From Wikipedia, the free encyclopedia

Jump to: navigation, search

ActionScript is an ECMAScript-based programming language used for scripting Adobe Flash movies and applications. Since both ActionScript and JavaScript are based on the same ECMAScript syntax, fluency in one theoretically translates easily to the other. However, while JavaScript's DOM is browser window-, document- and form-centric, the ActionScript DOM is movie-centric, which may include animations, audio, text and event handling.

Contents

History

Creating ActionScript 2.0 in Adobe Flash MX Professional 2004 for Mac OS X 10.4. The code creates a simple bouncing ball that can be picked up and released.
Enlarge
Creating ActionScript 2.0 in Adobe Flash MX Professional 2004 for Mac OS X 10.4. The code creates a simple bouncing ball that can be picked up and released.

ActionScript first appeared in its current syntax with the release of Flash 5, which was the first thoroughly programmable version of Flash. This ActionScript release was named ActionScript 1.0. Flash 6 (MX) then further broadened the utility of the programming environment by adding a number of built-in functions and allowing better programmatic control of movie elements. Flash 7 (MX 2004) introduced ActionScript 2.0, which added strong typing and class-based programming features such as explicit class declarations, inheritance, interfaces, and Strict Data Typing. ActionScript 1.0 and 2.0 share the same compiled form within Flash SWFs (Shock Wave File).

Timeline

ActionScript 2.0 being made in an external editor, TextWrangler.
Enlarge
ActionScript 2.0 being made in an external editor, TextWrangler.
  • Flash Player 2: First version with scripting support, actions included gotoAndPlay, gotoAndStop, nextFrame and nextScene for timeline control.
  • Flash Player 3: Expanded basic scripting support with the ability to load external SWFs (loadMovie).
  • Flash Player 4: First player with a full scripting implementation (called Actions). The scripting was a slash based syntax and contained support for loops, conditionals, variables and other basic language constructs.
  • Flash Player 6: Added an event handling model, and support for switch.
  • Flash Player 7: Flash Player 7 offered some new features such as CSS text and performance improvements. Macromedia Flash compilers released alongside Flash Player 7 also support ActionScript 2.0, a Class programming language based on the ECMAScript 4 Netscape Proposal. However, ActionScript 2.0 can cross compile to ActionScript 1.0 byte-code, so it can be run by Flash Player 6.
  • Flash Player 8: Further extended ActionScript 2.0 by adding new class libraries with APIs for controlling bitmap data at run-time, and file-upload.
  • Flash Player 9 (initially called 8.5): Added ActionScript 3.0 with the advent of a new virtual machine, called AVM2 (ActionScript Virtual Machine 2), which coexists with the previous AVM1 needed to support legacy content. Performance increases were a major objective for this release of the player including a new JIT compilation. This is the first release of the player to be titled Adobe Flash Player.
  • Flash Lite 1.0 and 1.1: Flash Lite is the Flash technology specifically developed for mobile phones and consumer electronics devices. Flash Lite 1.1 supports Flash 4 ActionScript.
  • Flash Lite 2.2: Added support for Flash 7 ActionScript 2.0.

The Language

Syntax

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. Classes are extensions to the ActionScript language which the programmer can write him/herself, though there are many built-in classes such as the MovieClip class, which can be used to draw vectors onto the screen dynamically. Class files can be used to make your programming easier, and the class files can be transferred between many projects if needed.

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 present since Flash 5. Flash can send and receive XML, which can be used to create online multiplayer games via an online server.

ActionScript code is frequently written directly in the Flash authoring environment, which offers reference, code hints and syntax highlighting. Often, 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 may be compiled with the built-in compiler in the Flash IDE or with Motion Twin ActionScript2 Compiler (MTASC). See external links.

Criticism

  • Programmers say the Macromedia ActionScript 2.0 compiler is rather slow, often taking several minutes to compile around 100 classes, though the open-source compiler MTASC can be used which compiles a lot faster. (Subject to change with ActionScript 3.0)
  • ActionScript's very tolerant syntax is often frowned upon by programmers, as it often makes it hard to read unclean code. However, long-time ActionScripters and JavaScripters find the version 2.0 class-based programming model and the introduction of strict datatypes far more difficult to work with than previous versions of the language. (Subject to change with ActionScript 3.0)
  • Flash's ActionScript VM tends to hit a ceiling quickly in regards to the amount of computation that ActionScript can perform before triggering an internal timeout, especially on the Mac Flash Player. Simply counting the numbers from 1 to 5000, for instance, threatens to exceed the capacity of the Flash Player for some users.
  • Many people do not like having to import certain classes into Flash 8 before being able to use them; unfortunately for them. (ActionScript 3.0 relies heavily on importing classes and scripting without is practically impossible.)
  • The .swf file format is easy to decompile making it very difficult to keep the source code secret.

Examples

ActionScript 2.0 Examples

Creating ActionScript 2.0 in Macromedia Flash MX Professional 2004 in Mac OS X 10.2. The code creates a customizable animation similar to the one seen during the boot process of Mac OS X.
Enlarge
Creating ActionScript 2.0 in Macromedia Flash MX Professional 2004 in Mac OS X 10.2. The code creates a customizable animation similar to the one seen during the boot process of Mac OS X.

The following prints Hello world. Note this will only work when run inside the Flash IDE, as the trace function is only supported inside it.

trace("Hello world!");

The following code outputs the current mouse position when the mouse moves, by using the onMouseMove event. Again this will only work in the Flash IDE.

onMouseMove = function () {
   trace("X: "+_root._xmouse);
   trace("Y: "+_root._ymouse);
};

This more advanced example creates an array containing numbers and strings, and assigns a number to a variable called num and a string to a variable called str using prototype functions and function recursion. Then, using the MovieClip API, a text field is drawn on screen, into which the variable values are written.

var my_Array:Array = new Array("Hello", "ActionScript", 3, 7, 11, "Flash");
Array.prototype.pickNumber = function():Number  {
   var rand:Number = random(this.length);
   return (typeof (this[rand]) == "number") ? this[rand] : this.pickNumber();
};
Array.prototype.pickString = function():String  {
   var rand:Number = random(this.length);
   return (typeof (this[rand]) == "string") ? this[rand] : this.pickString();
};
var num:Number = my_Array.pickNumber();
var str:String = my_Array.pickString();
_root.createTextField("txt", 1, 10, 10, 530, 390);
txt.text = "Array = "+my_Array+"\nRandom Number = "+num+"\nRandom String = "+str;

Array and dataProvider example:

var aData:Array = [{name: "J. Bell", age: "55"}, {name: "B. Longman", age: "21"}];
dataProvider.dataGrid = aData;

ActionScript 3.0 Examples

This Hello World example uses ActionScript 3.0:

package {
   import flash.display.Sprite;
   import flash.text.TextField;
   import flash.filters.DropShadowFilter;
   public class HelloWorld2 extends Sprite {
      public function HelloWorld2() {
         var shad:DropShadowFilter = new DropShadowFilter(2, 45, 0x000000, 25, 3, 3, 2, 2);
         var txt:TextField = new TextField();
         txt.textColor = 0xFFFFFF;
         txt.filters = [shad];
         txt.width = 120;
         txt.x = Math.random()*300;
         txt.y = Math.random()*300;
         txt.selectable = false;
         txt.text = "Hello World welcome! ["+Math.round(txt.x)+","+Math.round(txt.y)+"]";
         addChild(txt);
      }
   }
}

External links

Technical

Tutorials

Resources

  • Cursos ActionScript Actionscript Training This article was copied from the English Wikipedia article. (Spanish)
  • My Flash Resource - A continually growing collection of tutorials, thoughts and interests of a Flash Animator and/or Developer.
  • OSFlash - A resource site for open source Flash projects and tools.
  • MTASC - An open source command line ActionScript 2.0 compiler written in OCaml.
  • haXe - Another programming language that can be used to compiler SWF, by the authors of MTASC
  • NeoSwiff A C# to SWF compiler
  • KineticFusion - A commercial cross-platform ActionScript 2.0 compiler written in Java.
  • secureSWF - A free ActionScript Obfuscator.
  • SE|PY Actionscript Editor - An open source actionscript editor.
  • FlashDevelop - FlashDevelop is a .NET open source script editor designed mostly for Actionscript 2 development. FlashDevelop is very quick to setup and easy to use as an external editor for the Flash IDE or as a complete open source development environment.
  • Flash-DB - Flash-DB is an online community deticated to dynamic content in Flash. They offer numerous tutorials on topics involving Flash (5,MX,MX 2004, 8) and server side integration (PHP, ASP, mySQL, javascript, Flash remoting, and much more). They offer free support for all related topics and tutorials.

Other

See also