This is the lighter side of we love... opinions, news, fun stuff, our friends and partners. Enjoy, comment and spread the love...

« back to blog

Flash geek post on getDefinitionByName

Found this little gem of a util whilst working on an extendable list view data grid in AS3. Saved most of my hair from being pulled out too. So what is getDefinitionByName and what can it do:


Situation
You have a display object that you have exported from your Flash library. Let’s call it StarIcon. You want to use this in a class that displays multiple instances of icons that specify when creating a instance of that class. In this situation, my class (ratingClass) will show the starIcon as a rating system. So we could have loads of these stars being created when using this class.

The problem
When you create an instance of the ratingClass you can setup a new StarIcon in its constructor. Then use that StarIcon as many times as you need. This compiles fine, but then on running your little rating app you find that only one StarIcon is showing. What’s up with that!?

The Cause
When you created the instance of ratingClass, you passed that StarIcon with it. That was just one new instance of the StarIcon. So your ratingClass is just referencing the StarIcon and that’s why there is only one.

Workarounds
For each item you want to rate, you could create a new array of new StarIcons when creating a new constructor. That could be 5 new StarIcons for each item, which seems a waste as you may only use 2 icons.

Or

Get your ratings class to load new StarIcons as and when it needs. So you are effectively hardcoding a display object into your class. This means you lose some extendibility of your class.

Solution
Ok, I took a long time getting to the point of this blog, but we are here now.
getDefinitionByName allows you to specify a class name in a string and then create new instances of that class.
For example, in our rating system I pass a string of the StarIcon class (var my_icon:String = “StarIcon”) during my ratingClass constructor. When I need to display a new StarIcon, I create a new Class definition using getDefinitionByName(my_icon) as Class. Now I have my definition, I can use it to create an instance of my StarIcon (var icon:Sprite = (new icondef() ) as Sprite;
Add the icon to the display stack and you can now create as many icons as you need. Below is the full code example:

//*** Main document class **
public class Films extends Sprite
{
public function Films():void
{
// declare a new rating of a film (Film name, rating icon, films rating out of 5)
var rating:Rating = new Rating("A few good men", StarIcon, 1)
addChild(rating);
}
}

// *** Rating Class ***

import flash.utils.getDefinitionByName;

public class Rating extends Sprite
{
public function Rating(fileName:String, theIcon:String, itsRating:Number):void
{
// There would be a lot of code here to setup the film name and position it correctly. But for this example
// we just need the icon code

// First we need to create our definition class, which will hold our icon
var ratingIcon:Class = getDefinitionByName(theIcon) as Class;
// Thats it, one line of code – really easy. Next we use the ratingIcon to create an instance of an icon we
// can use
var myIcon:Sprite = ( new ratingIcon() ) as Sprite;
// Because ratingIcon is just class we need to specify the instance as a sprite.
// Now we have an icon we can use. So let add it to the display stack
addChild(myIcon);
// Done! Since this system would require as many icons as the film is rated, you would need to put the
// code in a loop and store your icons in an array.
}
}

This saves a lot of time creating new instances of an icon and passing it to a class constructor. Saved me pulling my hair out too!

No Comments »

Leave a Reply