我有一个有效的拖放游戏,但它并不完美。当你准确地与目标对齐时,我的所有动画片段都会拖放到目标上。
但是,如果在目标区域外拖动mc时放开鼠标,有时(但不总是)会抛出1010术语未定义错误,并且不会将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++;
}
}
}
答案 0 :(得分: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();