var _ldr:Loader=new Loader
var _req:URLRequest=new URLRequest

_req.url="AAA.swf"
_ldr.load(SWF);
_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderComplete);

function onLoaderComplete(e:Event):void{
var _contentMC:MovieClip=_ldr.content as MovieClip;

//這段語法是重點 必須先把_ldr轉換成MovieClip
_contentMC.addEventListener(Event.ENTER_FRAME,picSlide);
_contentMC.btn.addEventListener(MouseEvent.CLICK,enter_hotPoint);
_contentMC.btn.addEventListener(MouseEvent.CLICK,enter_hotPoint);

//這段語法用來控制AAA.swf裡面的btn按鈕進行function
}


function enter_hotPoint(e:MouseEvent){
trace("hello");
}

 

這樣就當你按下AAA.swf裡面的btn按鈕時 就可以執行在當下的function trace("hello");

可是問題來了

假如今天你load AAA.swf裡面有btn的按鈕沒錯,但是你可能其他的原因變成load BBB.swf

BBB.swf不見得裡面有 btn的按鈕 可能叫做btn2 或是其他的名子怎麼辦呢??

這時候就要改用 dispatchEvent 看下面

不要用xxxx.xxx.xxx.addEventListener這樣寫

AAA.swf
裡面的按鈕按下後, 發出一個自定的事件往上層打


dispatchEvent
(new Event("AAAClick",true));



然後你的主程式只要寫


var _ldr:Loader = new Loader();
_ldr.addEventListener("AAAClick" , _aaaClickHandler);
_ldr.load(new URLRequest("AAA.swf"));
function
aaaClickHandler(e:Event):void{
trace(e.currentTarget , e.target)
}


這樣就可以知道AAA.swf發出事件了

以上內容轉載http://www.flycan.com.tw/board/modules/newbb/viewtopic.php?topic_id=1980&forum=9&post_id=

實際操作

===AAA.SWF

btn.buttonMode=true;
btn.addEventListener(MouseEvent.CLICK, btn_dispatchMyEvent);

function bt_dispatchMyEvent(e:MouseEvent){
btn.dispatchEvent(new Event("hotPoint",true));
}



===MAIN.SWF

function START(){
_req.url=AAA.SWF;
_ldr.load(_req);
_ldr.x=-XCENTER;
_ldr.y=-YCENTER;
_container.addChild(_ldr);
_ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgress);
_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderComplete);
}

function onLoaderComplete(e:Event):void{
var _contentMC:MovieClip=_ldr.content as MovieClip;
_contentMC.addEventListener("hotPoint",enter_hotPoint);
}

function enter_hotPoint(e:Event):void{
hotPoint=e.target.name;
SWF.url=hotPoint+".swf";
_ldr.load(SWF);
}
arrow
arrow
    全站熱搜

    JEFF 發表在 痞客邦 留言(0) 人氣()