Actionscript 3 Actionscript3克隆对象并将其拖动到其他对象上

Actionscript 3 Actionscript3克隆对象并将其拖动到其他对象上,actionscript-3,flash,drag-and-drop,clone,Actionscript 3,Flash,Drag And Drop,Clone,我正在开发一个简单的flash游戏,用户可以从工具箱中拖动对象,然后拖动到某个区域。拖动对象时,我创建该对象的克隆并将其放置在该区域上。这是我的代码,工作起来有点好 package { imports... public class DocumentClass extends MovieClip { //variables... var someArray:Array = new Array(); var totalObjs = 5; var obj

我正在开发一个简单的flash游戏,用户可以从工具箱中拖动对象,然后拖动到某个区域。拖动对象时,我创建该对象的克隆并将其放置在该区域上。这是我的代码,工作起来有点好

package  {

imports...  

public class DocumentClass extends MovieClip {

    //variables...

    var someArray:Array = new Array();
    var totalObjs = 5;
    var objOnStage:Array = new Array();
    var ogx;
    var ogy;


    public function DocumentClass() {
        // constructor code
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;
        _width = stage.stageWidth;
        _height = stage.stageHeight;
       setGameObjects();
    }//constructor close

    //scaling Functions
    public function scalingheight(clip:MovieClip, percent:Number){
        var ratio = clip.width / clip.height;
        clip.height = percent * _height; 
        clip.width = clip.height * ratio;
    }

    public function scalingwidth(clip:MovieClip, percent:Number){
        var ratio = clip.height / clip.width;
        clip.width = percent * _width; 
        clip.height = clip.width * ratio;
    }

    public function setGameObjects():void{
        mc_toolbox = new mctoolbox;
        addChild(mc_toolbox);
        mc_toolbox.x = mc_toolbox.y = 0;
        scalingwidth(mc_toolbox, 0.08);
        mc_toolbox.height = _height;

        mc_board = new mcboard;
        addChild(mc_board);
        mc_board.x = mc_toolbox.width;
        mc_board.y = 0;
        scalingwidth(mc_board, 0.75);
        mc_board.height = _height;

        mc_optbox = new mcoptbox;
        addChild(mc_optbox);
        scalingwidth(mc_optbox, 0.10);
        mc_optbox.height = _height;
        mc_optbox.x = _width - mc_optbox.width;
        mc_optbox.y = 0;                

        setobjects();
    }

    public function setobjects():void{
        for(var i = 1; i <= totalObjs; i++){
        var className:String = "obj" + i;
        var ClassReference:Class = getDefinitionByName(className) as Class;
        var myInstance = new ClassReference;
        addChild(myInstance);           
        scalingwidth(myInstance, 0.08);
        someArray.push(myInstance);
        myInstance.x = stage.stageWidth - 0.09 * _width;
        myInstance.y = myInstance.height * (i-1);           
        }

        for(var i = 1; i <= totalObjs; i++){
        someArray[i-1].addEventListener(MouseEvent.MOUSE_DOWN, startMove);                       
        someArray[i-1].addEventListener(MouseEvent.MOUSE_UP, stopMove);          
        }
    }

    function startMove(evt:MouseEvent):void {
        //clone your movieclip here             
        ogx = evt.currentTarget.x;
        ogy = evt.currentTarget.y;
        evt.currentTarget.startDrag();
    }

    function stopMove(evt:MouseEvent):void {
        var newobj = new evt.currentTarget.constructor;
        addChild(newobj);
        newobj.width = evt.currentTarget.width;
        newobj.height = evt.currentTarget.height;
        newobj.x = evt.currentTarget.x;
        newobj.y = evt.currentTarget.y;
        evt.currentTarget.x = ogx;
        evt.currentTarget.y = ogy;
        evt.currentTarget.stopDrag();
        objOnStage.push(newobj);
    }

}//class close
}//package close
包{
进口。。。
公共类DocumentClass扩展了MovieClip{
//变量。。。
var someArray:Array=newarray();
var totalObjs=5;
var objOnStage:Array=new Array();
var-ogx;
变量学;
公共函数DocumentClass(){
//构造代码
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP\u左;
_宽度=舞台宽度;
_高度=舞台高度;
setGameObjects();
}//构造函数关闭
//标度函数
公共函数缩放高度(剪辑:MovieClip,百分比:数字){
var比率=clip.width/clip.height;
clip.height=百分比*\u高度;
clip.width=clip.height*比率;
}
公共函数缩放宽度(剪辑:MovieClip,百分比:数字){
var比率=clip.height/clip.width;
clip.width=百分比*\u宽度;
clip.height=clip.width*比率;
}
公共函数setGameObjects():void{
mc_工具箱=新的mctoolbox;
addChild(mc_工具箱);
mc_toolbox.x=mc_toolbox.y=0;
缩放宽度(mc_工具箱,0.08);
mc_toolbox.height=_height;
MCU板=新MCU板;
addChild(mc_板);
mc_board.x=mc_.width;
mc_board.y=0;
缩放宽度(MCU板,0.75);
mc_板高度=_高度;
mc_optbox=新的mcoptbox;
addChild(mc_optbox);
缩放宽度(mc_optbox,0.10);
mc_optbox.height=_height;
mc_optbox.x=_width-mc_optbox.width;
mc_optbox.y=0;
setobjects();
}
公共函数setobjects():void{

对于(var i=1;iTry,在将对象放在舞台上后移除
MouseEvent.MOUSE\u UP
,如下所示


evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP,stopMove);

我解决了这个问题。第二个movieclip位于第一个movieclip之后,所以我在拖动时将它放在所有其他movieclip之前

    function startMove(evt:MouseEvent):void {
        //clone your movieclip here             
        ogx = evt.currentTarget.x;
        ogy = evt.currentTarget.y;
        var myobj = evt.currentTarget as DisplayObject;
        setChildIndex(myobj, numChildren - 1); // < The solution
        evt.currentTarget.startDrag();
    }
功能启动移动(evt:MouseEvent):无效{
//在这里克隆你的电影唇
ogx=evt.currentTarget.x;
ogy=evt.currentTarget.y;
var myobj=evt.currentTarget作为显示对象;
setChildIndex(myobj,numChildren-1);//<解决方案
evt.currentTarget.startDrag();
}

这种情况是持续发生的,还是仅当第二个对象比第一个对象更靠近数组顶部时才会发生?我解决了这个问题。只有当我将第二个movieclip拖到第一个对象上,然后离开鼠标时才会发生。需要使用numChildren()将第二个对象放在所有其他对象之前