|
| 1 | +--- |
| 2 | +seoTitle: ActionScript Programming Reference – Flash and AIR Developer Guide |
| 3 | +description: "Comprehensive ActionScript 3.0 reference covering classes, event handling, display list, graphics, XML (E4X), and runtime scripting for Adobe Flash and AIR." |
| 4 | +keywords: "ActionScript, AS3, Flash, Adobe AIR, event handling, display list, E4X, graphics, animation, script reference, cheat sheet, VR-Rathod, Code-Note, code note vr, vr book" |
| 5 | +displayTitle: ActionScript |
| 6 | +--- |
| 7 | + |
| 8 | +- # History |
| 9 | + - **How**: |
| 10 | + - **ActionScript** originated as a simple scripting language for Macromedia Flash (supporting basic play/stop actions). It evolved into a full-fledged Object-Oriented language based on the **ECMAScript** standard. |
| 11 | + - Key milestones: |
| 12 | + - **ActionScript 1.0 / 2.0**: Prototypes and basic class declarations. |
| 13 | + - **ActionScript 3.0 (AS3)**: Released in **2006** with Flash Player 9. AS3 was a major ground-up rewrite, introducing a high-performance virtual machine (**AVM2**), strict compiler typing, XML support (E4X), and a structured Event Dispatcher model. |
| 14 | + - Adobe acquired Macromedia in 2005. Following Flash Player's deprecation in 2020, ActionScript continues to live on through the open-source **Apache Flex** project and **HARMAN's Adobe AIR** runtime for building mobile and desktop apps. |
| 15 | + - |
| 16 | + - **Who**: |
| 17 | + - Developed by **Macromedia** (later **Adobe**), now maintained by **Apache** and **HARMAN**. |
| 18 | + - |
| 19 | + - **Why**: |
| 20 | + - Created to control vector animations, handle complex user interactions, process XML data feeds, and render real-time vector graphics on web browsers and standalone desktop/mobile applications. |
| 21 | +- |
| 22 | +- # Introduction |
| 23 | + - ## Advantages |
| 24 | + collapsed:: true |
| 25 | + - **Robust Event-Driven Model** — Unified EventDispatcher handles touch inputs, rendering loops, and network requests cleanly. |
| 26 | + - **Advanced Vector Graphics Rendering** — High-performance vector drawing APIs (lines, shapes, gradients) built natively into the runtime. |
| 27 | + - **E4X XML Parsing Integration** — Native XML handling makes parsing, filtering, and query operations on XML feeds exceptionally simple (treating XML like regular objects). |
| 28 | + - **AIR Runtime Cross-Platform** — Build native desktop (macOS, Windows) and mobile (iOS, Android) applications using a single codebase. |
| 29 | + - |
| 30 | + - ## Disadvantages |
| 31 | + collapsed:: true |
| 32 | + - **Deprecated Ecosystem** — Flash Player browser plugin is no longer supported, making web distribution impossible without third-party runtimes like Ruffle. |
| 33 | + - **Single-Threaded Execution** — Heavy computation freezes the user interface unless broken up using asynchronous timers or worker threads. |
| 34 | + - **Slow Performance for 3D** — Native Vector graphics render on CPU; advanced 3D requires using Stage3D APIs, which add code complexity. |
| 35 | + - |
| 36 | + - ## Remember Points |
| 37 | + collapsed:: true |
| 38 | + - **AVM2 Engine** — AS3 runs on AVM2, which compiles code down to bytecode. |
| 39 | + - **The Display List** — Visual objects are only rendered on screen if they are explicitly added to the display hierarchy using `addChild()`. |
| 40 | + - **Packages and Filenames** — Class names must exactly match their file names, and they must be placed in matching directory package folders. |
| 41 | +- |
| 42 | +- # Basics |
| 43 | + - ## Hello World & trace Output |
| 44 | + - ```actionscript |
| 45 | + package { |
| 46 | + import flash.display.Sprite; |
| 47 | + |
| 48 | + public class HelloWorld extends Sprite { |
| 49 | + public function HelloWorld() { |
| 50 | + // trace sends text output to the debugging console |
| 51 | + trace("Hello, World!"); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + ``` |
| 56 | + - `package` defines the namespace namespace folder containing this class. |
| 57 | + - `extends Sprite` inherits visual display characteristics. |
| 58 | + - The function `public function HelloWorld()` is the constructor (executed upon instantiation). |
| 59 | + - |
| 60 | + - ## Variables and Primitive Data Types |
| 61 | + - ```actionscript |
| 62 | + // Variables and Constants |
| 63 | + var username:String = "VR Rathod"; |
| 64 | + const PI:Number = 3.14159; |
| 65 | + |
| 66 | + // Primitive Types: |
| 67 | + var count:int = -10; -- Signed integer (32-bit) |
| 68 | + var index:uint = 42; -- Unsigned integer (32-bit, recommended for colors/indices) |
| 69 | + var ratio:Number = 0.75; -- Double-precision floating-point (default numeric type) |
| 70 | + var isReady:Boolean = true; |
| 71 | + |
| 72 | + // Collections: |
| 73 | + var list:Array = [1, "two", 3.0]; -- Heterogeneous untyped array |
| 74 | + var vec:Vector.<int> = new Vector.<int>(); -- Strongly-typed dense array (much faster than Array) |
| 75 | + vec.push(10, 20, 30); |
| 76 | + ``` |
| 77 | +- |
| 78 | +- # Object-Oriented Programming |
| 79 | + - ## Class Structure & Access Modifiers |
| 80 | + collapsed:: true |
| 81 | + - ```actionscript |
| 82 | + // File: com/app/User.as |
| 83 | + package com.app { |
| 84 | + public class User { |
| 85 | + // Public properties (accessible everywhere) |
| 86 | + public var username:String; |
| 87 | + |
| 88 | + // Protected properties (accessible in class and subclasses) |
| 89 | + protected var score:int; |
| 90 | + |
| 91 | + // Private properties (accessible only inside this class) |
| 92 | + private var _password:String; |
| 93 | + |
| 94 | + // Constructor |
| 95 | + public function User(name:String, initialScore:int) { |
| 96 | + this.username = name; |
| 97 | + this.score = initialScore; |
| 98 | + } |
| 99 | + |
| 100 | + // Getter and Setter |
| 101 | + public function get password():String { |
| 102 | + return _password; |
| 103 | + } |
| 104 | + |
| 105 | + public function set password(value:String):void { |
| 106 | + if (value.length >= 6) { |
| 107 | + _password = value; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + ``` |
| 113 | + - |
| 114 | + - ## Inheritance & Interfaces |
| 115 | + collapsed:: true |
| 116 | + - ```actionscript |
| 117 | + // Inherit class behavior using 'extends' |
| 118 | + public class AdminUser extends User { |
| 119 | + public function AdminUser(name:String) { |
| 120 | + super(name, 1000); // Call parent constructor |
| 121 | + } |
| 122 | + |
| 123 | + // Override methods explicitly using the 'override' keyword |
| 124 | + override public function set password(value:String):void { |
| 125 | + super.password = value + "_admin"; // call parent setter |
| 126 | + } |
| 127 | + } |
| 128 | + ``` |
| 129 | +- |
| 130 | +- # Event Handling |
| 131 | + - ## Event Dispatcher Model |
| 132 | + collapsed:: true |
| 133 | + - ```actionscript |
| 134 | + import flash.events.MouseEvent; |
| 135 | + import flash.events.Event; |
| 136 | + |
| 137 | + // 1. Add event listener (syntax: target.addEventListener(type, listener_function)) |
| 138 | + myButton.addEventListener(MouseEvent.CLICK, onButtonClicked); |
| 139 | + |
| 140 | + // 2. Event Listener Callback |
| 141 | + private function onButtonClicked(event:MouseEvent):void { |
| 142 | + trace("Button clicked at x: " + event.localX); |
| 143 | + |
| 144 | + // Remove event listener to prevent memory leaks when no longer needed |
| 145 | + myButton.removeEventListener(MouseEvent.CLICK, onButtonClicked); |
| 146 | + } |
| 147 | + ``` |
| 148 | + - |
| 149 | + - ## Custom Events |
| 150 | + collapsed:: true |
| 151 | + - ```actionscript |
| 152 | + // Custom event declaration |
| 153 | + import flash.events.Event; |
| 154 | + |
| 155 | + public class GameEvent extends Event { |
| 156 | + public static const LEVEL_UP:String = "levelUp"; |
| 157 | + public var newLevel:int; |
| 158 | + |
| 159 | + public function GameEvent(type:String, level:int, bubbles:Boolean = false, cancelable:Boolean = false) { |
| 160 | + super(type, bubbles, cancelable); |
| 161 | + this.newLevel = level; |
| 162 | + } |
| 163 | + |
| 164 | + // Overriding clone is required for custom events to work in bubble dispatching |
| 165 | + override public function clone():Event { |
| 166 | + return new GameEvent(type, newLevel, bubbles, cancelable); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + // Dispatching event: |
| 171 | + // dispatchEvent(new GameEvent(GameEvent.LEVEL_UP, 5)); |
| 172 | + ``` |
| 173 | +- |
| 174 | +- # Display List & Graphics |
| 175 | + - ## Display Objects Hierarchy |
| 176 | + collapsed:: true |
| 177 | + - ```actionscript |
| 178 | + import flash.display.Sprite; |
| 179 | + import flash.text.TextField; |
| 180 | + |
| 181 | + var container:Sprite = new Sprite(); |
| 182 | + addChild(container); // Adds container to stage Display List |
| 183 | + |
| 184 | + var label:TextField = new TextField(); |
| 185 | + label.text = "Visual Element"; |
| 186 | + container.addChild(label); // Nested inside container |
| 187 | + |
| 188 | + // Access properties |
| 189 | + container.x = 100; |
| 190 | + container.y = 50; |
| 191 | + container.alpha = 0.5; // transparency |
| 192 | + ``` |
| 193 | + - |
| 194 | + - ## Vector Graphics Drawing API |
| 195 | + collapsed:: true |
| 196 | + - ```actionscript |
| 197 | + import flash.display.Shape; |
| 198 | + |
| 199 | + var box:Shape = new Shape(); |
| 200 | + addChild(box); |
| 201 | + |
| 202 | + // Clear and draw vector shape |
| 203 | + box.graphics.clear(); |
| 204 | + box.graphics.lineStyle(2, 0xFF0000); // 2px red outline |
| 205 | + box.graphics.beginFill(0x0000FF); // Blue fill |
| 206 | + box.graphics.drawRect(0, 0, 100, 100); // draw 100x100 square |
| 207 | + box.graphics.endFill(); |
| 208 | + ``` |
| 209 | +- |
| 210 | +- # E4X (ECMAScript for XML) |
| 211 | + - ## XML Parsing and Navigation |
| 212 | + collapsed:: true |
| 213 | + - ActionScript parses XML natively without using DOM nodes lookup. |
| 214 | + - ```actionscript |
| 215 | + var menuXML:XML = |
| 216 | + <menu> |
| 217 | + <item category="drink"> |
| 218 | + <name>Coffee</name> |
| 219 | + <price>2.50</price> |
| 220 | + </item> |
| 221 | + <item category="food"> |
| 222 | + <name>Sandwich</name> |
| 223 | + <price>5.99</price> |
| 224 | + </item> |
| 225 | + </menu>; |
| 226 | + |
| 227 | + // Accessing nodes using dot notation |
| 228 | + trace(menuXML.item[0].name); // Coffee |
| 229 | + |
| 230 | + // Reading attributes using '@' character |
| 231 | + trace(menuXML.item[1].@category); // food |
| 232 | + |
| 233 | + // Filtering nodes dynamically (XPath equivalent in E4X) |
| 234 | + var cheapItems:XMLList = menuXML.item.(price < 3.00); |
| 235 | + trace(cheapItems.name); // Coffee |
| 236 | + ``` |
| 237 | +- |
| 238 | +- # Memory Management |
| 239 | + - ## Weak References to prevent Leaks |
| 240 | + collapsed:: true |
| 241 | + - Strong event listeners keep visual objects alive in memory, causing leaks: |
| 242 | + - ```actionscript |
| 243 | + // Use 5th argument of addEventListener to establish a weak reference: |
| 244 | + // addEventListener(type, listener, useCapture, priority, useWeakReference) |
| 245 | + |
| 246 | + stage.addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true); // true = weak reference |
| 247 | + // When this object is removed from display, GC can clean it up automatically |
| 248 | + ``` |
| 249 | +- |
| 250 | +- # Build Tools & Project Execution |
| 251 | + - ## Compilers |
| 252 | + - **mxmlc**: Adobe standard compiler for building `.swf` files. |
| 253 | + - **adt**: AIR Developer Tool for packaging apps for desktop/mobile (.exe, .dmg, .apk, .ipa). |
| 254 | + - ```bash |
| 255 | + # Compile ActionScript file to SWF |
| 256 | + mxmlc src/Main.as -o bin/Main.swf -compiler.source-path=src |
| 257 | + |
| 258 | + # Run AIR desktop debugging application |
| 259 | + adl src/Main-app.xml |
| 260 | + ``` |
| 261 | +- |
| 262 | +- # More Learn |
| 263 | + - Explore valuable resources for ActionScript: |
| 264 | + - |
| 265 | + - [Adobe ActionScript 3.0 Reference](https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/) |
| 266 | + - [HARMAN Adobe AIR (Official Developer SDK Portal)](https://airsdk.harman.com/) |
| 267 | + - [OpenFL (Modern Haxe-based alternative to Flash)](https://www.openfl.org/) |
| 268 | + - [Starling Framework (GPU-accelerated 2D graphics)](http://gamua.com/starling/) |
0 commit comments