2013-09-22 65 views
0

我想做一個簡單的項目,當你點擊一個按鈕一個可拖動的MovieClip被添加到雄鹿,當你點擊它釋放MovieClip到X/Y在您點擊的位置,然後您可以拾取MovieClip並將其拖動到一個可以銷燬自己的bin(MovieClip)中。代碼運行良好我可以使用按鈕製作多個Movieclip,並且當我將它們拖動到bin中時它們全部被銷燬,但是我不喜歡有「錯誤代碼」。AS3的作品,但我得到一個ReferenceError:錯誤#1069屬性startDrag找不到

import flash.events.MouseEvent; 
var rubbish:my_mc = new my_mc(); 
btntest.addEventListener(MouseEvent.CLICK, makeRubbish); 

function makeRubbish (event:MouseEvent):void { 


addChild(rubbish); 

rubbish.x = mouseX - 10; 
rubbish.y = mouseY - 10; 
rubbish.width = 50; 
this.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 
rubbish.buttonMode = true; 
} 

function stopDragging (event:MouseEvent):void { 
    rubbish.stopDrag() 
    event.target.addEventListener(MouseEvent.CLICK, startDragging); 
    rubbish.buttonMode = true; 
      if (event.target.hitTestObject(bin)) 
      { 
       trace("hit"); 
       event.target.name = "rubbish"; 
    removeChild(getChildByName("rubbish")); 


      } 
} 
function startDragging (event:MouseEvent):void { 
event.target.startDrag(); 
this.addEventListener(MouseEvent.CLICK, stopDragging); 
} 

回答

0

一些指針

  • Eventtarget財產並不總是似乎什麼。它實際上是指事件冒泡過程中的當前階段。嘗試使用currentTarget屬性。
  • 我還建議將stopDragging方法綁定到舞臺上,因爲有時您的鼠標在您點擊時不會超過拖動。
  • 我會使用MOUSE_UP事件而不是CLICK來標準拖動行爲。
  • 拖動時,請保留對拖動的全局引用,以便在正確的對象上調用stopDrag方法。

試試這個

import flash.events.MouseEvent; 

var rubbish:my_mc = new my_mc(); 
var dragging:my_mc; 

btntest.addEventListener(MouseEvent.CLICK, makeRubbish); 

function makeRubbish (event:MouseEvent):void { 

    addChild(rubbish); 

    rubbish.x = mouseX - 10; 
    rubbish.y = mouseY - 10; 
    rubbish.width = 50; 
    rubbish.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 
    rubbish.buttonMode = true; 

} 

function stopDragging (event:MouseEvent):void { 

    this.stage.removeEventListener(MouseEvent.MOUSE_UP, stopDragging); 

    if(dragging !== null){ 

    dragging.stopDrag(); 

    if (event.currentTarget.hitTestObject(bin)){ 

     removeChild(dragging); 

    } 

    dragging = null; 

    } 
} 

function startDragging (event:MouseEvent):void { 

    dragging = event.currentTarget as my_mc; 

    dragging.startDrag(); 

    this.stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging); 

} 
+0

嗯,我仍然得到錯誤,但現在全臺移動時,我拖累了,它不喜歡讓垃圾 –

+0

的去再次編輯與完整的代碼。現在就試試。 – shennan

+0

第41行錯誤1118將靜態類型的值強制與可能不相關的類型綁定my_mc –

相關問題