Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a swf and interact with it?

I have tried SWFLoader, but the problem is the loaded content is MovieClip and I don't know how to interact with it, and the MovieClip#numChildren is zero.

And by the way, I can't pass the flashvars to the swf.

like image 560
Sefier Tang Avatar asked Jun 24 '16 10:06

Sefier Tang


1 Answers

Firstly, you should know that there is no exact answer to your question as it depends on your loaded SWF (you know it or not, its display list, ...) but I'll put a simple example to explain things and you have to adapt it to your case.

For this example, let's say that we have a very simple SWF (the loaded SWF) which contain a TextField (called txt_url) and a button (a MovieClip, called btn_go).

The btn_go button will open the URL entered in the txt_url TextField.

For our second SWF (the loader), we will use a Loader object to load our first one (which is in this case will be the Loader.content) and then we will set the URL (the txt_url text) and trigger the click event on the btn_go button.

So here is an example of the code of our loader.swf :

var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, on_SWFLoad);
    loader.load(new URLRequest('loaded.swf'));
    addChild(loader);

function on_SWFLoad(e:Event): void
{   
    // get our loaded SWF
    var loaded_swf:DisplayObjectContainer = DisplayObjectContainer(loader.content);

    // because we know our target objects, we can use "getChildByName()"
    // set the URL
    TextField(loaded_swf.getChildByName('txt_url')).text = 'http://www.example.com';

    // open the URL in the browser by triggering the click event on the "btn_go" button
    MovieClip(loaded_swf.getChildByName('btn_go')).dispatchEvent(new MouseEvent(MouseEvent.CLICK));     
}

This example will directly set and open the URL in the browser after loading the SWF, of course we can execute that action after clicking a button or something else but it's just a simple example to show you how you can do ...

Now, the problem is when we don't know anything about the loaded SWF and its children (names, depths, ...), in this case we should do more effort to do what we want : we should traverse the entire display list of the loaded SWF to identify the target objects.

Returning to our example and let's say that we only know that there are a TextField and a button in the stage, so our code can be like this for example :

function on_SWFLoad(e:Event): void
{
    var loaded_swf:DisplayObjectContainer = DisplayObjectContainer(loader.content);
    var num_children:int = loaded_swf.numChildren;

    for(var i:int = 0; i < num_children; i++)
    {
        var child:DisplayObject = loaded_swf.getChildAt(i); 

        if(child is TextField)
        {
            trace(child.name);          // gives : txt_url
            TextField(child).text = 'http://www.example.com';
        }
        else 
        {
            if(child.hasEventListener(MouseEvent.CLICK))
            {
                trace(child.name);      // gives : btn_go
                child.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
            }
        }
    }
}

Again, it's a very simple example just to show how we can proceed ...

...

Then about passing values (params) between SWFs, take a look on my answer of this question where you have a little example for that.

For more about Display programming (display list, display object, display object container, ...) take a look here.

Hope that can help.

like image 64
akmozo Avatar answered Oct 30 '22 22:10

akmozo