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

3 comments:

Dan Lavender said...

If you're getting this error:
ReferenceError: Error #1065: Variable SomeVariableClass is not defined

Then you need to read this below

Reflection in Actionscript 3.0/Flex 2

It seems you cannot dynamically create custom classes if they're not previously instantiated in memory.

Anonymous said...

Thanks a lot for posting! This solved a problem I was having today that had me scratching my head for more time than I'd like to mention.

wow gold said...

Weekends to peopleig2tmean that they can have a two-day wowgold4europe good rest. For example, people gameusdcan go out to enjoy themselves or get meinwowgoldtogether with relatives and friends to talk with each storeingameother or watch interesting video tapes with the speebiewhole family.
Everyone spends agamegoldweekends in his ownmmoflyway. Within two days,some people can relax themselves by listening to music, reading novels,or watchingogeworld films. Others perhaps are more active by playing basketball,wimming ormmorpgvipdancing. Different people have different gamesavorrelaxations.
I often spend weekends withoggsalemy family or my friends. Sometimes my parents take me on a visit to their old friends. Sometimesgamersell I go to the library to study or borrow some books tommovirtexgain much knowledge. I also go to see various exhibition to broadenrpg tradermy vision. An excursion to seashore or mountain resorts is my favorite way of spending weekends. Weekends are always enjoyable for me.
igxe swagvaultoforu wowgold-usaignmax wowgoldlivebrogame thsaleGoldRockU