检查碰撞时如何引用两个动态对象?

时间:2013-05-04 09:59:28

标签: actionscript-3 flash reference drag-and-drop parent-child

我无法弄清楚如何获取上面放置字母的哪个插槽的信息,因此字母只会捕捉到最后一个插槽,因为它是唯一正确引用的插槽。任何人都可以告诉我一个方法来检查上面的字母被丢弃的对象(插槽)?我创建的两种动画片段只是字母的背景和插入的插槽。

var myFormat:TextFormat = new TextFormat();
    myFormat.color = 0xFFFFFF;
    myFormat.font = "Verdana";
    myFormat.size = 35;  
    myFormat.bold = true;
    myFormat.align = TextFormatAlign.CENTER;

var myString:String = "firefly"
myString = myString.toUpperCase();
var myArray:Array = myString.split('');

for(var i:Number = 0; i < myString.length; i++) {

    var myBogstav:bogstav = new bogstav();
    var myBogstavSlot:bogstavSlot = new bogstavSlot();

    var myText:TextField = new TextField();
    myText.selectable = false;
    myText.width = 50;
    myText.height = 50;

    addChild(myBogstavSlot);
    addChild(myBogstav);
    myBogstav.addChild(myText);

    myText.defaultTextFormat = myFormat;
    myText.text = myArray[i];

    myBogstavSlot.name = "bogstavSlot" + i;
    myText.name = "bogstav" + i;
    myBogstav.name = "bogstavBG" + i;

    myBogstavSlot.x = i*60 + 10;
    myBogstavSlot.y = myBogstav.y - 100;
    myBogstav.x = i*60 + 10;

    myBogstav.addEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);
    myBogstav.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
}

var startPosition:Point;

function ClickToDrag(event:MouseEvent):void
{
    setChildIndex(event.target.parent, numChildren - 1);
    event.target.parent.alpha = 0.6;

    startPosition = new Point(event.target.parent.x, event.target.parent.y);

    event.target.parent.startDrag();
}

function releaseToDrop(event:MouseEvent):void
{   
    setChildIndex(event.target.parent, 0);
    event.target.parent.alpha = 1;
    event.target.parent.stopDrag();

    if (event.target.parent.hitTestObject(MovieClip)){
        event.target.parent.x = MovieClip.x;
        event.target.parent.y = MovieClip.y;

    } else {
        event.target.parent.x = startPosition.x;
        event.target.parent.y = startPosition.y;
    }
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

只需使用droptarget属性(doc)即可。此属性包含已拖放项目的DisplayObject的引用。

function releaseToDrop(event:MouseEvent):void
{   
    setChildIndex(event.target.parent, 0);
    event.target.parent.alpha = 1;
    event.target.parent.stopDrag();
    // here it just test if the dragged item is dropped on a bogstavSlot instance
    // adapt the condition to your needs
    if (event.target.parent.droptarget is bogstavSlot ){
        event.target.parent.x = event.target.parent.droptarget.x;
        event.target.parent.y = event.target.parent.droptarget.y;

    } else {
        event.target.parent.x = startPosition.x;
        event.target.parent.y = startPosition.y;
    }
}