views:

44

answers:

2

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.content 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?

A: 

try this:

var test:ITest = ITest(event.target.content );

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f87.html

PatrickS
Thanks, but it's not type coercion problem. It is all about security.I tried loading swf to web server and it work's fine. It asks for crossdomain.xml even with checkPolicyFile=false and works.Now I realised that I just cannot load swf from web server when I'm running swf on local machine.
Shens
I don't understand how a security issue can cause "event.target.content as ITest" to return null but "event.target.content.speak("a!");" to work.
David
I agree with David here! Sorry, I gave you +1 but it was meant for David :))) No worries! ;) You may well have a security issue but this is not the context of your question...
PatrickS
As far as I know, we can load and add to stage a pic from web. But we can't make bitmapdata.draw() of it. Again because of security issues. Since then I believe in any reason.
Shens
In order to be able to manipulate bitmap data, you will need to set up a crossdomain policy file:http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.htmland change the first argument in your LoaderContext constructor to true
PatrickS
A: 

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.

David
It sounds reasonable, but my way works fine(if I upload swf to server and run it from web). I don't know excatly, but I believe that interfaces compiled in the same way, that why they called so.
Shens
Out of curiosity , I'd be interested to know if it fails actually. How do you explain that the following worksevent.target.content.speak("a!");Furthermore the ITest interface doesn't seemed to be shared between the two projects. the ITest object is the actual loader content. Where do you see that the loader.swf implements ITest? ITest is simply a child of the loading project.
PatrickS
Ok, I want to believe you, but how to explain that it works online? event.target.content as ITest gives me ITest object and I can work with it. Unfortunatelly I don't know how all this stuff works inside flash(in byte code). I suppose that interface just gives us a description of available methods of some object. And we can legally call them. So it's not really important where it was compiled. Also I suspect that type casting operation needs more security permissions than just accessing methods of object. Just assumptions.
Shens
Why don't you try what PatrickS suggested. If it really is a security issue, you'll see the actual error and it will help you find the issue.
David
Of course I tried it, even before. ITest() gives everywhere in my cases the same result as "as ITest".
Shens
So, if you cast it, it's null but if you call a method on it, it is not. That's really weird !
David