AS3 动态文本的拖动与旋转
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
/**
* ...
* @author FLASH023
*/
public class Main extends Sprite
{
private var now_box:TextBox;
public function Main():void {
var _p:int = 12;
while (_p--) {
var _box:TextBox = new TextBox ();
_box.x = 100+Math.random () * 550;
_box.y = 100 + Math.random () * 400;
_box.rotationZ = Math.random () * 360;
_box.addEventListener (MouseEvent.CLICK, onClick);
addChild (_box);
}
}
private function onClick(_evt:MouseEvent ):void {
var _box:TextBox = _evt.target as TextBox;
if (_box) {
now_box&&(now_box.focus = false);
_box.focus = true;
now_box = _box;
}
}
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.events.MouseEvent;
class TextBox extends Sprite {
private var r_btn:Sprite;
private var p_txt:TextField;
public function TextBox():void {
p_txt = addChild (new TextField) as TextField ;
p_txt.autoSize = "left";
p_txt.text = "测试文本";
p_txt.selectable = false;
p_txt.mouseEnabled = false;
p_txt.x = width * -.5;
p_txt.y = height * -.5;
p_txt.borderColor = 0x333399;
r_btn = addChild (new Sprite) as Sprite;
r_btn.graphics.beginFill (0x333399,.6);
r_btn.graphics.drawCircle (0, 0, 5);
r_btn.x = p_txt.width * .5;
r_btn.y = -p_txt.height * .5;
r_btn.visible = false;
r_btn.buttonMode = true;
addEventListener (MouseEvent.MOUSE_DOWN, mouseDown );
}
private function mouseDown(_evt:MouseEvent ):void {
if (_evt.target != r_btn) {
startDrag ();
}else{
addEventListener (Event.ENTER_FRAME, startRing );
}
stage.addEventListener (MouseEvent.MOUSE_UP, mouseUp);
}
private function startRing(_evt:Event):void {
rotationZ = Math.atan2 (parent.mouseY - y, parent.mouseX - x) / Math.PI * 180;
}
private function mouseUp(_evt:MouseEvent ):void {
stopDrag();
removeEventListener (Event.ENTER_FRAME, startRing );
stage.removeEventListener (MouseEvent.MOUSE_UP, mouseUp);
}
public function set focus(_b:Boolean):void {
r_btn.visible = _b;
p_txt.border = _b;
}
}
补充:flash教程,As3.0