0
votes

I have a working drag and drop game, but it's not perfect. All my movieclips drag and drop to their targets when you are exactly lined up with the target.

However, if you let go of the mouse up when dragging the mc outside of the target zones, it will sometimes, but not always, throw the 1010 term undefined error and will not snap the mc back to its original start x/y coordinates (it just leaves the mc in the spot it was during mouse up). I ran the debugger and it deals with this line in my drop function:

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

FYI, allowed is a set of target arrays since I wanted "zones" for targets and not specific targets for some of the movieclips.

Any ideas?


Updated Code Below:

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++;
    }

}

}

1
My guess would be that something turned null as a result of the end of the drag event. Null check everything - event.currentTarget, event.currentTarget.dropTarget, and event.currentTarget.dropTarget.parent.Scott Mermelstein
Ok, I tried tracing and when the error happens, it deals with event.currentTarget.dropTarget.parent. It returns "root1"?habib_101

1 Answers

1
votes

Yes, when you stopDrag an object, it checks what kind of a DisplayObject is below the cursor, and that one is returned as dropTarget property in your event. So, if your object is dropped onto another MC that does not have allowed property, your 1010 error is thrown. You need to check this situation in a nested if statement like this:

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();