package{ import flash.display.DisplayObject; import flash.display.DisplayObjectContainer; import flash.display.GradientType; import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Matrix; import flash.geom.Point; import flash.text.TextField; import flash.utils.getQualifiedClassName; /** * @author Frost.Yen * @E-mail 871979853@qq.com * @create 2015-10-12 下午4:28:13 * */ [SWF(width="800",height="600")] public class DynamicRegistration extends Sprite { private var _obj:Sprite; private var _pot:Shape; private var _value:Number = 1; private var _property:Array = ["width","height","scaleX","scaleY","rotation"]; private var _currPro:String = "width"; public function DynamicRegistration() { initViews(); initEventListeners(); } private function initViews():void { var matr:Matrix = new Matrix(); matr.createGradientBox(480, 100, 0, 0, 0); _pot = new Shape(); _obj = new Sprite(); _obj.graphics.beginGradientFill(GradientType.LINEAR,[0xFF0000, 0xFF00FF],[1,1],[0,255],matr); _obj.graphics.drawRect(0,0,200,150); _obj.graphics.endFill(); _obj.x = (stage.stageWidth - _obj.width )*0.5; _obj.y = (stage.stageHeight - _obj.height)*0.5; _pot.graphics.beginFill(0,.5); _pot.graphics.drawCircle(0,0,5); _pot.graphics.endFill(); _pot.x = _obj.x; _pot.y = _obj.y; for(var i:int = 0;i<_property.length;i++){ var pro:TextField = getButton(" "+_property[i]+" ",this,stage.stageWidth-100,150+30*i,_property[i]); pro.addEventListener(MouseEvent.CLICK,onSelectProperty); } setProState(_currPro); this.addChild(_obj); this.addChild(_pot); } private function initEventListeners():void { _obj.addEventListener(MouseEvent.CLICK,onClick); } private function onClick(e:MouseEvent):void { _value = _obj[_currPro]; switch(_currPro){ case _property[0]: case _property[1]: _value+=10; break; case _property[2]: case _property[3]: _value+=0.2; break; case _property[4]: _value+=20; break; } dynamicRegistration1(_obj,new Point(_obj.mouseX,_obj.mouseY),_currPro,_value); _pot.x = mouseX; _pot.y = mouseY; } private function onSelectProperty(e:MouseEvent):void { _currPro = e.currentTarget.name; setProState(_currPro); } private function setProState(pro:String):void { for(var i:int = 0;i<_property.length;i++){ if(_property[i] == pro){ (this.getChildByName(_property[i]) as TextField).borderColor = 0x00ffff; }else{ (this.getChildByName(_property[i]) as TextField).borderColor = 0x222222; } } } private function getButton(text:String,parent:Sprite,x:Number=0,y:Number=0,name:String=null):TextField { var textButton:TextField = new TextField(); textButton.autoSize = "left"; textButton.width = 110; textButton.height = 38; textButton.selectable = false; textButton.border = true; textButton.borderColor = 0x222222; textButton.background = true; textButton.backgroundColor = 0xaaaaaa; textButton.htmlText = ""+text+""; textButton.x = x; textButton.y = y; if(name!=null){ textButton.name = name; } parent.addChild(textButton); return textButton; } /** * 动态改变注册点 * target 改变注册点的对象 * point 新的注册点 * pro 需要改变的属性 * value 新的属性值 */ private function dynamicRegistration1(target:DisplayObject,point:Point,pro:String,value:Number):void { //转换为全局坐标 var A:Point=target.parent.globalToLocal(target.localToGlobal(point)); if(pro == "x"||pro == "y"){ target[pro] = value-point[pro]; }else{ target[pro]=value; var B:Point = target.parent.globalToLocal(target.localToGlobal(point)); //把注册点从B点移到A点 target.x+=A.x-B.x; target.y+=A.y-B.y; } } } }