DragTest
Spriteを継承する方法で、TextFieldにドラッグ機能を付けました。
/**
* Copyright lla ( http://wonderfl.net/user/lla )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/t5K4
*/
package {
import flash.display.Sprite;
import flash.events.Event;
public class DragTest extends Sprite{
public function DragTest(){
var s: Square = new Square();
s.x = s.y = 50;
addChild(s);
var s2: Square = new Square();
s2.x = s2.y = 100;
addChild(s2);
}
}
}
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.geom.Rectangle;
import flash.display.DisplayObject;
class Square extends flash.display.Sprite {
public var tf: flash.text.TextField;
public var bounds: Rectangle;
public function Square(){
super();
tf = new flash.text.TextField();
tf.text = "";
tf.border = true;
tf.background = true;
addChild(tf);
//tf.selectable = false;
this..mouseChildren = false;
this.buttonMode = true;
addEventListener(Event.ADDED_TO_STAGE, addEvents);
}
public function addEvents(e: Event): void {
bounds = new Rectangle(0, 0, stage.stageWidth - width, stage.stageHeight - height);
addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
stage.addEventListener(Event.MOUSE_LEAVE, stopDragging);
}
public function startDragging(e: Event): void {
//e.target.parent.addChildAt(e.target as DisplayObject);//square<-textField
tf.text = String(e.target) + "\n" + String(e.currentTarget);
//parent.addChild(e.currentTarget as DisplayObject); //stage<-Square
parent.addChild(this); //stage<-Square
startDrag(false, bounds);
}
public function stopDragging(e: Event): void{
stopDrag();
}
}