ActionScript is a scripting language for Adobe Flash. ActionScript scripting language is used for developing applications with in Adobe Flash. ActionScript helps the developer to create Rich Internet Applications, provides interactivity etc. ActionScript is based on ECMAScript scripting language.
In order to execute an ActionScript we need an ActionScript Virtual Machine. This ActionScript Virtual Machine is a part of Adobe Flash Player.
ActionScript Virtual Machine
ActionScript Virtual Machine is the byte code interpreter for ActionScript. All ActionScript will be compiled to bytecode. This bytecode will be interpreted by the ActionScript Virtual Machine at run time and executes the ActionScript. The bytecode obtained as a result of compilation of ActionScript is embedded into SWF files. AVM helps the developer to handle runtime exceptions more efficiently resulting a more robust code.
A First ActionScript program.
Now let us start of first ActionScript. Our script will animate a simple circle.
First draw a circle on stage. Now convert this circle to a movie clip. Go to movie clip action. Enter the following code to MovieClip action.
onClipEvent (load) { // Set initial x position of the clip this._x = 100; } onClipEvent (enterFrame) { //Move clip 10 pixels towards x direction. this._x = this._x +10; }
A closer look at First Program
Now we have seen how to create a small animation using ActionScript. So now let's go to the details of our first ActionScript.
Our ActionScript is starting with onClipEvent(load). Here the onClipEvent is an event and the load is the event type. An event is a state of change of an object. We will look into the details of events like how to create an event?, how to handle an ActionScript event etc, later.
After onCickEvent(load) statement, we have opened a curly brace. Curly braces are used for grouping blocks of statements in event handlers, functions, classes, loops etc. In our script we need to execute a series of statements on onClipEvent event. So in order to group all these statements together we are using the curly braces. For event handlers, functions and classes even if the execution block contains only one statement we need the curly brace.
Inside curly race we have a statement this._x = 100; Here we are setting the x position of the movie clip on the stage. Since this event is executed at the time of load, when the movie loads our clip will be having a position at 100.
In next event handler we are moving the movie clip 10 pixels towards x axis.