Nips
Nips

Reputation: 13880

Why CLICK Event not working?

var url:String;
var link:String;
var w:Number;
var h:Number;

url = loaderInfo.parameters.swfurl;
link = loaderInfo.parameters.link;

var request:URLRequest = new URLRequest(url);
var loader:Loader = new Loader();

loader.load(request);
addChild(loader);


loader.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent){
        trace('clicked');
}

This script show me external swf file. But MouseEvent.CLICK not working. Why?

Upvotes: 0

Views: 1027

Answers (2)

Jeff Ward
Jeff Ward

Reputation: 19146

If you are seeing the content onscreen, then the issue is probably with the click event handling:

The click event is likely targeting something lower in the displaylist than the loader (like the loader content or a child therein - that is, you didn't click the loader, you clicked the MovieClip inside it). You can try a couple different things depending on your requirements:

  1. Listen with useCapture=true (this will register for any click on any content within the loaded movie):

    loader.addEventListener(MouseEvent.CLICK, onClick, true);
    
  2. Disable mouseChildren on the loader. This will mean the click events will stop at the loader and not register down in the loader's children:

    loader.mouseChildren = false;
    loader.addEventListener(MouseEvent.CLICK, onClick);
    
    • Note that this may cause content that wants click events in the loaded swf (like buttons) to not work. But if the loader content is not intended to be interactive, this should be fine.

Upvotes: 1

Krasimir
Krasimir

Reputation: 13549

what exactly you are loading as an external resource. If it is SWF, then is there something there. Also I'll suggest to attach the listener once the swf is loaded. I.e. in Event.COMPLETE handler. That's a code from the documentation:

package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.*;
import flash.net.URLRequest;

public class LoaderExample extends Sprite {
    private var url:String = "Image.gif";

    public function LoaderExample() {
        var loader:Loader = new Loader();
        configureListeners(loader.contentLoaderInfo);
        loader.addEventListener(MouseEvent.CLICK, clickHandler);

        var request:URLRequest = new URLRequest(url);
        loader.load(request);

        addChild(loader);
    }

    private function configureListeners(dispatcher:IEventDispatcher):void {
        dispatcher.addEventListener(Event.COMPLETE, completeHandler);
        dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
        dispatcher.addEventListener(Event.INIT, initHandler);
        dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        dispatcher.addEventListener(Event.OPEN, openHandler);
        dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        dispatcher.addEventListener(Event.UNLOAD, unLoadHandler);
    }

    private function completeHandler(event:Event):void {
        trace("completeHandler: " + event);
    }

    private function httpStatusHandler(event:HTTPStatusEvent):void {
        trace("httpStatusHandler: " + event);
    }

    private function initHandler(event:Event):void {
        trace("initHandler: " + event);
    }

    private function ioErrorHandler(event:IOErrorEvent):void {
        trace("ioErrorHandler: " + event);
    }

    private function openHandler(event:Event):void {
        trace("openHandler: " + event);
    }

    private function progressHandler(event:ProgressEvent):void {
        trace("progressHandler: bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
    }

    private function unLoadHandler(event:Event):void {
        trace("unLoadHandler: " + event);
    }

    private function clickHandler(event:MouseEvent):void {
        trace("clickHandler: " + event);
        var loader:Loader = Loader(event.target);
        loader.unload();
    }
}

}

So try to attach the listener in completeHandler method.

Upvotes: 2

Related Questions