2013-11-26 42 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中)之前,立即放开它,它只是抓住即使您几乎没有移动可拖动的物品,也可以使用最近的垫子。 –