What is ActionScript?

Moderators: Moderator, Global Moderator

Post Reply
Tami
Administrator
Administrator
Posts: 10892
Joined: Sun Apr 25, 2004 1:05 pm

What is ActionScript?

Post by Tami »

What is ActionScript?
by Adi Reddy Mora

Have you never used ActionScript before, and want to learn? This article will help you get started. Have you used only ActionScript 1.0, and wonder what changed with version 2.0? Keep reading to find out.

ActionScript is the object-oriented programming (OOP) language you use in your flash movies to add interactivity to your applications, whether it may be a simple animation or a complex enterprise rich internet application. The object-oriented programming (OOP) features in ActionScript 2.0 are based on the ECMAScript 4 Netscape Proposal ( www.mozilla.org/js/language/es4/index.html). Because the ECMA-4 proposal is not yet a standard, and because it is still changing, ActionScript 2.0 does not conform exactly to this specification.

ActionScript Basics

Syntax:

ActionScript has its own rules of grammar and punctuation that determine which characters and words are used to create meaning and in which order they can be written. For example, in English, a period ends a sentence; in ActionScript, a semicolon ends a statement.

[code]var name:String = "Jhon";[/code]

The above statement is a variable declaration of string data type.

ActionScript has syntax rules that you must follow to create scripts that can compile and run correctly.

ActionScript 2.0 is case sensitive, so the variables \"name\" and \"Name\" are different.

In ActionScript, dot syntax is used ( .) to access properties or methods belonging to an object or movie clip. It is also used to identify the target path to a movie clip, variable, function, or object.

[code]trace( rectangle_mc._width);[/code]

The above statement traces the width of the movieclip \"rectangle_mc\" where \"rectangle_mc\" is the instance name of the Movieclip.

Note: trace statement is used to output data in the output panel, mainly used for debugging purposes and these statements only execute during authoring.

ActionScript event handlers, class definitions, and functions are grouped together into blocks with curly braces ( {}).

[code]Ex: // Class Form.as

class Form() {

}

// onRelease Event handler for button

go_btn.onRelease = function() {  

};

// Function

function getData(name:String):Void {

}[/code]

Comments:

There are two ways you can write comments in ActionScript,
single line and multi-line comments.

[code]Ex:

// This is a single line comment

/*

This is a multi-line comment with two lines…

second line goes here…

*/[/code]

ActionScript contains predefined constants.

For example, the constants ENTER, BACKSPACE,SPACE, and TAB are properties of the Key object and refer to keyboard keys. To test whether the user is pressing the Enter key, you could use the following statement:

[code]if(Key.getCode() == Key.ENTER) {

}[/code]

Flash does not enforce constants; that is, you can\'t define your own constants.

ActionScript has the following basic data types that you use frequently in your programs:

