2013-11-26 46 views
0

所以這可能是一個簡單的解決方案,但我似乎無法自己弄清楚。 我正在做的是創建一個拖放遊戲,並且它非常漂亮,但如果你太早放棄這一塊,它會自動捕捉到最近的可用位置(根據我在陣列中的位置從中選擇)。如果在動畫片結束的範圍內說出它,或者它位於動畫片段的20像素半徑範圍內,我真的很想找到它。 希望你們能幫助我!它始於mouseUp事件。只有當物體足夠接近時纔會捕捉物體

代碼:

var dragArray:Array = [it_1, it_2, it_3, it_4, it_5, it_6, it_7, it_8, it_9]; 
var matchArray:Array = [mat_1, mat_2, mat_3, mat_4, mat_5, mat_6, mat_7, mat_8, mat_9] 
var placeArray:Array = [0, 1, 2, 3, 4, 5, 6, 7, 8]; 
// points to snap to 
var pointList:Array = new Array(); 
pointList.push(new Point(mat_1.x, mat_1.y)); 
pointList.push(new Point(mat_2.x, mat_2.y)); 
pointList.push(new Point(mat_3.x, mat_3.y)); 
pointList.push(new Point(mat_4.x, mat_4.y)); 
pointList.push(new Point(mat_5.x, mat_5.y)); 
pointList.push(new Point(mat_6.x, mat_6.y)); 
pointList.push(new Point(mat_7.x, mat_7.y)); 
pointList.push(new Point(mat_8.x, mat_8.y)); 
pointList.push(new Point(mat_9.x, mat_9.y)); 
////where points are placed after being snapped to 
var spliced:Array= new Array(); 

function SnapEvent(event:MouseEvent) { 

//// the clip we're dragging//// 
var referencePoint:Point = new Point(currentClip.x, currentClip.y); 
var resultPoints:Array = PointTester.findClosest(referencePoint, pointList, 1); 

////returns nearest "mat" and snaps current clip to it//// 
for each(var result:Object in resultPoints) { 
//trace("Point is at:", result.point.x, ", ", result.point.y, " that's ", result.distance, " units away"); 
     currentClip.x=result.point.x; 
     currentClip.y=result.point.y; 
     var posOfMat:int = pointList.indexOf(result.point); 
     trace("index: "+pointList[posOfMat]); 
     spliced.push(pointList[posOfMat]); 
     pointList.splice(posOfMat,1); 
     //trace("spliced: "+spliced); 
     trace("length: "+pointList.length); 
     } 
     //trace("result: "+result.point); 
     trace("full spliced: "+spliced.length); 
} 
+0

您的具體問題是什麼?你不知道如何計算距離?你不知道如何比較距離結果以得到最接近的結果? – prototypical

+0

它目前正在通過pointList數組比較來找出最接近的一個。這裏的問題是,我只想讓它開始比較,當我說一個10像素。 –

+0

所以只要有一個條件,不評估點,除非他們在這個距離內。 – prototypical

回答

0

裏面的for each循環中,您可以檢查結果點的距離是否小於20個像素,並且只捕捉當前剪輯的結果如果是的話。這裏有一個例子:

... 

function SnapEvent(event:MouseEvent) { 

    //// the clip we're dragging//// 
    var referencePoint:Point = new Point(currentClip.x, currentClip.y); 
    var resultPoints:Array = PointTester.findClosest(referencePoint, pointList, 1); 

    ////returns nearest "mat" and snaps current clip to it//// 
    for each(var result:Object in resultPoints) { 
     if(result.distance < 20){ //if the result point's distance is less than 20 pixels away from the current clip 
      currentClip.x=result.point.x; // snap the current clips position 
      currentClip.y=result.point.y; 
      var posOfMat:int = pointList.indexOf(result.point); 
      trace("index: "+pointList[posOfMat]); 
      spliced.push(pointList[posOfMat]); 
      pointList.splice(posOfMat,1); 
      //trace("spliced: "+spliced); 
      trace("length: "+pointList.length); 
     } 
     //otherwise do nothing 
    } 
    //trace("result: "+result.point); 
    trace("full spliced: "+spliced.length); 
} 

我希望這有助於。

+0

完全有幫助,你確切知道我要做什麼。謝謝。有時候,這是其中的一件事,別人會告訴你,你只是有一個「啊!」!時刻。 –

0

在邏輯上,這是我會做:通過位置

環路(您pointList

推含有從位置和距離的目標你分成數組

根據對象的距離屬性以升序對結果數組進行排序

所產生的有序數組的第一個元素是你最親密的位置

+0

我有9個對象與其他9個對象對齊。在這裏,它拾取當前抓取對象的位置,並從那裏獲取數組「resultPoints」中的最近點 - 它是當前位置和最接近當前位置的pointList.Resulting的一個元素的組合。如果你開始拉對象,那麼在你靠近「墊子」(每個「墊子」可以放置一個可拖動對象,並且其x和y值列在pointList中)之前,立即放開它,它只是抓住即使您幾乎沒有移動可拖動的物品,也可以使用最近的墊子。 –