Contoh ActionScript

Contoh ActionScript 2.0

Yang berikut mecetak Hello dunia dalam panel output Flash persekitaran pembangunan bersepadu (Integrated development environment - IDE). Nota ini hanya berlaku apabila dilaksanakan dalam Flash persekitaran pembangunan bersepadu (Integrated development environment - IDE), kerana fungsi jejak hanya disokong dalamnya.

 trace ("Hello world!");

The following code prints Hello world to the stage.

_root.createTextField("myText", 1, 0,0,100,25);_root.myText.text = "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, begins to play the timeline of the MovieClip the button is placed in. This also applies to the root timeline since the root is considered a MovieClip as well.

 on(release) {    play(); }

Contoh lebih maju ini mencipta jujukan (array) yang mengandungi nombor dan rantaian perkataan, dan menetapkan nombor kepada pembolehubah dikenali num dan pembolehubah rantaian (string) dikenali str menggunakan fingsi prototaip (prototype functions) dan fungsi (recursion). Kemudian, menggunakan MovieClip API, medan teks dilakar pada skrin, dalam mana nilai pembolehubah dipaparkan.

 var myArray: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 = myArray.pickNumber(); var str:String = myArray.pickString(); _root.createTextField ("txt", 1, 10, 10, 530, 390); txt.text = "Array = "+myArray+"\nRandom Number = "+num+"\nRandom String = "+str;

Array and dataProvider example:

 var aData:Array = [{name: "J. Bell", age: "55"}, {name: "B. Longman", age: "21"}]; dataGrid.dataProvider = aData;

Contoh ActionScript 3.0

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