利用Bitmap让TextField对象旋转,利用Sprite容器让Bitmap对象可以被鼠标拖拽
由于不能直接通过改变TextField对像的rotation属性来让其旋转(貌似没有这个属性),可以使用BitmapData和Bitmap类来做到。
看一下Bitmap所继承的类就可以知道其不是继承自InteractiveObject,所以不支持使用鼠标和键盘与之交互,可以利用最轻量级的Sprite容器做到
代码里有个单独的sprite对象circle进行比较。
package { import flash.text.TextField; import flash.text.TextFormat; import flash.text.TextFormatAlign; import flash.display.Sprite; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.MovieClip; import flash.display.Graphics; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; public class test extends Sprite { public var singleWordText:TextField; private var bitmap:BitmapData; private var map:Bitmap; private var circle:Sprite; private var container:Sprite; private var clickOffset:Point=null; private var clickOffsetcircle:Point=null; public function test() { container=new Sprite(); addChild(container); var format:TextFormat=new TextFormat(); format.font="Verdana"; format.color=0xFFFFFF; format.size=20; format.align=TextFormatAlign.LEFT; //TextField要先申明样式后再输入字符 singleWordText=new TextField; singleWordText.defaultTextFormat=format; singleWordText.text="A"; bitmap=new BitmapData(20,25,true,0xcccccccc); bitmap.draw(singleWordText,null,null,null,null,true); map=new Bitmap(bitmap,"auto",true); map.rotation=30; //container.rotation=30; container.addChild(map); container.x=200; container.y=200; container.addEventListener(MouseEvent.MOUSE_DOWN, startTextDrag); stage.addEventListener(MouseEvent.MOUSE_UP, stopTextDrag); container.addEventListener(Event.ENTER_FRAME, dragText); circle=new Sprite(); circle.graphics.beginFill(0xFF0000); circle.graphics.lineStyle(2, 0xFF0000); circle.graphics.drawCircle(400,200,30); circle.graphics.endFill(); addChild(circle); circle.addEventListener(MouseEvent.MOUSE_DOWN, startcircleDrag); stage.addEventListener(MouseEvent.MOUSE_UP, stopcircleDrag); circle.addEventListener(Event.ENTER_FRAME, dragcircle); } //------------------\text drag/-------------------------------- //drag map------------- private function startTextDrag(event:MouseEvent) { clickOffset = new Point(event.localX, event.localY); } // user released private function stopTextDrag(event:MouseEvent) { clickOffset = null; } // run every frame private function dragText(event:Event) { if (clickOffset != null) {// must be dragging container.x = mouseX - clickOffset.x; container.y = mouseY - clickOffset.y; } } //drag circle----------------- private function startcircleDrag(event:MouseEvent) { clickOffsetcircle = new Point(event.localX, event.localY); } // user released private function stopcircleDrag(event:MouseEvent) { clickOffsetcircle = null; } // run every frame private function dragcircle(event:Event) { if (clickOffsetcircle != null) {// must be dragging circle.x = mouseX - clickOffsetcircle.x; circle.y = mouseY - clickOffsetcircle.y; } } } }