2013-07-16 70 views
0

我有2個對象和1個目標。當鼠標鍵關閉時,我將拖動一個對象,並在鼠標鍵合時將對象放在目標對象上。當o2處於目標狀態時,將o1拖到目標上,o2轉到它的位置,而o1不會轉向目標。在Actionscript 3中拖放

有人可以幫助我嗎?

var t1:int=0; //target is empty 

o1.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 
o1.addEventListener(MouseEvent.MOUSE_UP, stopDragging); 

function startDragging(evt:MouseEvent):void 
{ 
o1.startDrag(); 
if (o1.x == tar1.x && o1.y == tar1.y) 
{ 
    t1 = 0; 
} 
} 
function stopDragging(evt:MouseEvent):void 
    { 
    if (tar1.hitTestObject(o1) && t1 == 0) 
    { 
     o1.x = tar1.x; 
     o1.y = tar1.y; 
     o1.stopDrag(); 
     t1 = 1; //target is full 
    } 
     else 
    { 
     o1.x = 250; 
     o1.y = 200; 
     o1.stopDrag(); 
    } 
     } 

    o2.addEventListener(MouseEvent.MOUSE_DOWN, startDragging2); 
    o2.addEventListener(MouseEvent.MOUSE_UP, stopDragging2); 

    function startDragging2(evt:MouseEvent):void 
    { 
o2.startDrag(); 
if (o2.x == tar1.x && o2.y == tar1.y) 
{ 
    t1 = 0; 
} 
} 
function stopDragging2(evt:MouseEvent):void 
    { 
    if (tar1.hitTestObject(o2) && t1 == 0) 
    { 
     o2.x = tar1.x; 
     o2.y = tar1.y; 
     o2.stopDrag(); 
     t1 = 1;} 
     else 
    { 
     o2.x = 250; 
     o2.y = 140; 
     o2.stopDrag(); 
    } 
     }   

enter image description here

回答

1

的訣竅是,你o2對象接收第一MouseEvent.ROLL_OVER事件,當你拖動o1,然後接收MOUSE_UP事件以及o1。然後,兩個事件監聽器分別觸發。第一個監聽stopDragging激活,檢查t1「全局」變量,發現它爲0 - OK,o1設置在target,然後stopDragging2觸發器,咣噹,t1不爲零,所以else分支觸發器和o2被替換回到起始位置。正常的解決方案是通過一個事件的target屬性的添加,並從你的物體移除聽衆,和尋址的對象,像這樣:

o1.addEventListener(MouseEvent.MOUSE_DOWN,startDragging); 
o2.addEventListener(MouseEvent.MOUSE_DOWN,startDragging); 
// note that listeners are not separate functions! We will 
// be using event.target to check which object is being dragged 
function startDragging(e:MouseEvent):void { 
    e.target.removeEventListener(MouseEvent.MOUSE_DOWN,startDragging); 
    // surprise! This object will not be moved via MOUSE_DOWN, 
    // because it's already being moved 
    e.target.addEventListener(
    e.target.startDrag(); // drag 
} // no alteration to t1, as we don't really care if we're dragging 
// our object off target - yet 
function stopDraggging(e:MouseEvent):void { 
    e.target.stopDrag(); // stop dragging anyway 
    e.target.removeEventListener(MouseEvent.MOUSE_UP,stopDragging); 
    // and stop listening for mouse-ups 
    if (e.target.hitTestObject(tar1)&& (t1==0)) { 
     e.target.x=tar1.x; 
     e.target.y=tar1.y; 
     t1=1; // target is full 
     return; // emergency exit. We don't need to do more 
    } 
    // should you add more targets, you can put hitTest code here 

    // if we reached this point, we don't hit any targets 
    // so we should put our objects back into their positions. 
    // The best way would be storing their base coordinates on 
    // objects themselves, so we don't have to run elsewhere for them 
    e.target.x=e.target.startingX; // make sure these are filled 
    e.target.y=e.target.startingY; 
    e.target.addEventListener(MouseEvent.MOUSE_DOWN,startDragging); 
    // and enable this object to be dragged again. 
} 
+0

謝謝。我正在考慮你的指示。 – Amir

+0

Vesper!你能幫我解決這個問題嗎?當你的解決方案處於目標狀態時,你的解決方案將從o1或o2中移除監聽器,但是我希望用戶能夠再次將它拖回到它的位置,即使該對象位於目標中。在我的真實項目中,我有9個物體和7個目標。有沒有辦法,當拖動一個對象其他對象監聽器禁用時間? – Amir

+0

如果您希望能夠將對象拖離目標,只需在代碼的開頭添加一個偵聽器,而不是最後。關於您的其他問題,您應該跟蹤是否已經有一個對象被拖動,並且如果有的話就不要繼續處理。 – Vesper