getDefinitionByName

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.

2 Responses to “getDefinitionByName”

  1. sdgdg Says:

    张家界旅游

  2. iestyn lloyd Says:

    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

Leave a Reply