0

我正在尝试创建一个 Start Drag 和 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();
}
4

1 回答 1

2

除非您明确将对象的name属性设置shampooing为“洗发”,否则getChildByName("shampooing")将引发错误。由于您已经引用了shampooing,因此可以将代码更改为:

if (check_box.hitTestObject(shampooing))
{
    check_box.x = shampooing.x;
    check_box.y = shampooing.y;
}
于 2013-03-05T03:15:52.090 回答