如何阻止物品返回原位



这是一个拖放匹配游戏,我的问题是当我匹配错误时,如何阻止项目返回到原来的位置?我希望这些物品留下来。请为我详细解释,不胜感激。

right_mc.visible=false; 
wrong_mc.visible=false;
var orig1X:Number=item1_mc.x;  
var orig1Y:Number=item1_mc.y;
var orig2X:Number=item2_mc.x;
var orig2Y:Number=item2_mc.y;
var orig3X:Number=item3_mc.x;
var orig3Y:Number=item3_mc.y; 
item1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
item1_mc.addEventListener(MouseEvent.MOUSE_UP, item1Release);
item2_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);   
item2_mc.addEventListener(MouseEvent.MOUSE_UP, item2Release);   
item3_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);    
item3_mc.addEventListener(MouseEvent.MOUSE_UP, item3Release);    
done_btn.addEventListener(MouseEvent.CLICK, checkAnswers);    
reset_btn.addEventListener(MouseEvent.CLICK, reset);
item1_mc.buttonMode=true;    
item2_mc.buttonMode=true;    
item3_mc.buttonMode=true;

function dragTheObject(event:MouseEvent):void 
{ 
    var item:MovieClip=MovieClip(event.target); 
    item.startDrag(); 
    var topPos:uint=this.numChildren-5; 
    this.setChildIndex(item, topPos);    
}  
function item1Release(event:MouseEvent):void 
{ 
    var item:MovieClip=MovieClip(event.target); 
    item.stopDrag();       
    if (dropZone1_mc.hitTestPoint(item.x,item.y))
    { 
        item.x=dropZone1_mc.x; 
        item.y=dropZone1_mc.y; 
    } 
    else 
    { 
        item.x=orig1X; 
        item.y=orig1Y; 
    }
}    
function item2Release(event:MouseEvent):void 
{ 
    var item:MovieClip=MovieClip(event.target); 
    item.stopDrag();   
    if (dropZone2_mc.hitTestPoint(item.x,item.y)) 
    { 
        item.x=dropZone2_mc.x; 
        item.y=dropZone2_mc.y; 
    } 
    else 
    { 
        item.x=orig2X; 
        item.y=orig2Y; 
    }
}    
function item3Release(event:MouseEvent):void 
{ 
    var item:MovieClip=MovieClip(event.target);
    item.stopDrag();
    if (dropZone3_mc.hitTestPoint(item.x,item.y))
    {
        item.x=dropZone3_mc.x;
        item.y=dropZone3_mc.y;
    } 
    else 
    {
        item.x=orig3X;
        item.y=orig3Y;
    }
} 
function checkAnswers(event:MouseEvent):void 
{
    if (dropZone1_mc.hitTestPoint(item1_mc.x,item1_mc.y) &&
    dropZone2_mc.hitTestPoint(item2_mc.x,item2_mc.y) &&
    dropZone3_mc.hitTestPoint(item3_mc.x,item3_mc.y)) 
    {
        wrong_mc.visible = false;
        right_mc.visible = true;
    } 
    else 
    {
        wrong_mc.visible = true;
        right_mc.visible = false;
    }
}
function reset(event:MouseEvent):void {
    item1_mc.x=orig1X;
    item1_mc.y=orig1Y;
    item2_mc.x=orig2X;
    item2_mc.y=orig2Y;
    item3_mc.x=orig3X;
    item3_mc.y=orig3Y;
    right_mc.visible=false;
    wrong_mc.visible=false;
}
function itemRelease(event:MouseEvent):void {
    var thisItem:MovieClip = MovieClip(event.target);
    thisItem.stopDrag();
    if (dropZone1_mc.hitTestPoint(thisItem.x,thisItem.y)) 
    {
        thisItem.x = dropZone1_mc.x;
        thisItem.y = dropZone1_mc.y;
    } 
    else if (dropZone2_mc.hitTestPoint(thisItem.x,thisItem.y)) 
    {
        thisItem.x = dropZone2_mc.x;
        thisItem.y = dropZone2_mc.y;
    } 
    else if (dropZone3_mc.hitTestPoint(thisItem.x,thisItem.y)) 
    {
        thisItem.x = dropZone3_mc.x;
        thisItem.y = dropZone3_mc.y;
    }
    else if (thisItem==item1_mc) 
    {
        event.target.x = orig1X;
        event.target.y = orig1Y;
    } 
    else if (thisItem==item2_mc) 
    {
        event.target.x = orig2X;
        event.target.y = orig2Y;
    } 
    else {
        event.target.x = orig3X;
        event.target.y = orig3Y;
    }
}

