//for迴圈為4個拼圖影片片段加入事件偵聽 for(var i:int=1;i<=4;i++){ //with()函數,統一為4個拼圖影片片段指定事件 with(this['pic'+i+'_mc']){ //設定拼圖影片片段元件buttonMode模式為true buttonMode = true; //拼圖偵聽MOUSE_DOWN事件,發生自訂drag函數 addEventListener(MouseEvent.MOUSE_DOWN,drag); //拼圖偵聽MOUSE_UP事件,發生自訂dragStop函數 addEventListener(MouseEvent.MOUSE_UP,dragStop); } } // 上面那段程式等同下面效果 // 設定拼圖影片片段元件buttonMode模式為true // pic1_mc.buttonMode = true; // pic2_mc.buttonMode = true; // pic3_mc.buttonMode = true; // pic4_mc.buttonMode = true; //拼圖偵聽MOUSE_DOWN事件,發生自訂drag函數 // pic1_mc.addEventListener(MouseEvent.MOUSE_DOWN,drag); // pic2_mc.addEventListener(MouseEvent.MOUSE_DOWN,drag); // pic3_mc.addEventListener(MouseEvent.MOUSE_DOWN,drag); // pic4_mc.addEventListener(MouseEvent.MOUSE_DOWN,drag); //拼圖偵聽MOUSE_UP事件,發生自訂dragStop函數 // pic1_mc.addEventListener(MouseEvent.MOUSE_UP,dragStop); // pic2_mc.addEventListener(MouseEvent.MOUSE_UP,dragStop); // pic3_mc.addEventListener(MouseEvent.MOUSE_UP,dragStop); // pic4_mc.addEventListener(MouseEvent.MOUSE_UP,dragStop); // ------------------------------------------------------------------------- //drag函數,定義在圖片上按下按鈕時發生動作 function drag(me:MouseEvent):void{ me.currentTarget.useHandCursor = true;//這個影片片段顯示手指游標 var object=me.target;//取得目標名稱 //將目標交換到容器(這個場景)的最上方 this.setChildIndex(object,this.numChildren-1); //開始拖曳目標 me.currentTarget.startDrag(); }