Skip to main content
Known Participant
November 23, 2011
Question

Accessing a class from a loaded SWF

  • November 23, 2011
  • 3 replies
  • 1956 views

In Flash Professional, I drew a shape, converted it to symbol, linked it to class Symbol1 (extends MovieClip) which is generated at run-time, and saved the SWF file as shape.swf.

Now my main application wants to load shape.swf and create multiple instances of Symbol1 but I get ReferenceError when trying to access the class Symbol1.

Below is my main application's code. Errors thrown are mentioned in comments.

public class MovieClipTest extends Sprite
{
    public function MovieClipTest()
    {
        var url:String = 'shapes.swf';
        var l:Loader = new Loader();
        l.contentLoaderInfo.addEventListener(Event.COMPLETE, f);
        l.load(new URLRequest(url), new LoaderContext(false, ApplicationDomain.currentDomain));
    }
   
    private function f(event:Event):void
    {
        var content:Sprite = (event.currentTarget as LoaderInfo).content as Sprite;
       
        trace(getQualifiedClassName(content.getChildAt(0))); // output: Symbol1
        trace(ApplicationDomain.currentDomain.getDefinition('Symbol1')); // throws Error #1065: Variable Symbol1 is not defined.
        trace(getDefinitionByName('Symbol1')); // throws Error #1065: Variable Symbol1 is not defined.
    }
}

This topic has been closed for replies.

3 replies

Kenneth Kawamoto
Community Expert
Community Expert
November 24, 2011

Your "Symbol" is obviously not in the current ApplicationDomain, but in the loaded SWF's domain:

var symbolClass:Class = event.target.applicationDomain.getDefinition("Symbol") as Class;

var symbolInstance:MovieClip = new symbolClass() as MovieClip;

Also because you're loading a SWF, the content of the Loader is a MovieClip, not a Sprite.

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

OmerererAuthor
Known Participant
November 24, 2011

Thanks! That worked. I need to revise application domains.

kennethkawamoto2 wrote:


Also because you're loading a SWF, the content of the Loader is a MovieClip, not a Sprite.

MovieClip extends Sprite so it doesn't make a difference.

Kenneth Kawamoto
Community Expert
Community Expert
November 24, 2011

MovieClip extends Sprite so it doesn't make a difference.

That's true. Then why don't you use DisplayObject or even Object instead?

--

Kenneth Kawamoto

http://www.materiaprima.co.uk/

OmerererAuthor
Known Participant
November 23, 2011

An update on my problem: when I compile that same code with Flash Professional, it works perfectly but compiling it as an ActionScript application in Flash Builder gives those errors I mentioned earlier. Unfortunately, I have to target the latter.

kglad
Community Expert
Community Expert
November 23, 2011

explicitly construct that Symbol1 class.

OmerererAuthor
Known Participant
November 23, 2011

I did but that didn't help either. Thanks for your replies by the way.

kglad
Community Expert
Community Expert
November 23, 2011

there's no problem with your code.  there's no class named Symbol1 found in your shapes.swf

OmerererAuthor
Known Participant
November 23, 2011

kglad wrote:


there's no class named Symbol1 found in your shapes.swf

Why does getQualifiedClassName() output "Symbol1" then? I did select the checkbox that says "Export for ActionScript" in Symbol1's properties dialog box.