getDefinitionByName
Posted: January 25th, 2008 | Author: v0id | Filed under: Actionscript3 | 7 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.