3

我的目标示例

标题或多或少是不言自明的,我一直在学习许多不同的教程,老实说,我对 AS3 不太擅长。(上图显示了我的目标)

无论如何,我在看到的大多数在线教程中都注意到,拖放教程要么基于一个对象到一个目标,要么基于多个对象到多个目标,所以我想知道是否有人能帮助我并解释如何我可以让多个对象连接到一个目标。

并且,如果可能使其可切换,例如,当我将对象 2 拖到上方时,如果对象 1 已经在目标上,那么对象 1 将返回到其原始位置,而对象 2 将取而代之。

一个更简单的解释方法是说我正在尝试创建一个游戏,其中有三个雕像,用户可以选择三个雕像中的一个放置在设定的目标区域中。

如果我说的话没有多大意义,我很抱歉,如果这引起混乱,我会澄清任何事情。这是我目前正在使用的 AS3 代码。

var startX:int;
var startY:int;

circle1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circle2_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle2_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
circle3_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
circle3_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);

function pickUp(event:MouseEvent):void {
    startX = event.target.x;
    startY = event.target.y;
    event.target.startDrag(true);
    event.target.parent.addChild(event.target);

}

function dropIt(event:MouseEvent):void {
    event.target.stopDrag();

    var theTargetName:String = "target" + event.target.name;
    var theTarget:DisplayObject = getChildByName(theTargetName);
    if (event.target.dropTarget != null && event.target.dropTarget.parent == theTarget){    
        event.target.buttonMode = false;
        event.target.x = theTarget.x;
        event.target.y = theTarget.y;
    }

else{

    event.target.x = startX;
    event.target.y = startY;

circle1_mc.buttonMode = true;
circle2_mc.buttonMode = true;
circle3_mc.buttonMode = true;
4

1 回答 1

5

除了检查 之外dropTarget,您还可以使用hitTestObject来查看放置的对象是否“接触” theTarget。否则,任何其他已被丢弃的项目都theTarget可能被报告为dropTarget. 此外,由于MovieClip是动态的,您可以在每个实例中存储startX和值。startY

以下修改后的代码将使用单个target_mc作为放置目标。当一个物品掉在上面时,任何其他物品都将被移回原来的位置:

// create an array as @David suggested to keep track of your draggable items
var circles:Array = [circle1_mc, circle2_mc, circle3_mc];
for each(var circleMC:MovieClip in circles)
{
    circleMC.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    circleMC.addEventListener(MouseEvent.MOUSE_UP, dropIt);
    circleMC.startX = circleMC.x;
    circleMC.startY = circleMC.y;
}

function pickUp(event:MouseEvent):void
{
    // no longer need to keep track of startX & startY here because that's already been done up above
    event.target.startDrag(true);
    event.target.parent.addChild(event.target);

}

function dropIt(event:MouseEvent):void
{
    event.target.stopDrag();
    // check to see if the event target is touching target_mc using hitTestObject
    if(event.target.hitTestObject(target_mc)){
        event.target.buttonMode = false;
        event.target.x = target_mc.x;
        event.target.y = target_mc.y;
        // move all circles OTHER than the current target back to their original positions
        for each(var circleMC:MovieClip in circles)
        {
            if(event.target != circleMC)
            {
                circleMC.x = circleMC.startX;
                circleMC.y = circleMC.startY;
            }
        }
    }
    else
    {
        // only need to move the event target back if it was dropped outside of target_mc
        event.target.x = event.target.startX;
        event.target.y = event.target.startY;
        event.target.buttonMode = true;
    }
}
于 2013-03-12T13:43:24.757 回答