您应该从每个发布函数中删除else条件。更新代码如下:

    right_mc.visible=false; 
    wrong_mc.visible=false;
    var orig1X:Number=item1_mc.x;  
    var orig1Y:Number=item1_mc.y;
    var orig2X:Number=item2_mc.x;
    var orig2Y:Number=item2_mc.y;
    var orig3X:Number=item3_mc.x;
    var orig3Y:Number=item3_mc.y; 
    item1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
    item1_mc.addEventListener(MouseEvent.MOUSE_UP, item1Release);
    item2_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);   
    item2_mc.addEventListener(MouseEvent.MOUSE_UP, item2Release);   
    item3_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);    
    item3_mc.addEventListener(MouseEvent.MOUSE_UP, item3Release);    
    done_btn.addEventListener(MouseEvent.CLICK, checkAnswers);    
    reset_btn.addEventListener(MouseEvent.CLICK, reset);
    item1_mc.buttonMode=true;    
    item2_mc.buttonMode=true;    
    item3_mc.buttonMode=true;

    function dragTheObject(event:MouseEvent):void 
    { 
        var item:MovieClip=MovieClip(event.target); 
        item.startDrag(); 
        var topPos:uint=this.numChildren-5; 
        this.setChildIndex(item, topPos);    
    }  
    function item1Release(event:MouseEvent):void 
    { 
        var item:MovieClip=MovieClip(event.target); 
        item.stopDrag();       
        if (dropZone1_mc.hitTestPoint(item.x,item.y))
        { 
            item.x=dropZone1_mc.x; 
            item.y=dropZone1_mc.y; 
        }
    }    
    function item2Release(event:MouseEvent):void 
    { 
        var item:MovieClip=MovieClip(event.target); 
        item.stopDrag();   
        if (dropZone2_mc.hitTestPoint(item.x,item.y)) 
        { 
            item.x=dropZone2_mc.x; 
            item.y=dropZone2_mc.y; 
        }
    }    
    function item3Release(event:MouseEvent):void 
    { 
        var item:MovieClip=MovieClip(event.target);
        item.stopDrag();
        if (dropZone3_mc.hitTestPoint(item.x,item.y))
        {
            item.x=dropZone3_mc.x;
            item.y=dropZone3_mc.y;
        }
    } 
    function checkAnswers(event:MouseEvent):void 
    {
        if (dropZone1_mc.hitTestPoint(item1_mc.x,item1_mc.y) &&
        dropZone2_mc.hitTestPoint(item2_mc.x,item2_mc.y) &&
        dropZone3_mc.hitTestPoint(item3_mc.x,item3_mc.y)) 
        {
            wrong_mc.visible = false;
            right_mc.visible = true;
        } 
        else 
        {
            wrong_mc.visible = true;
            right_mc.visible = false;
        }
    }
    function reset(event:MouseEvent):void {
        item1_mc.x=orig1X;
        item1_mc.y=orig1Y;
        item2_mc.x=orig2X;
        item2_mc.y=orig2Y;
        item3_mc.x=orig3X;
        item3_mc.y=orig3Y;
        right_mc.visible=false;
        wrong_mc.visible=false;
    }

如果你遇到任何问题,请告诉我。

相关内容

  • 没有找到相关文章

最新更新