How to load a swf and interact with it?
Asked Answered
N

1

6

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.

Nitrobacteria answered 24/6, 2016 at 10:14 Comment(12)
The property is numChildren, and probably the SWF you are loading is written so that it adds its content directly to stage.Brendin
@Brendin then how could I interact with it? MovieClip#stage return null.Nitrobacteria
While you wait for an SWFLoader / Flex related answer... What happens if you use AS3 code (inside a </mx:Script>) to load your SWF using Loader API etc?Tiein
@Tiein Yes, I tried that too, and the result is the same. Any other better way? I just want to interact with the flash, like inputing numbers to its text field and click one of its buttons etc.Nitrobacteria
Show us some code. It could be that there is some other flex component over the swf blocking the interaction or it could be that the swf itself is broken.Xenolith
@SefierTang, I don't get what your problem is exactly. It's correct that the SWF will be loaded as MovieClip object. You add it to the stage (use addChild). When it's visible, it's ready for interaction. To eliminate issues : You have the loaded SWF in same place as your own app? Meaning you're not loading SWF from some other web server? If it's on your server then at least it shows up? At this point either show temporary link to the SWF so we can test ourselves, or show some code so we see what could be "blocking" it from usage...Tiein
@Tiein it's loaded locally and it is visible, but I can't interact with it or I don't know how. I call loader.content.numChildren and it returns 0, which means it can't be accessed? I will give you a swf file, rails-assets.oss-cn-hangzhou.aliyuncs.com/ws-0618.swf. Thank you.Nitrobacteria
How are you loading the SWF and how are you adding the SWF to stage? What else does the parent container of the SWF contain? By default you should be able to interact with the swf loaded. See also my previous comment for possible errors.Xenolith
@RobinvandenBogaard I loaded it by Loader.load, and addElement, "Interact" means get its element and click on it by program, I want to do flash automation. Not by click it by hands.Nitrobacteria
I need more details on the SWF you're trying to load because it greatly depends on its setup if you are able to use its content. Do you want to use the assets in from SWF or is it an application on its own?Xenolith
@RobinvandenBogaard, he already put a temporary SWF link. Thing is, if you open in a new tab it just shows a blank white canvas, but if you save link & load from HDD it will show something. It's all Chinese texts and with only one working button that when clicked appears to do nothing. I don't know what the consequences of that "pressing" was...Tiein
@SefierTang, did you make the SWF? If you know the exact function & variable names then you could write code to update vars or do things (run some functions, etc). If the SWF is made by someone else, then maybe it'll be easier to make your own app to re-create what that SWF does. Something like a clone / copy product.Tiein
I
3

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.

Isadoraisadore answered 30/6, 2016 at 23:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.