如何将影片剪辑与另一个影片剪辑对齐,以便在as3中拖放

时间:2015-05-12 23:59:25

标签: actionscript-3 flash drag-and-drop movieclip

我是As3的一名成员。我需要使用As3在拖放游戏中完成我的第一个项目。问题是我没有得到匹配编码来将可拖动的影片剪辑与另一个影片剪辑对齐。我想制作一个完整的火车,按照完整列车示例的顺序将火车车身拖到头部。我有三个火车车身的电影剪辑和火车头的电影剪辑。我想要做的是按照使用拖放游戏风格给出的示例完整列车的布置将列车车身拖到其头部。如果正确的身体训练被拖到它的头部,那么它将对齐并且出现另一个训练体并且将以相同的方式重复直到它完成。

任何人都知道如何做到这一点?拜托,任何帮助都会很好。
感谢

所以这是我的代码:

    stop();

    import flash.display.MovieClip;

    var orig1X:Number=body_mc.x;  
    var orig1Y:Number=body_mc.y;
    var orig2X:Number=body1_mc.x;
    var orig2Y:Number=body1_mc.y;
    var orig3X:Number=body2_mc.x;
    var orig3Y:Number=body2_mc.y; 


    body_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
    body_mc.addEventListener(MouseEvent.MOUSE_UP, bodyRelease);
    body1_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);   
    body1_mc.addEventListener(MouseEvent.MOUSE_UP, body1Release);   
    body2_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);    
    body2_mc.addEventListener(MouseEvent.MOUSE_UP, body2Release); 

    function dragTheObject(event:MouseEvent):void { 

        var item:MovieClip=MovieClip(event.target); 
        addChild(item);
        item.startDrag(); 
        var topPos:uint=this.numChildren-1; 
        this.setChildIndex(item, topPos);

    }  

    function bodyRelease(event:MouseEvent):void { 
        var item:MovieClip=MovieClip(event.target); 
        item.stopDrag();    
    if (head_mc.hitTestPoint(item.x,item.y)) { 
            item.x=head_mc.x; 
            item.y=head_mc.y; 
        }
        else { 
          item.x=orig1X; 
           item.y=orig1Y; 
        } 
    };    

    function body1Release(event:MouseEvent):void { 
        var item:MovieClip=MovieClip(event.target); 
        item.stopDrag();    
    if (head_mc.hitTestPoint(item.x,item.y)) { 
            item.x=head_mc.x; 
            item.y=head_mc.y; 
        }
        else { 
           item.x=orig2X; 
           item.y=orig2Y; 
        } 
    };  

    function body2Release(event:MouseEvent):void { 
        var item:MovieClip=MovieClip(event.target); 
        item.stopDrag();    
    if (head_mc.hitTestPoint(item.x,item.y)) { 
            item.x=head_mc.x; 
            item.y=head_mc.y; 
        }
        else { 
           item.x=orig3X; 
           item.y=orig3Y; 
        } 
    };  

1 个答案:

答案 0 :(得分:0)

我对你想要的东西感到有点困惑,但如果我理解你的话,那么你的火车头部/头部和火车的3个部分,你必须将这些部件拖到火车头部如果他们放开时触摸头部,它们将与你添加的最后一块对齐???如果那是你想要做的,那么试试这个作为你的基类。

包 {

/**
 * ...
 * @author Steven Davies
 */
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;

public class TrainDragAndDrop extends MovieClip
{
    private var origins:Vector.<Point> = new Vector.<Point>;

    private var trains:Array = new Array();
    private var numberOfTrains:int = 3;
    private var trainOrder:Array = new Array();

    private var movingTrainIndex:int = 0;
    private var mouseOffSet:Point = new Point();

    private var draggingTrain:Boolean = false;

    public var head_mc:MovieClip;

    public function TrainDragAndDrop():void
    {
        addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
    }

    public function addedToStageHandler(e:Event):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

        //should really add the trains dynamically but this will work.
        for (var i:int = 0; i < numberOfTrains; i++)
        {
            if (this["body" + i + "_mc"] != null)
            {
                trains[i] = this["body" + i + "_mc"];
                origins[i] = new Point(trains[i].x, trains[i].y);
            }
            else
            {
                break;
            }
        }

        addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    }

    private function mouseDownHandler(e:MouseEvent):void 
    {
        for (var i:int = 0; i < trains.length; i++)
        {
            if (trains[i].hitTestPoint(mouseX, mouseY))
            {
                movingTrainIndex = i;
                mouseOffSet.x = trains[i].x - mouseX;
                mouseOffSet.y = trains[i].y - mouseY;
                draggingTrain = true;

                break;
            }
        }

        if (draggingTrain == true)
        {
            removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
            addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }
    }

    private function mouseUpHandler(e:MouseEvent):void
    {
        draggingTrain = false;
        removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
        removeEventListener(Event.ENTER_FRAME, enterFrameHandler);

        if (head_mc.hitTestPoint(mouseX, mouseY, true))
        {
            if (trainOrder.length == 0)
            {
                trains[movingTrainIndex].x = head_mc.x;
                trains[movingTrainIndex].y = head_mc.y;
            }
            else
            {
                trains[movingTrainIndex].x = trains[trainOrder[trainOrder.length - 1]].x;
                trains[movingTrainIndex].y = trains[trainOrder[trainOrder.length - 1]].y + trains[trainOrder[trainOrder.length - 1]].height;
            }
            trainOrder.push(movingTrainIndex);
        }
        else
        {
            trains[movingTrainIndex].x = origins[movingTrainIndex].x;
            trains[movingTrainIndex].y = origins[movingTrainIndex].y;
        }
        addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    }

    private function enterFrameHandler(e:Event):void 
    {
        if (draggingTrain)
        {
            trains[movingTrainIndex].x = mouseX + mouseOffSet.x;
            trains[movingTrainIndex].y = mouseY + mouseOffSet.y;
        }
    }
}

}

我似乎无法为此添加fla,因此无法向您展示示例,但为此工作确保您将“body_mc”实例命名为“body0_mc”,而列车的锚点是在影片剪辑的顶部,如果您希望我向您发送短信,请将您的电子邮件发送给我,我会将其发送给您。

如果我误解了你需要发生什么,请告诉我,我们可以谈谈你需要什么。