拖放每个目标只允许一个对象

时间:2013-09-18 14:08:03

标签: actionscript-3

我确信这很简单,但我的拖放代码非常有用......但是,它允许我为每个目标删除多个对象。我想要的是每个目标只允许一个对象,我看起来似乎无法找到如何测试以查看对象是否已经被丢弃。这是我的代码:

package com {

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.Point;
import com.greensock.*;

public class DraggableObject extends MovieClip 
{
    protected var origPos:Point;
    public function DraggableObject() 
    {
        origPos = new Point(x, y);
        buttonMode = true; // changes cursor to hand
        addEventListener(MouseEvent.MOUSE_DOWN, down);

    }


    protected function down (event:MouseEvent): void
    {
        parent.addChild(this); // adds object to the top of the display list to keep the object on top
        startDrag(); // built in Flash method
        stage.addEventListener(MouseEvent.MOUSE_UP, stageUp);
        var upPosX = x + 5;
        var upPosY = y - 5;
        TweenMax.to(this, .2, {dropShadowFilter:{color:0x666666, alpha:1, blurX:12, blurY:12, distance:12}});
        TweenLite.to(this, .2, {x: upPosX, y: upPosY});
        mouseEnabled = false;
    }

    protected function stageUp (event:MouseEvent): void 
    {
        stage.removeEventListener(MouseEvent.MOUSE_UP, stageUp); // good coding
        stopDrag(); // built in Flash method
        TweenMax.to(this, .5, {dropShadowFilter:{color:0x666666, alpha:0, blurX:12, blurY:12, distance:12}});

         if (dropTarget.parent.name == "root1"){
                returnToOrigPos();
        } 
            else if(dropTarget)// dropTarget is a built in property this means if dropTarget != null
        {
            trace("dropt on "+dropTarget.parent.name);
            x = dropTarget.parent.x;
            y = dropTarget.parent.y;

            //buttonMode = false; //only use these to keep the user's selection...can't change if these are active.
            //removeEventListener(MouseEvent.MOUSE_DOWN, down);

        } 
        mouseEnabled = true;
    }

    protected function returnToOrigPos(): void 
    {
            x = origPos.x;
            y = origPos.y;
    }
}

我认为hitTestObjectdropTarget会起作用,但无法弄清楚如何使用它们。

感谢您的帮助! 麦克

1 个答案:

答案 0 :(得分:0)

将一个公共属性(例如:_hasDroppedObject)添加到您要删除对象的任何类型的对象中,并在将对象放入其中时将其设置为true:

dropTarget.hasDroppedObject = true;

然后将“if(dropTarget.parent.name ==”root1“)”行更改为:

if (dropTarget.parent.name == "root1" || dropTarget.hasDroppedObject) {

(这假设您可以访问drop接收类)。