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