JavaScript and ActionScript | WebReference

JavaScript and ActionScript

current pageTo page 2
[next]

JavaScript and ActionScript

JavaScript and ActionScript share a syntactical core - ECMAScript edition 2. This gives JavaScript coders a leg up on using one of the most popular animation formats - Flash .SWF files.

Macromedia is a vigorous user of JavaScript. Its entire MX line of products use JavaScript as the macro scripting language either explicitly as in the case of Dreamweaver and Fireworks MX or behind the covers in the case of Freehand MX. But perhaps one of the most intriguing uses of JavaScript is in the animation scripts for Flash - ActionScript.

ActionScript is a subset of ECMAScript 1.0 edition 2 which roughly corresponds to Netscape's JavaScript 1.4, making ActionScript about one version behind the latest JavaScript technology. But more importantly, it means JavaScript developers have an advantage by picking up and using ActionScript. In effect, since day one they've known all the syntax, operators, and major statements of ActionScript.

Now some developers might say: "Big deal, who cares about Flash animations ?" Well, as it turns out - quite a lot of folks in the software business. First, Adobe has thrown in the towel on SVG and adopted Flash's .SWF file format for animations as has just about every other major player in the graphics, 3D modeling and image design fields. Both Adobe and Corel have also adopted ActionScript (or a subset) for their Live Motion 2 and RAVE animation tools respectively. Second, the documentation and demo-creation tools are exploding with Flash Help and Flash demo systems. The advantage is that the .SWF file format is very efficient in storing the rich set of still images, audio, and video used in demos. Some vendors like eHelp's RoboDemo provide output in .FLA as well as .SWF format, making it simple to add ActionScript routines to their demos. Finally, Flash 5.x and 6.x plugins are available on 98% of all browsers. Given the explosion of Flash-based banner ads, Flash data components and complete Flash websites, Flash usage has moved well beyond the 'skip intro' excesses of the Dot.Com bust era. The bottom line: Flash .SWF animations are a major web development fixture and ActionScript is the scripting language for Flash.

What You Already Know

If you are proficient in JavaScript, 65-75% of your skills will immediately transfer over to ActionScript because as noted, ActionScript, like JavaScript derives from the same ECMAScript 262 standard. Thus, the rules for creation of variables, arrays, functions, and objects are identical. As well, nearly the same syntax is used for operators, statements, logical expression and flow of control statements as illustrated with the following code snippet in ActionScript:

var cities = ["Albany", "Cairo", "Dublin", "Lima", "Warsaw"];
for (ii = 0; ii <cities.length; ii++){
  // spell out the names of each city backwards
  jj=cities[ii].length;
  while ( --jj >=0) trace(cities[ii].charAt[jj);
}


Comments, scoping of variables, and the object model are the same. There are some omissions in ActionScript of syntax and scripting capabilities that JavaScript coders may find disconcerting. Most notable - ActionScript does not support the exception handling capabilities of JavaScript. Missing are the basic statements: try, catch, finally and throw. Statement labels for break and continue statements are not allowed in ActionScript. Regular Expressions are not supported. The ActionScript date function will not parse human readable dates like "July 4 , 2002". The eval() function is severely restricted in what type of expressions can be supplied in ActionScript. In total, roughly a dozen of these type limitations exist.

However, ActionScript is more lenient than ECMAScript/JavaScript on case sensitivity - variable names and identifiers are case insensitive. Likewise a reference to an undeclared variable causes a runtime error where as ActionScript allows for them. Some developers think this leniency has been carried to an extreme with ActionScript just ignoring undeclared functions (often a typo error), while JavaScript will produce a runtime error, flagging the offending function as an undefined reference. This party is ambivalent - sometimes I want the program to continue functioning and there are other times when I want the error flagged.

The object syntax of ActionScript is simple and has optional formats:

//First we define a class, sprite, and its properties
function sprite(rad, scolor, xx, yy, rotate){

  this.radius = rad; this.color = scolor;
  this.xpos = xx; this.ypos = yy; this.angle = rotate;
}

//Now to create a sprite object is easy
var graydot = new sprite(.25, 0x333333, 0, 50, 20);
var bigGreen = new sprite(10, 0x33FF33, 5, 500, 0);
// And references to the objects properties can be made two ways
var size = graydot.radius;
var size = graydot['radius'];

And this simplicity continues into the definition of the methods and classes for a function - see Colin Moock's ActionScript for Flash MX 2nd Edition or Bhangall et alia's excellent ActionScript Reference Guide for all the details on declaring and using objects in ActionScript. The chapters resemble the equivalent sections on objects/classes in David Flanagan's JavaScript: the Definitive Guide precisely because the syntax is similar. However, a major difference between JavaScript and ActionScript are the many predefined objects.

current pageTo page 2
[next]

Created: June 2, 2003
Revised: June 26, 2003

URL: https://webreference.com/programming/javascript/j_s/column2