-1

I'm making a drag-and-drop game. On the MouseEvent.MOUSE_UP I have an if-statement like so:

function fnUp(event:MouseEvent):void
    {
        clicked.stopDrag();
        if (clicked.dropTarget.parent.parent.name == clicked.currentLabel)
        {
            var dropped:Card = clicked.dropTarget.parent.parent as Card;
            dropped.gotoAndStop(dropped.name);
            dropped.bg.gotoAndStop("done");
            removeChild(clicked);

        }
        else
        {
            clicked.x = clicked.sx;
            clicked.y = clicked.sy;
            clicked.bg.gotoAndStop("off");
        }
        clicked = null;
    }

"clicked" is an earlier declared var of type Card(); and it's set in the MouseEvent.MOUSE_DOWN like so:

    function fnDown(event:MouseEvent):void
    {
        clicked = event.currentTarget as Card;
        clicked.bg.gotoAndStop("on");
        clicked.startDrag();
        addChild(clicked);
    }

I thought that if I build my if-statement like this there wouldn't be an error if the card was dropped anywhere but on one of the three possible target cards. But I still get:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

tldr; I get an error if I drop my card outside one of the 3 possible targets.

4

1 回答 1

1

On which line you getting an error? You need to check if all of the following objects existing:

if (clicked.dropTarget &&
clicked.dropTarget.parent &&
clicked.dropTarget.parent.parent )
{
    then do something...
}
于 2012-05-29T15:50:01.543 回答