所以這可能是一個簡單的解決方案,但我似乎無法自己弄清楚。 我正在做的是創建一個拖放遊戲,並且它非常漂亮,但如果你太早放棄這一塊,它會自動捕捉到最近的可用位置(根據我在陣列中的位置從中選擇)。如果在動畫片結束的範圍內說出它,或者它位於動畫片段的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);
}
您的具體問題是什麼?你不知道如何計算距離?你不知道如何比較距離結果以得到最接近的結果? – prototypical
它目前正在通過pointList數組比較來找出最接近的一個。這裏的問題是,我只想讓它開始比較,當我說一個10像素。 –
所以只要有一個條件,不評估點,除非他們在這個距離內。 – prototypical