hanyeah 专注于AS

动态改变注册点

flash中元件的注册点(锚点)是不能动态改变的,但是我们可以通过AS实现类似的效果。

先看效果:

获得 Adobe Flash Player

代码如下:

AnchorMc.as

package {
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	public class AnchorMc extends Sprite {
		public static const CENTER:String = "center";
		public static const LEFT:String = "left";
		public static const RIGHT:String = "right";
		public var dis:DisplayObject;
		public function AnchorMc(dis:DisplayObject):void {
			this.dis = dis;
			this.addChild(dis);
			this.x = dis.x;
			this.y = dis.y;
			dis.x = dis.y = 0;
			
			var sp:Shape = new Shape();
			addChild(sp);
			sp.graphics.beginFill(0x00ff00);
			sp.graphics.drawCircle(0, 0, 10);
			sp.graphics.endFill();
		}
		public function setAnchorType(xtype:String, ytype:String):void {
			var rect:Rectangle = dis.getRect(this);
			var p:Point = new Point(rect.x,rect.y);
			switch(xtype) {
				case LEFT:
					p.x = rect.x;
					break;
				case CENTER:
					p.x = rect.x+rect.width*0.5;
					break;
				case RIGHT:
					p.x = rect.x+rect.width;
					break;
			}
			switch(ytype) {
				case LEFT:
					p.y = rect.y;
					break;
				case CENTER:
					p.y = rect.y+rect.height*0.5;
					break;
				case RIGHT:
					p.y = rect.y+rect.height;
					break;
			}
			setAnchor(p);
		}
		public function setAnchor(pos:Point):void {
			var mt:Matrix = this.transform.matrix;
			var p:Point = mt.deltaTransformPoint(pos);
			this.x += p.x;
			this.y += p.y;
			dis.x -= pos.x;
			dis.y -= pos.y;
		}
	}
}

Main.as

package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	
	public class Main extends MovieClip {
		
		public var mc:MovieClip;
		public var p:MovieClip;
		
		private var anchorMc:AnchorMc;
		public function Main() {
			// constructor code
			
			anchorMc = new AnchorMc(mc);
			addChild(anchorMc);
			setAnchor(p);
			addChild(p);
			//anchorMc.setAnchorType(AnchorMc.CENTER, AnchorMc.CENTER);
			
			p.addEventListener(MouseEvent.MOUSE_DOWN, pmouseHandler);
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		private function setAnchor(p:MovieClip):void {
			var rect:Rectangle = p.getRect(anchorMc);
			anchorMc.setAnchor(new Point(rect.x+rect.width*0.5, rect.y+rect.height*0.5));
		}
		private function enterFrameHandler(e:Event):void 
		{
			anchorMc.rotation++;
		}
		
		private function pmouseHandler(e:MouseEvent):void 
		{
			p.startDrag(false, new Rectangle(0, 0, 550, 400));
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
		
		private function mouseMoveHandler(e:MouseEvent):void 
		{
			setAnchor(p);
		}
		
		private function mouseUpHandler(e:MouseEvent):void 
		{
			p.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		}
	}
	
}

源码打包下载

2015年10月27日 | 发布:hanyeah | 分类:as3.0笔记 | 评论:0

发表留言: