开始和停止拖动无法正常工作

时间:2013-03-05 02:50:59

标签: actionscript-3

我正在尝试创建一个Start Drag and Stop Drag简单应用。但它似乎无法正常工作。它开始拖动但它不会在任何地方掉落

addChild(shampooing);
addChild(check_box);
check_box.gotoAndStop(2);
check_box.x = 40;
check_box.y = 550;

check_box.addEventListener(MouseEvent.MOUSE_DOWN, dragObject);
check_box.addEventListener(MouseEvent.MOUSE_UP, stopDragObject);
function dragObject(mn:MouseEvent):void
{
    xPos = check_box.x;
    yPos = check_box.y;

    check_box.startDrag(true);
}
function stopDragObject(l:MouseEvent):void
{
    if (check_box.hitTestObject(getChildByName("shampooing")))
    {
        check_box.x = getChildByName("shampooing").x;
        check_box.y = getChildByName("shampooing").y;
    }
    else
    {
        check_box.x = xPos;
        check_box.y = yPos;
    }

    check_box.stopDrag();
}

1 个答案:

答案 0 :(得分:2)

除非您明确将name对象的shampooing属性设置为"洗头,"然后getChildByName("shampooing")会抛出错误。由于您已经引用了shampooing,因此您可以将代码更改为:

if (check_box.hitTestObject(shampooing))
{
    check_box.x = shampooing.x;
    check_box.y = shampooing.y;
}