Getting Started with ActionScript

Getting Started with ActionScript

ActionScript is an object-oriented programming language that has a wide reach on the web, the desktop, mobile devices, and beyond. If you want to be able to hit many targets with one code base, ActionScript is a great way to do so; it also lets you build a variety of application types, from 2D and 3D games to enterprise applications, video players, and more. In this workshop, expert software engineer Joseph Labrecque teaches you the very fundamentals of programming in ActionScript 3 using both Flash Professional and Flash Builder. You will learn how to use core constructs and object types such as variables, arrays, and functions, as well as more advanced types like video, sound, XML, and JSON.

Access the full workshop.

Grab the detailed release [PDF].

The following three sample movies are public:

Creating Custom Context Menus in Flash

The assets for my 5 minute quick tip talk for the 2011 Adobe Education Leader Institute are located here. This is the completed example (Flash Professional CS5.5):
[download AEL2011_FlashContextMenu]

Here is an image preview:

Custom Context Menus!

Here is the SWF:

Flash Player 10.2 or greater is required!

And here be the code bits!

package  {
	import flash.display.Sprite;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	import flash.ui.ContextMenuBuiltInItems;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.events.ContextMenuEvent;

	public class CustomMenu extends Sprite {
		private const MENUITEM_FVM:String = "©2011 Fractured Vision Media, LLC";
		private const MENUITEM_JOSEPH:String = "Joseph Labrecque";

		public function CustomMenu(){
			buildMenu();
		}

		private function buildMenu():void {
			var myMenu:ContextMenu = new ContextMenu();
			myMenu.hideBuiltInItems();

			var menuItem0:ContextMenuItem = new ContextMenuItem(MENUITEM_FVM);
			menuItem0.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, visitURL);

			var menuItem1:ContextMenuItem = new ContextMenuItem(MENUITEM_JOSEPH);
			menuItem1.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, visitURL);
			menuItem1.separatorBefore = true;

			myMenu.customItems.push(menuItem0);
			myMenu.customItems.push(menuItem1);

			this.contextMenu = myMenu;
		}

		private function visitURL(e:ContextMenuEvent):void {
			var site:String = "";
			switch(e.target.caption){
				case MENUITEM_FVM:
					site = "http://fracturedvisionmedia.com/";
					break;
				case MENUITEM_JOSEPH:
					site = "http://josephLabrecque.com/";
					break;
			}
			navigateToURL(new URLRequest(site), "_blank");
		}

	}

}

CodeBass Article: AS3 Audio Libraries

Audio Tests

An article I had written on using a variety of ActionScript and Alchemy-based audio encoding libraries in Flash Player has been published over at CodeBass.

Check it out and even play with the demo set from within your web browser. Full source code and links to referenced AS3 libraries is also available.

Nice to finally get something up there!

InsideRIA: AIR 2.0 NativeProcess API – What’s It Good For?

Just a quick post to encourage everyone to check out my article over at InsideRIA:

AIR 2.0 NativeProcess API – What’s It Good For?

While there are a few examples available for connecting AIR applications to native operating system processes today, most of them are basic “Hello World” examples, or happen to be purely theoretical. I’ve only come across one or two real world examples of applications that leverage this functionality in a meaningful way. In this article, I’m going to talk about the DropFolders application which uses AIR 2.0 NativeProcess APIs to make working with HandBrake a more streamlined, hands-off process.

Read the full article!

Strange ActionScript dispatchEvent / addEventListener Gotcha

I came across a strange situation over the weekend where a custom event being broadcast through dispatchEvent() was not being caught by the registered listener object. To make things even more bizarre, hasEventListener() definitely showed the event as registered, and I could even trace the type out through willTrigger().

Relevant example code is below:

1
2
3
4
5
6
7
8
	private function fileDoesNotExist(e:IOErrorEvent):void {
		textSlide = new TextSlide(data.comment);
		textSlide.addEventListener("slideReady", writeIt);
	}
 
	private function writeIt(e:Event):void {
		// Okay!
	}

After a bunch of experimentation and comparing this structure with similar classes, I found that simply delaying the event dispatcher with a Timer by a few milliseconds resulted in a successful dispatch and capture.

Now, the class transmitting the event is a very simple one that simply has a constructor function that renders some bitmapData from injected data (severely shortened for this example):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package edu.du.image {
	import edu.du.image.ResizeImage;
 
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
 
	[Event(name="slideReady", type="flash.events.Event")]
 
	public class TextSlide extends Bitmap {
 
		public function TextSlide(t:String, bitmapData:BitmapData=null, pixelSnapping:String="auto", smoothing:Boolean=true){
			super(bitmapData, pixelSnapping, smoothing);
 
			/*
 
			perform a set of processes to render text to bitmapData
			almost all is removed in this example
			you get the idea...
 
			*/
 
			var timer:Timer = new Timer(500, 1);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, eGo);
			timer.start();
		}
 
		private function eGo(e:TimerEvent):void {
			dispatchEvent(new Event("slideReady"));
		}
	}
}

Putting the Timer delay on there fixes everything. The only thing I can think of is that the constructor function must execute so quickly that the listener on the parent just hasn’t completely registered yet!

One of those things that can drive you nuts and waste hours of time.