ActionScript

From Wikipedia, the free encyclopedia

ActionScript is a scripting language based on ECMAScript, primarily used to develop software for the Adobe Flash .

Applications developed using Adobe Flash or Adobe Flex will often use ActionScript for runtime manipulation of data and content. Other platforms, such as ColdFusion also support scripting with the ActionScript language.

Contents

[edit] History

Creating ActionScript 2.0 in Macromedia Flash MX Professional 2004 for Mac OS X 10.4. The code creates a simple bouncing ball that can be picked up and released.
Creating ActionScript 2.0 in Macromedia 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.

[edit] Timeline

  • 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.1: Added support for Flash 7 ActionScript 2.0.
  • 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.

[edit] Syntax

In ActionScript 2.0 there can be classes, and also, a library item (a MovieClip) can be associated with a class. Classes are usually written within the Flash IDE as external Actionscript Files; however, they can be created using a text editor; providing that the resulting file has 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:

  • 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.

The Flash authoring environment for ActionScript 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).

An example of syntax is the period symbol ("."). This symbol separates characters, for example, if one wanted a piece of code to access the inside of an mc, then they would use the dot for “onClipEvent(enterFrame){if (_root.ball.hitTest(_root.enemy)){_root.ball._x+=10;}}”

[edit] Criticism

The neutrality or factuality of this article or section may be compromised by weasel words.
You can help Wikipedia by improving weasel-worded statements
.
  • 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)
  • Although its syntax is very tolerant, ActionScript 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.
  • You have to import certain classes into Flash 8 before being able to use them, which could be rather tedious; unfortunately, ActionScript 3.0 relies heavily on importing classes and scripting without it is practically impossible.
  • The .swf file format is easy to decompile, making it very difficult to keep the source code secret. Although this could be beneficial, the use of certain third-party software can encrypt and/or obfuscate the .swf, making it difficult or nearly impossible to decompile.

[edit] Examples

[edit] ActionScript 2.0 Examples

The following prints Hello world! into the Flash IDE output panel. 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);
};

The following code, when placed on a Button, begins to play the timeline of the MovieClip the button is placed in. This also applies to the root timeline since the root is considered a MovieClip as well.

on(release) {
   play();
}

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 myArray: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 = myArray.pickNumber();
var str:String = myArray.pickString();
_root.createTextField("txt", 1, 10, 10, 530, 390);
txt.text = "Array = "+myArray+"\nRandom Number = "+num+"\nRandom String = "+str;

Array and dataProvider example:

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

[edit] 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);
      }
   }
}

[edit] See also

[edit] External links