getDefinitionByName
Posted: January 25th, 2008 | Author: v0id | Filed under: Actionscript3 | 8 Comments »Imagine you have a .swc file that contains objects named from "mcObj0" to "mcObj11", and want to add those objects randomly to the screen. One way to do this would be:
A better approach is one that uses 'getDefinitionByName'. This method allows to create objects dinamically and results in less code written. That means we could refactor the above code into:
Look ma, only three lines, that's great! To use this method you also have to remember to add the compiler parameter "-include-libraries". Here is how to do it:
- In the project properties choose "ActionScript Compiler"
- On the "Additional compiler arguments" field add the following:
-include-libraries PATH_TO_SWC
The path should be the absolute path, relative won't work.
å¼ å®¶ç•Œæ—…æ¸¸
Hey, thanks for this, I was wondering WTF was going on for a while
FYI, I got a relative path working. If you’re using FlashDevelop (3.0.0 Beta9), it needs to be relative to your .as3proj file.
This worked: -include-libraries dev\swc\videos.swc
Hi,
I have something similar to this, all though not the same, I have no experience with SWC files.
I have defined 3 MovieClips:
var man1DR:MovieClip = new MovieClip();
var man1UL:MovieClip = new MovieClip();
var man1UR:MovieClip = new MovieClip();
I want to add one of these to a movieClip as a child, depending on a variable ‘dir’.
“man1″ + dir gives me the name of one of those 3 movieClips, but if I do:
object.addChild(“man1″+ dir);
I get an error as it is a string and not the actual movieClip. Do you know how I fix this??
If I do this:
var man:Class = getDefinitionByName(“man1″ + dir) as Class;
var obj = new man() as DisplayObject;
object.removeChildAt(0);
object.addChild(obj);
I get this error:
ReferenceError: Error #1065: Variable man1UR is not defined.
When, as you can see above, it is defined…?
Dan:
Your problem is that getDefinitionByName() accepts class names as an input parameter, not variable names. You could use your method if you created the MovieClips as separate classes, or exported them from the Flash GUI. However, I think what you’re looking for is the ability to reference dynamic variable names. This is done using the this["variable_name"] syntax. Here’s a quick example similar to yours:
package
{
import flash.display.DisplayObject;
import flash.display.SpreadMethod;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Dictionary;
import flash.utils.getDefinitionByName;
public class Main extends Sprite
{
private var man1Red:MovieClip = new MovieClip();
private var man1Green:MovieClip = new MovieClip();
private var man1Blue:MovieClip = new MovieClip();
public function Main():void
{
//red fill
initGraphic(man1Red, 0xFF0000);
//green fill
initGraphic(man1Green, 0×00FF00);
//blue fill
initGraphic(man1Blue, 0×0000FF);
showDirection(“Red”);
}
private function initGraphic(mc:MovieClip, color:uint):void
{
with (mc)
{
graphics.beginFill(color);
graphics.drawRect(0, 0, 50, 50);
graphics.endFill();
}
}
private function showDirection(color:String):void
{
if (this.numChildren > 0)
{
removeChildAt(0);
}
addChildAt(this["man1" + color], 0);
}
}
}
Relative paths seem to work fine in Flex 3 on a Mac…
-include-libraries ./../libs/myswc.swc
But I haven’t tried opening the same file with a windows version yet…
Hey,
I want to load classes dynamically and reflect their properties at runtime without actually having any references, or importing them. Is there any way to do this without adding the swc’s to the -include-libraries field? In my case there can be any number of swc’s in a directory at a time that I want to load classes from. Some could be removed, and some could be added. So having this command line argument doesn’t really work for me. Thanks!
[...] use any imports or dummy variables. I would like to give credit to v0id from Dreaming in Flash for this blog post that explained to me how to use this [...]