Drag and drop text : MouseEvent « TextField « Flash / Flex / ActionScript






Drag and drop text

 
package {
    
  import flash.display.Sprite;
  import flash.events.MouseEvent;
  import flash.text.TextField;
  import flash.text.TextFieldAutoSize;
  import flash.display.StageAlign;
  import flash.display.StageScaleMode;
  
  public class Main extends Sprite {
    
    public function Main(  ) {
        
      stage.align = StageAlign.TOP_LEFT;
      stage.scaleMode = StageScaleMode.NO_SCALE;
        
      var example:String = "A B C";
      var words:Array = example.split(" ");
      var word:Sprite;
      var wordText:TextField;

      for ( var i:int = 0; i < words.length; i++ ) {
        word = new Sprite(  );
        addChild( word );
        
        wordText = new TextField(  );
        word.addChild( wordText );
        
        wordText.autoSize   = TextFieldAutoSize.LEFT; // Left-justify the text
        wordText.border     = true;
        wordText.background = true;
        wordText.selectable = false;
            
        wordText.text = words[i];
        
        word.addEventListener( MouseEvent.MOUSE_DOWN, handleDrag );
        word.addEventListener( MouseEvent.MOUSE_UP, handleDrop );
        
        word.x = 10*i;
        word.y = 10*i;
      }
    }
    
    private function handleDrag( event:MouseEvent ):void {
      var word:Sprite = event.target.parent;
      
      setChildIndex( word, numChildren - 1 );
      
      word.startDrag(  );
    }
    
    private function handleDrop( event:MouseEvent ):void {
      var word:Sprite = event.target.parent;
      
      word.stopDrag(  );
    }        
  }    
}

        








Related examples in the same category

1.Add mouse click listener to TextField
2.Disappearing TextField
3.Working with Advanced Text Layout
4.Highlight a paragraph when the user clicks a character
5.To remove the highlight when the mouse moves away from both text fields, we would first register both text fields to receive the MouseEvent.MOUSE_OUT event
6.Clickable TextField
7.Using multiLine and TextMetrics: set the characters in the line underneath the user's mouse to red.
8.Unicode characters