Skip navigation
Currently Being Moderated

More questions about drag and drop and depth

Jan 23, 2013 7:09 PM

Ok team,

so I know my drag and drop code is probably "sticking" on the mouse up event because there is something in front of the object..

I am trying to implement the numChildren code to bring the object being dragged to the front..

SO I am adding this code inside each draggable answer...

 

addEventListener(MouseEvent.MOUSE_DOWN, changedepth);

function changedepth(event:MouseEvent):void{

this.parent.setchildIndex(this, this.parent.numChildren-1)

}

ok so that part is ok, but my project is quite complex, so now when it goes onto the next section or resets itself there are children out there that are not behaving themselves..

So now I want to put something like this

 

addEventListener(MouseEvent.MOUSE_UP, removechild);

function removechild(event.MouseEvent){

this.parent.removeChild(-1);

}

 

but of course this doesnt work... any ideas?

Cheers for your help

Sub

 
Replies
  • Currently Being Moderated
    Jan 23, 2013 7:16 PM   in reply to subtlefly72

    You should assign the MOUSE_UP listener to the stage so that it is object independent.  If you need to know which object was being dragged then store a reference to it in a variable.

     

    Also, the removeChild() method argument needs to be a DisplayObject, not a number

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 23, 2013 9:15 PM   in reply to subtlefly72

    There are many ways to do what you want, but the basic ideas that Ned gave are what you need.

     

    You want the mouse down handlers on the things you want to drag.

    You wan the mouse up handler on the stage because you might release the mouse not over the thing you are dragging.

    If you want to put something back to the depth/stacking order it came from you will need to keep track of the layer it came from.

    You can't use code that doesn't do what you want just because you want it to do something different. Flash does what we tell it, not what we want!

     

    I can't find anything regarding childindex. If it was a built in property or method it would probably be childIndex. There is a childIndex property in the XML class, but I don't think that is what you want.

     

    Create a new flash file (with a stage 1000 x 800) add this code and give it go.

     

    import flash.display.Sprite;

    import flash.display.Graphics;

    import flash.text.TextField;

    import flash.events.Event;

    import flash.events.MouseEvent;

    import flash.display.MovieClip;

     

    var home:MovieClip=this;

    var nunItems:int=4;

    var i:int;

     

    for(i=0;i<nunItems;i++){

              var s:MovieClip=new MovieClip();

              var g:Graphics=s.graphics;

              var t:TextField=new TextField();

              t.name="field";

              t.autoSize=TextFieldAutoSize.LEFT;

              t.selectable=false;

              s.mouseChildren=false;

              s.addChild(t);

              var val:Number=((0x80+0x80*Math.random())<<16)+((0x80+0x80*Math.random()) <<8)+((0x80+0x80*Math.random()))

              g.beginFill(val);

              g.drawRect(-40,-40,80,80);

              addChild(s);

              s.x=Math.random()*800+100;

              s.y=Math.random()*600+100;

              s.addEventListener(Event.ENTER_FRAME,handleFrame);

              s.addEventListener(MouseEvent.MOUSE_DOWN,handleDown);

    }

     

    function handleDown(e:MouseEvent){

              var s:MovieClip=e.target as MovieClip;

              s.originalDepth=getChildIndex(s);

              addChild(s);

              s.startDrag();

              stage.addEventListener(MouseEvent.MOUSE_UP,handleUp);

    }

     

    function handleUp(e:MouseEvent){

              var s:MovieClip=e.target as MovieClip;

              stage.removeEventListener(MouseEvent.MOUSE_UP,handleUp);

              stopDrag();

              addChildAt(s,s.originalDepth);

    }

     

    function handleFrame(e:Event){

              var s:Sprite = e.target as Sprite;

              var t:TextField= s.getChildByName("field") as TextField;

              t.text=home.getChildIndex(s).toString();

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 23, 2013 10:37 PM   in reply to Rothrock

    This made my day:

     

    Arrgh this is killing me!

    ...

    s there anything that I can do ?

    (in a simple way)

    Response from you:

     

    var val:Number=((0x80+0x80*Math.random())<<16)+((0x80+0x80*Math.random()) <<8)+((0x80+0x80*Math.random()))

     

    I see what you did there

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 24, 2013 6:38 AM   in reply to subtlefly72

    That is a really important lesson about debugging. The problem is what it is. It isn't what we want it to be. This goes with my other piece of advice: Flash does what we tell it to do, not what we want it to do. Keep those both in mind

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points