AS3 Easesprite - 마우스가 지나갈 때 부드럽게 크기를 조절하는 간단한 버튼

1598 단어
package
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 *  
	 * @author Jave.Lin
	 */	
	public class EaseSprite extends Sprite
	{
		private var _easing:Number=0.3;
		protected var _bigScaleX:Number=1.5;
		protected var _bigScaleY:Number=1.5;
		private var _isMouseOver:Boolean=false;
		
		public function EaseSprite()
		{
			if(stage)
			{
				addToStage();
			}
			else this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
		}
		
		private function addToStage(e:Event = null):void
		{
			buttonMode=true;
			addEventListener(MouseEvent.MOUSE_OVER,mouseOver);
			addEventListener(MouseEvent.MOUSE_OUT,mouseOut);
		}
		
		private function onEnterFrameHandler(e:Event):void
		{
			var tx:Number;
			var targetScale:Number;
			
			if(_isMouseOver)
			{
				targetScale=_bigScaleX;
			}else
			{
				targetScale=1;
			}
			tx=targetScale-scaleX;
			
			scaleX+=tx*_easing;
			scaleY+=tx*_easing;
			if(Math.abs(tx)<0.01)
			{
				scaleX=targetScale;
				scaleY=targetScale;
				removeEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
			}
		}
		
		private function mouseOver(e:MouseEvent):void
		{
			_isMouseOver=true;
			if(!hasEventListener(Event.ENTER_FRAME))
			{
				addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
			}
		}
		
		private function mouseOut(e:MouseEvent):void
		{
			_isMouseOver=false;
			if(!hasEventListener(Event.ENTER_FRAME))
			{
				addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
			}
		}
	}
}

좋은 웹페이지 즐겨찾기