1 Reply Latest reply on Aug 10, 2009 11:04 AM by David_F57

    Drag drop -  if match, remove event listener

    Handycam Level 1

      I have an app with objects the user needs to drag onto other objects.  These objects are custom components based on Groups.  Using the code below, I have gotten basic drag-n-drop functionality to work fine.

       

      I need to only allow the drop if the "label" value  in the dragged item matched the one I will set in the drop target.  For example, if I have set the label in a dragged object to be "triangle" and my drop target is a picture of a triangle, fine, accept the drop, otherwise reject the drop.

       

      This seems to be working in my code so far; in the dragEnterHandler:

      if (event.dragSource.hasFormat('label')) 
                     {
                          if (value == "foo"){
                               dropTarget.currentState = "over";                         
                               // Accept the drop.
                               DragManager.acceptDragDrop(dropTarget);
                          } else {
                               dropTarget.currentState = "wrong";
                          }
                     }
      

      The problems I am having are:

      1. When the drag is rejected, setting the state back to "normal" on the dropTarget
      2. When the drag is ACCEPTED, switch off that drop target so that it accepts no further drags (it is "done")

       

      I tried to do item 2 by adding dropTarget.removeEventListener(DragEvent.DRAG_ENTER,dragEnterHandler) to the dragDropHandler, but it still accepts drags.

       

      Here is the code:

       

                private function dragIt(event:MouseEvent, value:String):void 
                {
                     // Get the drag initiator component from the event object.
                     var dragInitiator:NameTag = event.currentTarget as NameTag;
                     
                     // Create a DragSource object.
                     var dragSource:DragSource = new DragSource();
                     
                     // Add the data to the object.
                     dragSource.addData(value, 'label');
                     
                     // Call the DragManager doDrag() method to start the drag. 
                     DragManager.doDrag(dragInitiator, dragSource, event);
                }
      
                protected function dragEnterHandler(event:DragEvent):void
                {
                     // Get the drop target component from the event object.
                     var dropTarget:ItemArtwork=event.currentTarget as ItemArtwork;
                     
                     // Accept the drag only if the user is dragging data 
                     // identified by the 'value' format value.
                     
                     // Get the data identified by the color format from the drag source.
                     var value:String = event.dragSource.dataForFormat('label') as String;
                     
                     if (event.dragSource.hasFormat('label')) 
                     {
                          if (value == "foo"){
                               dropTarget.currentState = "over";                         
                               // Accept the drop.
                               DragManager.acceptDragDrop(dropTarget);
                          } else {
                               dropTarget.currentState = "wrong";
                          }
                     } 
                }
      
      
                protected function dragDropHandler(event:DragEvent):void
                {
                     var value:String = event.dragSource.dataForFormat('label') as String;
                     var dropTarget:ItemArtwork=event.currentTarget as ItemArtwork;
                     dropTarget.currentState = "normal";
                     dropTarget.tag.cardLabel.text = value;
                     dropTarget.removeEventListener(DragEvent.DRAG_ENTER,dragEnterHandler);                    
                }
      
      
                protected function dragExitHandler(event:DragEvent):void
                {
                     trace('left the box');
                     
                     var dropTarget:ItemArtwork=event.currentTarget as ItemArtwork;
                     dropTarget.currentState = "normal";                    
                }