package mmo.utils { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.DisplayObject; import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; import flash.geom.ColorTransform; import flash.geom.Point; import flash.geom.Rectangle; import flash.utils.getTimer; import mmo.manager.SystemManger; import mmo.ui.core.UISprite; public class CoolDown extends Sprite { private const startFrom:Number = -90 / 180 * Math.PI; private var running:Boolean = false; private var coolColor:uint; private var halfW:Number; private var halfH:Number; /**存放4个角的弧度/存放4个角的X坐标/存放4个角的Y坐标*/ private var radianList:Array; private var pointXList:Array; private var pointYList:Array; /**开始的时间/总时间*/ private var beginTime:Number; private var cookTime:Number private var totalTime:Number; private var maskShape:Shape; private var bitmap:Bitmap; private var speedFrm:int = 0; private var keyId:uint; private var _fireEvent:Boolean; public function CoolDown(display:DisplayObject,fireEvent:Boolean=false) { this.mouseChildren = false; this.mouseEnabled = false; this._fireEvent = fireEvent; if(display){ setTarget(display); } } public function setTarget(display:DisplayObject):void{ if(!maskShape)maskShape = new Shape(); addChild(maskShape); if (!bitmap)bitmap = new Bitmap(); if (bitmap.bitmapData) bitmap.bitmapData.dispose(); var rect:Rectangle = display.getBounds(display.parent); halfW = rect.width * 0.5; halfH = rect.height * 0.5; pointXList = [-halfW, -halfW, halfW, halfW]; pointYList = [-halfH, halfH, halfH, -halfH]; radianList = [Math.atan2(-halfH, -halfW) + Math.PI * 2, Math.atan2( halfH, -halfW), Math.atan2( halfH, halfW), Math.atan2( -halfH, halfW), startFrom]; var bitmapData:BitmapData = new BitmapData(rect.width, rect.height); var staPoint:Point = display.parent.localToGlobal(new Point(rect.x, rect.y)); maskShape.graphics.beginFill(0x222222); maskShape.graphics.drawRect(0,0,rect.width+5, rect.height+5); maskShape.graphics.endFill(); bitmapData.draw(maskShape); bitmap.bitmapData = bitmapData; bitmap.x = -halfW; bitmap.y = -halfH; addChild(bitmap); bitmap.mask = maskShape; bitmap.alpha = 0.8; this.x = rect.x + halfW; this.y = rect.y + halfH; display.parent.addChildAt(this, display.parent.getChildIndex(display) + 1); } public function start(totalTime:Number,cookTime:Number=0):void{ if (maskShape){ this.totalTime = totalTime; this.cookTime = cookTime; beginTime = getTimer(); running = true; /**先画一个矩形*/ maskShape.graphics.beginFill(0x333333); maskShape.graphics.drawRect(-halfW,-halfW,halfW * 2+4,halfH * 2+4); maskShape.graphics.endFill(); if(!keyId) keyId = SystemManger.frameAdv.setInterval(speedFrm,enterFrameHandler); } } private function enterFrameHandler():void{ var postTime:Number = getTimer()-beginTime+cookTime; maskShape.graphics.clear(); /**时间到了*/ if (postTime >= totalTime){ SystemManger.frameAdv.clearForId(keyId); running = false; if(_fireEvent){ this.dispatchEvent(new Event(Event.COMPLETE)); } }else{ maskShape.graphics.beginFill(0x333333); maskShape.graphics.lineTo(0,-halfH); var currRadian:Number = 2 * Math.PI * (postTime / totalTime) + startFrom; var px:Number, py:Number; for (var i:int = 0; i < 5; i++){ var radian:Number = radianList[i]; trace("radian ="+radian); if (currRadian < radian){ maskShape.graphics.lineTo(pointXList[i], pointYList[i]); }else{ if (1 == i || 3 == i){ px = 1 == i ? -halfW : halfW; py = Math.tan(currRadian) * px; }else{ py = 2 == i ? halfH : -halfH; px = py / Math.tan(currRadian); } maskShape.graphics.lineTo(px, py); break; } } maskShape.graphics.lineTo(0, 0); maskShape.graphics.endFill(); } } public function get isRunning():Boolean { return running; } public function get color():uint{ return coolColor; } public function set color(value:uint):void { if (coolColor!=value){ coolColor = value; if(bitmap){ var r:Number = (value & 0xFF0000) >>> 16; var g:Number = (value & 0x00FF00) >>> 8; var b:Number = value & 0x0000FF; bitmap.transform.colorTransform = new ColorTransform(0, 0, 0, 1, r, g, b); } } } public function dispose():void{ //super.dispose(); SystemManger.frameAdv.clearForId(keyId); maskShape&&(maskShape.graphics.clear()); radianList = null; pointXList = null; pointYList = null; maskShape = null; bitmap = null; } } } _cool=new CoolDown(sp, true); _cool.addEventListener(Event.COMPLETE, onCoolCom); _cool.start(8000); } var _cool:CoolDown private function onCoolCom(evt:Event):void { if (_cool) endCool(); } private function endCool():void { _cool.removeEventListener(Event.COMPLETE, onCoolCom); _cool.dispose(); _cool=null; }