0
votes

I have a loader swf acting as the main swf that is responsible for loading and rendering external swf's.

In the document class of the loader swf, I have a function named test.

public function test() {
    ExternalInterface.call("console.log", "Test");
}

I want to call this function from the child swf that is being loaded in using an external class known as StateManager. A new instance of the StateManager class is being created in the document class of the loader swf as can be seen below.

import com.xxxx.state.StateManager;

public class Loader extends MovieClip {

    private static var _instance:Loader;
    public static function get instance() { return _instance; }

    public var stateManager = new StateManager();

    // Other code has been ommited obviously.

}

A function is then called in StateManager which renders the new swf.

public function setActiveState(url) {
    var request = new URLRequest(url); 
    var loader = new Loader(); 
    loader.load(request);
    addChild(loader);
}

In the child swf's document class, I have attempted to call the loader swf's test function using many different methods, all of which have resulted in nothing happening and no error being produced (I have confirmed that the child swf is rendering properly). I have tried using the following code.

public class ChildSWF extends MovieClip {

    public function ChildSWF() {
        MovieClip(parent.parent).Loader.instance.test();
    }

}

As well as

public class ChildSWF extends MovieClip {

    public function ChildSWF() {
        MovieClip(parent.parent.parent).Loader.instance.test();
    }

}

and many other pieces of code that I have seen when researching this problem. If anyone could help, that would be greatly appreciated.

1
It would be helpful if you indicated the security context your child swf's will be. Are they being loaded from the same folder as the parent swf? - BadFeelingAboutThis

1 Answers

0
votes

First. Never, absolutely never name your classes as the already existing classes. You're, like, welcoming future troubles with the huge neon WELCOME display with the occasional firework blasts in the background.

Then. Your class Loader is not a member (nor a variable) of any display object and just cannot be accessed the way you try it. Classes do not work like that. They are definitions of the ApplicationDomain. You might want to find and read about it: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/ApplicationDomain.html#getDefinition()

The rest is pretty simple.

public var callBack:Function;

public function setActiveState(url, handler)
{
    callBack = handler;

    var request = new URLRequest(url); 
    var loader = new Loader; 
    loader.load(request);
    addChild(loader);
}

Then in the loaded content:

public class ChildSWF extends MovieClip
{
    public function ChildSWF()
    {
        (parent.parent as Object).callBack();
    }
}

The loaded content will call whatever method reference you will have in the callBack variable;