Actionscript 3克隆和拖动对象

时间:2014-10-02 12:48:51

标签: 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 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

当我在区域上拖动一个对象然后在前一个对象上再拖一个对象时,我的问题就出现了。在这种情况下,不会调用mouse up事件,因此对象movieclip不会克隆自身。我想我犯了一些愚蠢的错误,请指导。

2 个答案:

答案 0 :(得分:1)

我解决了这个问题。第二个动画片段落后于第一个,所以我只是在使用这个拖动时将它带到所有其他动画片段前面

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

答案 1 :(得分:0)

尝试在将对象放在舞台上后删除MouseEvent.MOUSE_UP

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