Monday, August 24, 2009

how do you integrate library assets into your code?

I'm curious as to how folks reference library assets in their code. (I'm talking about Flash projects, not Flex projects).

I don't know if I'm doing it the wisest way, but I create a package called flaBridge. In that package, I make a class for each library item. Often the class is empty...


package com.grumblebee.project.flaBridge
{
import flash.display.Sprite;

class CompanyLogo extends Sprite
{
public function CompanyLogo()
{

}
}
}


I then link to this class in the fla Library.

This gives me a placeholder for adding specific functionality. It also stops my IDE (FDT) from complaining that I'm referencing classes that don't exist.

If I'm referencing an instance that's on the fla Stage, I do it this way:

var logo : CompanyLogo = this[ "logo" ]; //on stage

3 comments:

  1. If you are referencing assets from the same FLA/SWF as you are compiling, you can use flash.utils.getDefinitionByName() or loaderInfo.getDefinition() to pull out your library symbol:

    e.g. var ball:MovieClip = (new getDefinitionByName("Ball")) as MovieClip;

    There is a difference between the two but not one you have to worry about unless you're loading a swf into another swf and trying to use this.

    If you are pulling in library symbols from a different SWF altogether, e.g. an "assets" swf, you can use the above technique:

    e.g. swfLoaded.contentLoaderInfo.getDefinition()

    ...or use the [Embed] tag for Flex or AS3 project using the Flex SDK, and you can also use this tag with code compiled for an FLA in Flash CS4. The thing is it tends to convert any symbols to Sprites, so the workaround is to create a class as you have above, use the Embed tag above the line that begins "public class", and have that extend MovieClip. It will then be treated as a MovieClip and allow you to add custom methods.

    ReplyDelete
  2. Cool info, Richard, but I'm don't quite get why you'd choose getDefinitionByName over new Whatever();

    ReplyDelete
  3. Yeah the reason is because you don't get the errors you mention in Flex Builder/FDT due to those classes not existing outside of the FLA. loaderInfo.getDefinition() is also the only way to get a class from another loaded swf at runtime.

    ReplyDelete