0

我有一个有效的拖放游戏,但它并不完美。当您与目标完全对齐时,我所有的电影剪辑都拖放到它们的目标上。

但是,如果您在将 mc 拖到目标区域之外时松开鼠标,有时但并非总是会抛出 1010 term undefined 错误,并且不会将 mc 捕捉回其原始起始 x/y 坐标(它只是将 mc 留在鼠标抬起时的位置)。我运行了调试器,它在我的 drop 函数中处理了这一行:

if (event.currentTarget.dropTarget != null && MovieClip(event.currentTarget.dropTarget.parent).allowed.indexOf(event.currentTarget) >= 0){

仅供参考,允许的是一组目标数组,因为我想要目标的“区域”而不是某些电影剪辑的特定目标。

有任何想法吗?


更新代码如下:

if (event.currentTarget.dropTarget != null) {
var mc:MovieClip=event.currentTarget.dropTarget as MovieClip;
if (mc==null) { // typecast fails. Say there's a Sprite below
    reply_txt.textColor = 0xEE1212
    reply_txt.text = "Oops!  Try Again!";
    event.currentTarget.alpha = 1;
    event.currentTarget.x = startX;
    event.currentTarget.y = startY; 
    return; // nothing to do here
}
mc=mc.parent;
if (mc && mc.allowed) { 
    // this MC has "allowed" property not "undefined" - we're in the grid
    // so now we can check indexOf() safely
    if (mc.allowed.indexOf(event.currentTarget)>=0){
        reply_txt.textColor = 0x33BC10
        reply_txt.text = "Good Job!";
        event.currentTarget.alpha = 1;
        event.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
        event.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
        event.currentTarget.buttonMode = false;
        event.currentTarget.x = MovieClip(event.currentTarget.dropTarget.parent).x;
        event.currentTarget.y = MovieClip(event.currentTarget.dropTarget.parent).y;
        stored.push(event.currentTarget);
        startXarray.push(startX);
        startYarray.push(startY);
        counter++;
    }

}

}

4

1 回答 1

1

是的,当您停止拖动对象时,它会检查光标下方的 DisplayObject 类型,并将该对象作为dropTarget事件中的属性返回。因此,如果您的对象被拖放到另一个没有allowed属性的 MC 上,则会引发 1010 错误。您需要在这样的嵌套 if 语句中检查这种情况:

if (event.currentTarget.dropTarget != null) {
    var mc:MovieClip=event.currentTarget.dropTarget as MovieClip;
    if (mc==null) { // typecast fails. Say there's a Sprite below
        returnThisBack();
        return; // nothing to do here
    }
    mc=mc.parent;
    if (mc && mc.allowed) { 
        // this MC has "allowed" property not "undefined" - we're in the grid
        // so now we can check indexOf() safely
        if (mc.allowed.indexOf(event.currentTarget)>=0)  snapThisToGrid();
    } else returnThisBack();
} else returnThisBack();
于 2013-03-19T03:56:41.050 回答