使用 ActionScript 3 为捕捉函数创建数组

creating an array for a snapping function with ActionScript 3

我创建了一个拼图,您可以在其中拖放 16 块拼图。我使用了一个数组,这样代码就不会变得太大。现在我想添加一个功能,当你靠近目的地时,每个拼图块都会卡到正确的位置。

我的问题是我不知道如何创建一个可以实现我的目标的数组。我尝试了以下方法(没有数组,但如果我对所有 16 个拼图块都这样做会产生太多代码):

if(target1_mc.hitTestObject(piece1_mc.tar1_mc))
        {
            piece1_mc.x = 207,15;
            piece1_mc.y = 119,25;
        }

代码:

import flash.events.Event;
import flash.events.MouseEvent;

    var puzzleArr:Array = new Array (piece1_mc, piece2_mc, piece3_mc, piece4_mc,
piece5_mc, piece6_mc, piece7_mc, piece8_mc, 
piece9_mc, piece10_mc, 
piece11_mc, piece12_mc, piece13_mc, piece14_mc, piece15_mc, piece16_mc);


for (var i:uint =0; i < puzzleArr.length; i++) {
 puzzleArr[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
 puzzleArr[i].addEventListener(MouseEvent.MOUSE_UP, drop);
}


function drag(event:MouseEvent):void {
 event.currentTarget.startDrag();
}


function drop(event:MouseEvent):void {
 event.currentTarget.stopDrag();
}

有几种方法可以做到这一点。最简单的方法是向您的片段添加一个动态 属性 来存储片段的目标(正确的位置对象)。

var puzzleArr:Array = []; //you don't really even need the array in my example
var tmpPiece:MovieClip; //this stores the current dragging piece, and I also reuse it in the loop below 

//I don't like typing a lot, so let's use a loop for all 16 pieces and their targets
for(var i:int=1;i<=16;i++){
    tmpPiece = this["piece" + i + "_mc"]; //get a reference to piece whose number matches i

    if(!tmpPiece){
        trace("Sorry - there is no piece called: 'piece" + i + "_mc'");
        continue;
    }

    //give the piece a dynamic property that is a reference to it's target spot
    tmpPiece.targetTile = this["target" + i + "_mc"];

    if(!tmpPiece.targetTile){
        trace("Sorry - there is no target called: 'target" + i + "_mc'");
        continue;
    }

    tmpPiece.tar_mc = tmpPiece["tar" + i + "_mc"]; //it would be better to just take the number out of each pieces tar_mc child object making this line uneccessary

    //track where the piece is placed
    tmpPiece.startingPos = new Point(tmpPiece.x, tmpPiece.y);

    //only add the mouse down listener to the piece (not mouse up)
    tmpPiece.addEventListener(MouseEvent.MOUSE_DOWN, drag);

    //if still using the array, add the piece to the array
    puzzleArr.push(tmpPiece);
}

接下来,仅在拖动时添加鼠标移动侦听器

function drag(event:MouseEvent):void {
    tmpPiece = event.currentTarget as MovieClip; //assign the dragging object to the tmpPiece var


    tmpPiece.startDrag();

    //add a mouse move listener so you can check if snapping is needed
    tmpPiece.addEventListener(MouseEvent.MOUSE_MOVE, moving);

    //add the mouse up listener to the stage - this is good because if you drag fast, the mouse can leave the object your dragging, and if you release the mouse then it won't trigger a mouse up on the dragging object
    stage.addEventListener(MouseEvent.MOUSE_UP, drop);
}

function drop(event:MouseEvent):void {
    //stop all dragging
    this.stopDrag();

    if(tmpPiece){
        //remove the mouse move listener
        tmpPiece.removeEventListener(MouseEvent.MOUSE_MOVE, moving);

        //ensure a snap at the end of the drag
        if(!checkSnapping()){
           //if not snapped, reset it's position
           tmpPiece.x = tmpPiece.startingPos.x;
           tmpPiece.y = tmpPiece.startingPos.y;
        } 
    }

    //remove the mouse up listener
    stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
}

现在让我们在鼠标移动处理程序中进行捕捉:

function moving(e:MouseEvent = null):void {
    checkSnapping();
}

//return true if snapped
function checkSnapping():Boolean {
    if(tmpPiece && tmpPiece.tar_mc.hitTestObject(tmpPiece.targetTile)){
        tmpPiece.x = tmpPiece.targetObj.x - tmpPiece.tar_mc.x;
        tmpPiece.y = tmpPiece.targetObj.y - tmpPiece.tar_mc.y;
        return true;
    }

    return false;
}
for (var i:int = 0; i < puzzleArray.length; i++)
{
    if(puzzleArray[i].hitTestObject(puzzleArray[i]._target))
    {
        puzzleArray[i].x = puzzleArray[i]._xGoal;
        puzzleArray[i].y = puzzleArray[i]._yGoal;
    }
}

显然,您需要向拼图对象添加几个属性 (_xGoal_yGoal_target),您可以根据需要进行操作.您可能会使用循环,但前提是它们有某种顺序。如果它们大小不等并且在网格中,那么您显然必须手动输入它们。

如果它们在一个网格中并且每个部分大小相等,如果您需要帮助在循环中创建这些属性,请告诉我。