Skip to main content
Inspiring
July 17, 2014
Answered

Weird click event on loaded SWF

  • July 17, 2014
  • 1 reply
  • 1177 views

Hi, recently I'm working on a project to parse and show the VAST3 ad. One thing I need to do is listen for the MouseEvent.CLICK on the loaded SWF file.

I added the loader to a sprite and add the event listener on the sprite. When I load the following SWF file, the click event fires only when I click the background. If I click on the site logo, no click event happens.

If I downloaded the SWF file to my local hard drive, everything works fine. The click event can be fired wherever I click on the ad.

The SWF that I load has all the assets embedded.

I want to find out what's causing this problem because with some SWF files I can only listen to the MouseEvent.CLICK event on the _loader.content whereas some like the one below, partially responds to click event on _loader.content and _container.

I simplified the code as listed below.

package  {

    import flash.display.Loader;

    import flash.display.Sprite;

    import flash.events.MouseEvent;

    import flash.events.Event;

    import flash.net.URLRequest;

  

    public class Main extends Sprite {

        private var _container:Sprite;

        private var _loader:Loader;

        public function Main() {

            _container = new Sprite();

          

            _loader = new Loader();

            _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

            _loader.load(new URLRequest("http://gtms04.alicdn.com/tps/i4/TB1Fzd.FVXXXXaOXXXXeflbFXXX.swf"));

          

            _container.addEventListener(MouseEvent.CLICK, onClicked);

            addChild(_container);

        }

      

        private function onComplete(event:Event):void {

            _container.addChild(_loader);

            _loader.content.addEventListener(MouseEvent.CLICK, loadedSwfClicked);

        }

      

        private function loadedSwfClicked(event:MouseEvent):void {

            trace("loaded swf clicked"); // When I click on the Logo and the animal

        }

      

        private function onClicked(event:MouseEvent):void {

            trace("clicked"); // When I click on the gradient background

        }

    }

}

This topic has been closed for replies.
Correct answer kfitfk

You need to be able to look at the code for the loaded swf to see what listeners are assigned that can bubble up and interfere with the parent.  Normally when you assign a mouse-related bit of code to a parent, the children are blocked from access.  In this case, something of the child is overriding that - you need to see the code in the child.


One of my colleagues told me that it has something to do with the Security issue. So I need to do a Security.allowDomain call like this:

private function onComplete(event:Event):void {

    Security.allowDomain(_loader.contentLoaderInfo.url);

    _container.addChild(_loader);

    //_loader.content.addEventListener(MouseEvent.CLICK, loadedSwfClicked);

}

And the click on the loaded SWF will bubble up properly.

Because the gradient background layer of the loaded SWF only contains a Bitmap, which is not an instance of InteractiveObject, it won't respond to the MouseEvent.CLICK listener on the _loader.content.

1 reply

Ned Murphy
Legend
July 17, 2014

I am not sure why the _container listener gets overridden by the child, but it can be cured to only listen to teh _Container being clicked if you disable its mousechildren property...

private function onComplete(event:Event):void { 

            _container.addChild(_loader); 

            _container.mouseChildren = false;

            _loader.content.addEventListener(MouseEvent.CLICK, loadedSwfClicked); 

}

kfitfkAuthor
Inspiring
July 17, 2014

Thanks Ned. Well, this solution works great if the loaded SWF doesn't contain any interaction like the one in my sample code. But I don't know whether the loaded SWF is user intractable in the production environment. I'll be much appreciated if someone could tell me why the click event listener is not working on some portions of _container or _loader.content.

Ned Murphy
Legend
July 17, 2014

You need to be able to look at the code for the loaded swf to see what listeners are assigned that can bubble up and interfere with the parent.  Normally when you assign a mouse-related bit of code to a parent, the children are blocked from access.  In this case, something of the child is overriding that - you need to see the code in the child.