25 March 2008

The Statistics of Argh!

You know those days when you just cannae take it any more (Capt'n) and end up typing "AAAAARRRRRRRRRRRRRRGGGGGHHHHH!!!!" into Google? Yet have you ever wondered how many AAAA's RRRR's GGGG's and HHHH's to use?

Well here's the answer:
http://osteele.com/words/aargh

The most common spelling appears to be "argh", which is really more of an apathetic sigh than a gut wrenching scream of annoyance.

Only 14 people seem to have gone with my spelling of 5 A's and 15 R's. You certainly need more than one A to get it going and there definitely needs to be more R's in the "argh" as I consider that the "body" of the scream when it audibly starts to blend with the multitude of A's.

What the statistics don't show are the G's and H's which, in my opinion, round the scream off. You need a good few G's to release that guttural sound of pain, followed by a few strained, aspirated H's as your lungs, exhausted of air, begin to collapse.

You see? T'interweb has its uses!

Back to work.... AAAAARRRRRRRRRRRRRRGGGGGHHHHH!!!!

18 March 2008

Use Weak References for Your Event Listeners

Please read this:

http://www.gskinner.com/blog/archives/2006/07/ as3_weakly_refe.html

Using weakly referenced event listeners means an object can be cleared during Garbage Collection even if there's an event listener acting upon it.

It's very useful when you're spawning / deleting a lot of objects, in my case bubble "bullets" that I'm firing at targets underwater.

It saved my bacon (and my forehead) as I've been banging my head on my desk wondering why objects I thought I'd removed still persisted in memory.

The fact it's not a default setting and not very well explained, nor is its importance highlighted (this should be stapled to the front of the Flash CS3/Flex DVD box), is bloody annoying.

Thanks Grant.

24 February 2008

Creating Class Instances from a DisplayObject in AS3

*** The technique below doesn't clone a class instance, but allows you to create a new class instance derived from an unknown DisplayObject ***

After selecting an item by clicking the mouse, I needed to create a new instance of the selected item to be added to an item inventory. I needed to find out what type of object was being passed into the MouseEvent handler so I could create a new object of a specific type i.e.

private function mouseHandler(e:MouseEvent):void
{
// this is only a DisplayObject - what is it?
trace(e.target);

// this is what I want to do, but how do I know if e.target is a Rock?
var newItem:IItem = new Rock();
}

The following function uses flash.utils.getQualifiedClassName() to return a string that contains the name of the class including full namespace.

It then uses flash.utils.getDefinitionByName() to return a Class object which can then be used to create a new class.

public function duplicateItem(obj:*):*
{
var className:String = getQualifiedClassName(obj).split('::').join('.');
var ClassRef:Class = getDefinitionByName(className) as Class;
var item:* = new ClassRef();
return item;
}

So to create a duplicate object, just use call the duplicateItem function. You can also cast the returned class instance, if required, as shown below:

private function mouseHandler(e:MouseEvent):void
{
var newItem:IItem = duplicateItem(e.target) as IItem;
}

29 January 2008

Flex 2: Embedding Flash 9 swfs and controling the MainTimeline

I've been embedding Flash 9 swfs into a Flex 2 project using the following code:

[Bindable]
[Embed(source="myFlash9.swf")]
private var mySwf:Class;

This creates the bindable variable "mySwf" which can be bound to the backgroundImage attribute of certain components e.g.

<mx:Canvas backgroundImage="{mySwf}" />

However much I tried, I couldn't access the timeline of that swf as it was cast as a Class type and essentially wasn't a MovieClip. Flex only permits Class or String variables when embedding in this way.

I needed to find a way to load a swf into Flex and use it as a fully accessible MovieClip. Here's what I did:
  • Create a bindable MovieClip variable
  • Load in the swf using the flash.display.Loader class
  • Assign the result from the Loader class to the bindable MovieClip variable
  • used the Image component in Flex to add the swf to the application

Here's the complete MXML code:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.*;

[Bindable]
private var mySwf:MovieClip;

private function init():void{
var bgURL:URLRequest = new URLRequest("Test.swf");
var bgLoader:Loader = new Loader();
bgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
bgLoader.load(bgURL);
}

private function completeHandler(event:Event):void {
mySwf = event.target.content as MovieClip;
mySwf.gotoAndPlay(20); // access the timeline of the loaded swf
}

]]>
</mx:Script>

<mx:Canvas width="856" height="527" horizontalCenter="0" verticalCenter="0">
<mx:Image source="{mySwf}" />
</mx:Canvas>
</mx:Application>


You can now directly access the timeline of the loaded swf using the "mySwf" variable.

Using the backgroundImage attribute caused errors as it expected a Class variable to be passed into it. Using Image seems to have got around this problem.

15 January 2008

Calgary Flash and Flex User Group Meetings

It'll be an interesting couple of months with the releases of Flex 3 and AIR and as usual, Adobe reps are doing their pre-release tours.

There are 2 events in Calgary that can't be missed: