Instantly share code, notes, and snippets.
Last active
December 11, 2015 03:18
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save lynxerzhang/4536528 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils | |
{ | |
import flash.display.SimpleButton; | |
import flash.errors.IllegalOperationError; | |
import flash.utils.Dictionary; | |
/** | |
* | |
* @example | |
* | |
* 点击某按钮在关闭父级界面的同时, 再次添加至舞台, 会发现改按钮状态仍为over状态, 所以暂时解决就是 | |
* 重新new一个simpleButton对象, 或者直接用Sprite或者MovieClip来代替SimpleButton也可以避免 | |
* 这个问题。 | |
* OddSimpleButtonStateUtil.addDetect(_confirmBtn, {"click":clickConfirmBtnHandler}); | |
* | |
* maybe you need update btn's coordinate | |
* OddSimpleButtonStateUtil.updatePos(_confirmBtn, this._confirmBtn.x, this._confirmBtn.y); | |
* | |
*/ | |
public class OddSimpleButtonStateUtil | |
{ | |
public function OddSimpleButtonStateUtil() | |
{ | |
throw new IllegalOperationError("OddSimpleButtonStateUtil is static class"); | |
} | |
private static const map:Dictionary = new Dictionary(); | |
public static function addDetect(sbtn:SimpleButton, eventListener:Object = null):void{ | |
if(map[sbtn]){ | |
trace("has been register this simpleButton instance"); | |
return; | |
} | |
map[sbtn] = new OddState(sbtn, eventListener); | |
} | |
public static function removeDetect(sbtn:SimpleButton):void{ | |
if(map[sbtn]){ | |
var d:OddState = map[sbtn] as OddState; | |
if(d){ | |
d.dispose(); | |
} | |
delete map[sbtn]; | |
} | |
} | |
public static function updatePos(sbtn:SimpleButton, initX:Number, initY:Number):void{ | |
if(map[sbtn]){ | |
var d:OddState = map[sbtn] as OddState; | |
if(d){ | |
d.setInitXY(initX, initY); | |
} | |
} | |
} | |
} | |
} | |
import flash.display.DisplayObject; | |
import flash.display.DisplayObjectContainer; | |
import flash.display.SimpleButton; | |
import flash.events.Event; | |
import flash.geom.Rectangle; | |
internal class OddState{ | |
private var _overState:DisplayObject; | |
private var _downState:DisplayObject; | |
private var _upState:DisplayObject; | |
private var _hitTestState:DisplayObject; | |
private var _btn:SimpleButton; | |
public function OddState(sbtn:SimpleButton, eventListener:Object):void{ | |
this._btn = sbtn; | |
this._overState = this._btn.overState; | |
this._downState = this._btn.downState; | |
this._hitTestState = this._btn.hitTestState; | |
this._upState = this._btn.upState; | |
this._initBtnX = this._btn.x; | |
this._initBtnY = this._btn.y; | |
this._eventListener = eventListener; | |
updateListener(); | |
} | |
public function setInitXY(initX:Number, initY:Number):void{ | |
if(this._initBtnX != initX){ | |
this._initBtnX = initX; | |
} | |
if(this._initBtnY != initY){ | |
this._initBtnY = initY; | |
} | |
} | |
private function updateListener():void{ | |
for(var event:String in this._eventListener){ | |
this._btn.addEventListener(event, this._eventListener[event]); | |
} | |
this._btn.addEventListener(Event.ADDED_TO_STAGE, addToStageBtnHandler); | |
this._btn.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStageBtnHandler); | |
} | |
private var _eventListener:Object; | |
private var _initBtnX:Number; | |
private var _initBtnY:Number; | |
private function addToStageBtnHandler(evt:Event):void{ | |
this._btn.x = this._initBtnX; | |
this._btn.y = this._initBtnY; | |
} | |
private function removeFromStageBtnHandler(evt:Event):void{ | |
var rect:Rectangle = _btn.getBounds(_btn.stage); | |
if(rect.contains(_btn.stage.mouseX, _btn.stage.mouseY)){ | |
removeListener(); | |
var c:DisplayObjectContainer = this._btn.parent; | |
var d:int = c.getChildIndex(this._btn); | |
c.removeChild(this._btn); | |
this._btn = this.createBtn(); | |
c.addChildAt(this._btn, d); | |
this._btn.x = this._initBtnX; | |
this._btn.y = this._initBtnY; | |
updateListener(); | |
} | |
} | |
public function dispose():void{ | |
this.removeListener(); | |
if(this._btn.parent){ | |
this._btn.parent.removeChild(this._btn); | |
} | |
this._overState = null; | |
this._downState = null; | |
this._upState = null; | |
this._hitTestState = null; | |
this._btn = null; | |
this._eventListener = null; | |
} | |
private function removeListener():void{ | |
this._btn.removeEventListener(Event.ADDED_TO_STAGE, addToStageBtnHandler); | |
this._btn.removeEventListener(Event.REMOVED_FROM_STAGE, removeFromStageBtnHandler); | |
for(var event:String in this._eventListener){ | |
this._btn.removeEventListener(event, this._eventListener[event]); | |
} | |
this._btn.upState = null; | |
this._btn.downState = null; | |
this._btn.overState = null; | |
this._btn.hitTestState = null; | |
} | |
private function createBtn():SimpleButton{ | |
var _simbtn:SimpleButton = new SimpleButton(); | |
_simbtn.upState = this._upState; | |
_simbtn.overState = this._overState; | |
_simbtn.downState = this._downState; | |
_simbtn.hitTestState = this._hitTestState; | |
return _simbtn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment