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');
}
此脚本显示外部swf文件。但MouseEvent.CLICK无法正常工作。为什么呢?
答案 0 :(得分:2)
您正在加载什么作为外部资源。如果是SWF,那么那里有什么东西。此外,我建议在加载swf后附加监听器。即在Event.COMPLETE处理程序中。这是文档中的代码:
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();
}
}
}
所以尝试在completeHandler方法中附加监听器。
答案 1 :(得分:1)
如果您在屏幕上看到内容,那么问题可能在于点击事件处理:
click事件可能针对的是置换列表中的某些内容,而不是加载器(如加载器内容或其中的子项 - 也就是说,您没有单击加载器,而是单击其中的MovieClip)。根据您的要求,您可以尝试几种不同的东西:
使用useCapture = true监听(这将注册加载的电影中任何内容的任何点击):
loader.addEventListener(MouseEvent.CLICK, onClick, true);
在加载程序上禁用mouseChildren。这意味着点击事件将在加载器处停止,而不是在加载器的子项中注册:
loader.mouseChildren = false;
loader.addEventListener(MouseEvent.CLICK, onClick);