ActionScript

From Free net encyclopedia

ActionScript is an ECMAScript-based programming language used for scripting Macromedia 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

Image:OSX10-2-actionscript2.png 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 object-oriented 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 (Shockwave Flash files).

Timeline

  • 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 control bitmap data at run-time, and file-upload.
  • Flash Player 8.5 (scheduled for release in Spring 2006, currently in public beta): Added ActionScript 3.0 with the advent of a new virtual machine, called AVM2 (ActionScript Virtual Machine 2) which will coexist with the previous AVM1 needed to support legacy content. Performance increases have been a major objective for this release of the player.

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 himself, 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.
  • ActionScript's very tolerant syntax is often frowned upon by programmers, as it often makes it hard to read unclean code.
  • Using many vectors in Flash can lag terribly for the machine running the application, as Flash redraws every vector frame-by-frame. Flash 8 introduced the cacheAsBitmap variable which will temporarily turn the vector into a Bitmap, which can help reduce lag.
  • 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

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;

ActionScript 3.0 Examples

This advanced Hello World program currently needs to be compiled using the Flex 2.0 Public Beta IDE.

package {
   import flash.display.TextField;
   import flash.display.MovieClip;
   import flash.filters.DropShadowFilter;
   public class HelloWorld extends MovieClip {
      public function HelloWorld() {
         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! ["+Math.round(txt.x)+","+Math.round(txt.y)+"]";
         addChild(txt);
      }
   }
}

External links

Technical

Tutorials

Resources

  • OSFlash - A resource site for open source Flash projects and tools.
  • MTASC - An open source command line ActionScript 2.0 compiler written in OCaml.
  • 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.
  • V-Cam - A virtual "camera" written by Sham Bahngal.

Other

  • FlashGuru - FlashGuru's Reference blog for Flash Developers.
  • Newgrounds - A community site where you can post your Flash games, there is also the Flash forum where you can seek help.
  • Gotoandplay: a bunch of flash examples: Flash applied
  • Flashplayer - a site where one can post flash animations or games and view other's animations and games for free
  • UnitZeroOne - A blog for Flash Developers.
  • Ioji.it - A site owned by a flash game designer with a forum about Flash and Actionscript.
  • FlashKit - A Flash Developers Resourse Site including tutorials, sound fx and music.

See also

de:ActionScript es:ActionScript fr:ActionScript gl:ActionScript he:ActionScript it:ActionScript nl:ActionScript ja:ActionScript pl:ActionScript pt:ActionScript ru:ActionScript sv:ActionScript zh:ActionScript