Yggdrasil 3D: World Tree in Papervision 3D and AS3

I’ve been meaning to play with Papervision for awhile, but haven’t found the time. I finally squeezed in a little time to tinker, and the result is Yggdrasil 3D. The Yggdrasil is the world tree in Norse mythology that spans the underworld, earth and the heavens.

The scene is generated entirely programmatically (no prebuilt textures or models). Parts of it were harder than I expected, but I’m starting to get a good grip on it, and plan to do a series of experiments in ActionScript 3 and Papervision 3D.

Continue reading →

Building a Static EventDispatcher in AS3

One of my favorite features of AS3 is the low-level inclusion of EventDispatcher in the AS3 framework. I do not miss the days of including EventDispatcher mix-ins in 80% of my classes in 100% of my projects. I especially like the additional features of the AS3 EventDispatcher: cancellation, custom event types (built in), preventDefault, event phases, etc.

However we have had several AS3 projects that have had a need for an EventDispatcher that is not instance-based. So, without further ado, here is an approach that we have used since our AS2 days:

package {
import flash.events.EventDispatcher;
public class MyClass {
protected static var disp:EventDispatcher;
public static function addEventListener(...p_args:Array):void {
if (disp == null) { disp = new EventDispatcher(); }
disp.addEventListener.apply(null, p_args);
}
public static function removeEventListener(...p_args:Array):void {
if (disp == null) { return; }
disp.removeEventListener.apply(null, p_args);
}
public static function dispatchEvent(...p_args:Array):void {
if (disp == null) { return; }
disp.dispatchEvent.apply(null, p_args);
}
// Other class code
}
}

Note that this differs from a Singleton approach, in that I didn’t want to force a user to extend EventDispatcher (plus I dislike getInstance()). Additionally, some of my fellow developers have noted that using the apply syntax instead of defining all the arguments with types is lazy. Since this runs through an actual EventDispatcher instance, and this class is just a proxy, I didn’t think they were necessary.

I hope someone out there finds this useful. Internally we use it quite often – and it seems to be the most reliable way to add static-access events to a class.


Update
Since I was scolded for not typing the arguments, here is an update, and a sample usage.

package {
import flash.events.EventDispatcher;
import flash.events.Event;
public class MyClass {
protected static var disp:EventDispatcher;
public static function addEventListener(p_type:String, p_listener:Function, p_useCapture:Boolean=false, p_priority:int=0, p_useWeakReference:Boolean=false):void {
if (disp == null) { disp = new EventDispatcher(); }
disp.addEventListener(p_type, p_listener, p_useCapture, p_priority, p_useWeakReference);
}
public static function removeEventListener(p_type:String, p_listener:Function, p_useCapture:Boolean=false):void {
if (disp == null) { return; }
disp.removeEventListener(p_type, p_listener, p_useCapture);
}
public static function dispatchEvent(p_event:Event):void {
if (disp == null) { return; }
disp.dispatchEvent(p_event);
}
// Public API that dispatches an event
public static function loadSomeData():void {
dispatchEvent(new Event(Event.COMPLETE));
}
}
}
MyClass.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
function onComplete(p_event:Event):void {
trace("Complete!");
}
MyClass.loadSomeData();

AIR Panel for Flash CS3 Updated

We’ve updated our panel for testing, configuring and packaging AIR applications in Flash CS3. The most significant updates are the addition of a setting for window mode (system or transparent), and support for XML round-tripping, so edits/additions to the AIR application XML file will be preserved.

We’ve also tweaked the interface, and made some other minor updates. Click over to the original post for installation instructions, and a download link. Please use extension manager to remove the previous extension prior to installing this version! Everything should still run ok, but removing the old version in the future may cause problems.

We think we’ve also isolated the issues some people were having with packaging to a bug in the JSAPI, and have added troubleshooting information about this to the end of the original post.

Thank you to everyone who tested it, and provided feedback! Please continue to do so.

Creating AIR Projects with Flash CS3


UPDATE: Official Adobe AIR support, August 21

Adobe has released official support for AIR in Flash CS3. We’ll keep the AIR panel available, but it’s unlikely it will be updated any further. Get the Flash CS3 update for AIR.


Currently, you can only create AIR (formerly Apollo) applications with FlexBuilder. Adobe has already announced that they will be adding support to Flash CS3 to create AIR projects, but we wanted a solution in the meantime. As such, we’ve extended the work that Guy Watson began with his “Test in Apollo” jsfl command, and built a panel that lets you test, configure and compile AIR applications from within Flash CS3.

Continue reading →

Accessing EnterFrame Events in non-Display Objects

Once in a while you want to be able to have an object carry out actions on a frame interval. This is simple to accomplish when you’re working with a DisplayObject, because you can just subscribe to its ENTER_FRAME event directly, but there are times when you need this functionality in a non-display object without passing around a reference to a movieclip. In some cases, you can get by with using a Timer or interval, but this is often not ideal because you have no guarantee how often the event will fire in a single frame (a timer can fire multiple times, or not at all depending on how it synchs up with a frame cycle).

Turns out the solution is ridiculously simple: Just create a flash.display.Shape instance in your object, and subscribe to its ENTER_FRAME event. DisplayObjects do not have to be on the stage to generate enterFrame events.

StyleCollection for CS3 Components

The StyleCollection class lets you create groups of styles that are applied to and automatically updated on subscribing components. It supports both instance and renderer styles (setStyle and setRendererStyle respectively), and uses component introspection to apply only relevant styles to each component. StyleCollection exposes a robust interface, including a static interface that provides global access to named styles.

Continue reading →

Variable Scrollbar Width for CS3 Components

I’ve seen a few people asking for the ability to have variable scrollbar widths in the Flash CS3 component set. Unfortunately, it wasn’t something we were able to get in for the release, but since then I have put a bit of time into the feature, and believe I have it working satisfactorily with all components. I added two styles: scrollBarWidth and scrollArrowHeight. The former controls the width of the scroll bar (or height in a horizontal scroll bar), the latter controls the height of its arrows (allowing you to have irregularly sized arrows).

It’s important to note that this modification has not seen comprehensive testing, and it is not connected with Adobe in any way. Any mistakes are my own, and I’d appreciate if you can let me know about them in the comments.

Continue reading →