Jump to content

ActionScript: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Kameronmf (talk | contribs)
Line 58: Line 58:
trace("Y: "+_root._ymouse);
trace("Y: "+_root._ymouse);
};
};

The following code, when placed on a [[Button symbol]], makes a Flash movie begin to play through the timeline.
on(release) {
play();
}


This more advanced example creates an array containing numbers and strings, and assigns a number to a variable called <code>num</code> and a string to a variable called <code>str</code> 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.
This more advanced example creates an array containing numbers and strings, and assigns a number to a variable called <code>num</code> and a string to a variable called <code>str</code> 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.

Revision as of 01:52, 6 December 2006

You must add a |reason= parameter to this Cleanup template – replace it with {{Cleanup|October 2006|reason=<Fill reason here>}}, or remove the Cleanup template.

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.

History

File:As2 bounceball.png
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.

Timeline

File:Actionscript external.png
An ActionScript function 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.1: Added support for Flash 7 ActionScript 2.0.

The Language

ActionScript 2.0 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). 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

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);
};

The following code, when placed on a Button symbol, makes a Flash movie begin to play through the timeline. 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 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);
      }
   }
}

See also