Loading swf and using it through interface
I've created simple swf with interface:
public class Test extends MovieClip implements ITest
{
public function Test()
{
Security.allowDomain("*");
Security.allowInsecureDomain("*");
}
public function speak(str):String
{
trace(str);
return "yeah";
}
}
ITest:
public interface ITest {
// Interface methods:
function speak(str):String
}
And then I'm trying to load it:
public function SWFLoader()
{
var url='http://xxxxxxxx/test.swf';
var loadURL:URLRequest=new URLRequest(url);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loader.load(loadURL, context);
}
private function completeHandler(event:Event):void
{
var test:ITest;
test = event.target.conte开发者_高级运维nt as ITest;
test.speak("ggg");
}
So if I have test.swf in the same directory(local way) it work's fine. But if I'm placing it on the dedicated server: (event.target.content as ITest) returns null. However, I can access speak() without interface like this event.target.content.speak("a!");
How to solve this problem?
try this:
var test:ITest = ITest(event.target.content );
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f87.html
How do you share the ITest interface between your two swf ?
I imagine you have two projects one for the test.swf (the loaded one) and one for the loader (I'll call him loader.swf). I think you can't just declare the ITest interface twice (one for test.swf, one for loader.swf). If you do so, there will be two interfaces, with the same interface name, the same declared methods, but they still will be 2 different interfaces. And casting one into another will fail.
I bet that if you do (as suggested by PatrickS)
var test:ITest = ITest(event.target.content );
You will see a type error -> that's the advantage of this form of casting. This will confirm what I think : the two interfaces are different.
To really share the interface between your 2 projects, you should store it into a library (.swc file) and use that library in your 2 projects. This should solve the issue.
精彩评论