只有在物体足够接近时才能捕捉对象

时间:2013-11-26 19:12:11

标签: arrays actionscript-3 drag snapping

所以这可能是一个简单的解决方案,但我似乎无法弄清楚自己。 我正在做的是创建一个拖拽游戏,它一切都很漂亮,但如果你过早放弃它,它会自动捕捉到最近的可用位置(根据我在阵列中的位置来选择)从)。我真的很想找它,如果说它在动画片段的范围内,或者如果它在动画片段的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);
}

2 个答案:

答案 0 :(得分:0)

就逻辑而言,这就是我要做的事情:

循环浏览位置(您的pointList

将包含位置和距离的对象推入数组

根据对象的距离属性按升序对结果数组进行排序

生成的排序数组的第一个元素是您最近的位置

答案 1 :(得分: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);
}

我希望这会有所帮助。