String: A string is a sequence of characters such as letters, numbers, and punctuation marks. You enter strings in an ActionScript statement by enclosing them in single (\') or double (\") quotation marks.

Ex:
[code]var name:String = "Steve";[/code]

Number: The number data type is a double-precision floating-point number. The minimum value of a number object is approximately 5e-324. The maximum is approximately 1.79E+308.

Ex:
[code]var age:Number = 24;[/code]

Boolean: A Boolean value is one that is either true or false. ActionScript also converts the values true and false to 1 and 0 when appropriate. Boolean values are most often used with logical operators in ActionScript statements that make comparisons to control the flow of a script.

Ex:
[code]var smoking:Boolean = false;[/code]

Object: An object is a collection of properties. Each property has a name and a value. The value of a property can be any Flash data type - even the object data type. This lets you arrange objects inside each other, or nest them. To specify objects and their properties, you use the dot ( .) operator.

Ex:
[code]var user:Object = new Object();

user.name = "Steve";

user.age = 24;

user.sex = "male"[/code]

MovieClip: Movie clips are symbols that can play animation in a Flash application. They are the only data type that refers to a graphic element. The MovieClip data type lets you control movie clip symbols using the methods of the MovieClip class.

You do not use a constructor to call the methods of the MovieClip class. You can create a movie clip instance on the Stage or create an instance dynamically. Then you simply call the methods of the MovieClip class using the dot ( .) operator.

Null: The null data type has only one value, null. This value means no value - that is, a lack of data. You can assign the null value in a variety of situations to indicate that a property or variable does not yet have a value assigned to it.

Undefined: The undefined data type has one value, undefined, and is automatically assigned to a variable to which a value hasn\'t been assigned, either by your code or user interaction.

The value undefined is automatically assigned; unlike null, you don\'t assign undefined to a variable or property. You use the undefined data type to check if a variable is set or defined.

Void : The void data type has one value, void, and is used in a function definition to indicate that the function does not return a value, as shown in the following example:

Ex:
[code]//Function with a return type Void

function setItem(url:String):Void {

}[/code]

A variable is a container that holds information. The container itself is always the same, but the contents can change. By changing the value of a variable as the SWF file plays, you can record and save information about what the user has done, record values that change as the SWF file plays, or evaluate whether a condition is true or false.

New features and changes in ActionScript language elements

There are many changes and new elements were added in ActionScript 2.0. If you intend to use any of the following elements you need to target your applications to flash player 7 and later.

MovieClipLoader class: Very useful class which lets you monitor the progress of files (SWF or JPEG) as they are loaded into movie clips. The methods and properties available with this class let you monitor the progress of loading files and perform various tasks based on the loading information.

NetConnection class and NetStream class: These classes let you stream local Flash Video (FLV) files and control them.

Sound.onID3 and Sound.id3: Event handler and property to provide access to ID3 data associated with a Sound object that contains in an MP3 file.

Array.sort() and Array.sortOn(): These new methods let you perform additional sorting options, such as ascending and descending sorting, whether to consider case-sensitivity when sorting, and so forth.

System class: This class has several new objects and methods, and the System.capabilities object has several new properties.

TextField.condenseWhite: This property can be used to remove extra white space from HTML text.

TextField.mouseWheelEnabled: This property can be used to specify whether a text field\'s contents should scroll when the mouse pointer is positioned over a text field and the user rolls the mouse wheel.

TextField.StyleSheet class: This Class lets you create a style sheet object that contains text formatting rules such as font size, color, and other formatting styles.

TextField.styleSheet: This property lets you attach a style sheet object to a text field.

TextFormat.getTextExtent(): This method accepts a new parameter, and the object it returns contains a new member.

XML.addRequestHeader(): This method lets you add or change HTTP request headers sent with POST actions.

Mouse.onMouseWheel: This event listener will be generated when the user scrolls using the mouse wheel.

MovieClip.getNextHighestDepth(): This method lets you create MovieClip instances at runtime and be guaranteed that their objects render in front of the other objects in a parent movie clip\'s z-order space.

MovieClip.getInstanceAtDepth(): This method lets you access dynamically created MovieClip instances using the depth as a search index.

MovieClip.getSWFVersion(): This method lets you determine which version of Flash Player is supported by a loaded SWF file.

MovieClip.getTextSnapshot(): This method and the \"TextSnapshot object\" let you work with text that is in static text fields in a movie clip.

MovieClip._lockroot: This property lets you specify that a movie clip will act as _root for any movie clips loaded into it or that the meaning of _root in a movie clip won\'t change if that movie clip is loaded into another movie clip.

This section describes changes that improve your ability to debug your ActionScript programs.

Output window changed to Output panel You can now move and dock the Output panel in the same way as any other panel in Flash. It shows error messages (including some runtime errors) and lists of variables and objects

Improved error reporting at compile time In addition to providing more robust exception handling, ActionScript 2.0 provides several new compile-time errors.

Improved exception handling The Error class and the throw and try..catch..finally statements let you test and respond to runtime errors from within your script so that you can implement more robust exception handling.

Changes compared to ActionScript 1.0

The following are the major changes that you will find in ActionScript 2.0 when compared with 1.0.
Case Sensitivity: Case sensitivity is the new feature introduced in ActionScript 2.0, and whenever you publish your movie in Flash Player 7 or later it will be applied to your code. That means variable names that differ only in case (depth and Depth) are considered different from each other. This applies to keywords, class names, variables, method names, and so on.

This change also affects external variables loaded with LoadVars.load() or loadvariables().

Case-sensitivity is implemented in external scripts such as class files, scripts that you import using the #include command, and scripts that you write in your flash movie.
Strict Data Typing: This is another useful feature that allows you to type cast your variables to their appropriate data types, so that you can restrict variables to accept only specified data type values.

When creating a variable with ActionScript 2.0, use the following syntax not only to create the variable, but also to assign its data type at the same time:

[code]var a:String = "Hello World";[/code]

The above line says that \"a\" is a variable of string data type. If you assign any other data type value to \"a\" (Example: a = 20) it throws a \"Type mismatch\" error when compiled. Strict data typing also applies to classes, functions to type cast function parameters, return types, and so forth. Example:

[code]var my_array:Array = new Array(); var my_lv:LoadVars = new LoadVars(); function myFunction(name:String, age:Number):Void { }[/code]

Conclusion

This introduction just tells you the basics and core features and changes in ActionScript 2.0 compared to ActionScript 1.0. However there are a lot more useful features and concepts available in ActionScript 2.0, using which you can create some wonderful flash based applications.

Here are some useful ActionScript resources over the Web for you to get started using ActionScript:

[url=\"http://www.macromedia.com/devnet/mx/fla ... cript.html\"]http://www.macromedia.com/devnet/mx/fla ... cript.html[/url]

[url=\"http://www.actionscript.com/\"]http://www.actionscript.com/[/url]

[url=\"http://www.actionscripts.org/\"]http://www.actionscripts.org/[/url]

[url=\"http://www.devarticles.com/c/a/Flash/Wh ... ionScript/\"]source[/url]
Image

[color=\"#41211C\"]It takes years to build up trust and only seconds to destroy it

[/color]
Josh
Hero Member
Hero Member
Posts: 4627
Joined: Fri Jun 04, 2004 2:54 pm

What is ActionScript?

Post by Josh »

Good article! <img src=\'http://www.killanet.net/forum3/public/s ... >/good.gif\' class=\'bbc_emoticon\' alt=\'(Y)\' />
[align=center]

Image

“If you stop learning, you stop living.” ~Tami Quiring

“It's the rare man who understands the value of a single perfect rose.”

[/align]
Post Reply

Return to “Flash”