0
votes

I am building a simple flash game where user can drag objects from a tool-box and drag over to an area. On dragging the object, I create a clone of that object and place it over the area. Here is my code that works somewhat fine..

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

My problem arises when I drag one object on the area and then I drag one more on the previous object. In such case the mouse up event is not called and hence the object movieclip does not clone itself. I think I am making some silly mistake, please guide.

2
Does this happen consistently, or does it only happen when your second object is closer to the top of the array than the first? - Brian
I got this solved. It happened only when I dragged the second movieclip over the first and then left the mouse. Needed to bring the second obj in front of all other using numChildren() - Andy Boy

2 Answers

1
votes

I got this solved. The second movieclip came behind the first so I just brought it in front of all other movieclips while dragging using this

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

Try, removing MouseEvent.MOUSE_UP after dropping the object on the stage like so,

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