0

これは簡単な解決策かもしれませんが、自分で理解できないようです。私がやっていることは、ドラッグアンドドロップゲームを作成することです.それはすべてうまくいき美しいですが、ピースを早く離すと、最も近い利用可能な場所に自動的にスナップします(選択する配列にある位置に基づいて)から)。ムービークリップの範囲内で終了した場合、またはムービークリップの半径20px以内にある場合にのみスナップすることを本当に探しています。皆さんが私を助けてくれることを願っています! これは、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);
}
4

2 に答える 2

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);
}

これが役立つことを願っています。

于 2013-11-27T00:27:32.930 に答える