首页 > 代码库 > 在实际项目中实现把swf资源里面的MC进行转化成按钮,实现代码的复用
在实际项目中实现把swf资源里面的MC进行转化成按钮,实现代码的复用
下面是前不久一个游戏项目中的一小部分内容,拿出来只是为了说明,用AS3对加载进程序中swf内容进行封装变换处理
下面是这个例子,只不过不完整。。。
package com.ctrlcc.sanguo.view{ /** * C-公会转盘-视图-主视图 */ public class ZhuanPanView extends Window { private var _panel:MovieClip; private var _closeBtn:EButton; private var _buyEBtn:EButton; private var _buyAllEbtn:EButton; private var _info:TextField; private var _cardsTF:TextField; private var _coinComBox:ComboBox; private var _id:int = 1; private var _timer:Timer; private var _zhenArray:Array; private var _rewards:Object; private var _costFlag:int = 0; private var _costObj:Object; private var _isPlaying:Boolean = false; private var _cardBtn:EButton; public function ZhuanPanView() { this._rewards = new Object(); this._costObj = new Object(); this.removeThenDispose = true; GameLoader.getIns().load(["zhuanpan_view"], this.inits); }// end function public function inits() : void { this.addValue(); this.addEvent(); this.globalUpdate([UpdateType.TYPE_VIP_CHANGE, UpdateType.TYPE_FCM, UpdateType.TYPE_DEPOT]); }// end function public function initPanel() : void { this._panel = ResFuns.getRes("Res_ZhuanpanView"); this.addChild(this._panel); }// end function public function addValue() : void { this.align = AlignType.ALIGN_CENTER; this.initPanel(); this.initBtn(); this.initZhizhen(); GFuns.setStageCenter(this); }// end function public function initBtn() : void { this._closeBtn = new EButton(); this._closeBtn.changeBtn(this._panel.closeBtn); // 开始按钮 this._buyEBtn = new EButton(); this._buyEBtn.changeBtn(this._panel.signBtn); this.addChild(this._buyEBtn); this._cardsTF = this._panel.cardsTF; // 贡献令btn this._cardBtn = new EButton(); this._cardBtn.changeBtn(this._panel.cardBtn); this._cardBtn.toolTip = "在公会会堂内可以换取或在怪物攻城和公会战活动中获得"; }// end function public function initZhizhen():void { _zhenArray = new Array(); var zhen:MovieClip; var nu:int = 10; for(var i:int =1; i<= nu;i++) { zhen = this._panel.zhizhen.getChildByName("zhen"+i ); zhen.gotoAndStop(1); if(i>1) { zhen.visible = false; } this._zhenArray.push(zhen); } } public function addEvent() : void { this._closeBtn.addEventListener(MouseEvent.CLICK, this.onMouseHandler); this._buyEBtn.addEventListener(MouseEvent.CLICK, this.onMouseHandler); }// end function private function alertZhuanPanCallBack(_arg1:String) : void { if (_arg1 == "RECHARGE") { TxPublicHandler.instance().costzijing("K1001", 20, "1002.png", this.qqZhuanPanCallBack); setTimeout(this.startMouseEnable, 4000); } else { this.startMouseEnable(); } }// end function public function onm ouseHandler(event:MouseEvent) : void { var _loc_2:ConfigValueLO = null; var _loc_3:Object = null; var _loc_4:int = 0; var _loc_5:String = null; var _loc_8:ItemBoxDO = null; var _loc_6:String = ""; var _loc_7:* = GameTimerUtils.getInstance().getGameTime().time; switch(event.target) { case this._closeBtn: { WinsManager.getIns().close(this); break; } case this._buyEBtn: { _loc_8 = DepotData.getIns().getItemBoxByIndex("41001"); if (_loc_8 == null) { PopMsgManager.instance().show("缺少贡献令牌"); return; } this._buyEBtn.mouseEnabled = false; AppFrame.instance.sendInstruction(new Instruction(InnerActivityCmd.ACTION_ZHUANPAN_CMD)); break; } default: { break; } } }// end function private function startMouseEnable() : void { this._buyEBtn.mouseEnabled = true; }// end function public function qqZhuanPanCallBack(_arg1:String) : void { this._buyEBtn.mouseEnabled = false; this._closeBtn.mouseEnabled = false; }// end function public function isCanClick() : String { this._costFlag = DayLoginData.getInstance().zhuanpanCost; if (this._costFlag == 1) { if (GameRuleSys.getGold() < 20) { ZhuanpanTextType.setConfig(); return ZhuanpanTextType.noGold; } this._costObj.type = 1; } else { DayLoginData.getInstance().zhuanpanCost = 1; this._costObj.type = 1; } return ""; }// end function public function setCostFlag(_arg1:int) : void { this._costFlag = _arg1; }// end function public function setRewards(_arg1:Object) : void { this._rewards = _arg1; }// end function public function anima(idx:int) : void { this._zhenArray[(this._id -1)].visible = false; var _loc_2:* = idx; this._id = idx; this.play(_loc_2); }// end function public function play(id:int) : void { this._zhenArray[0].visible = false; var zhen:MovieClip = this._zhenArray[(id-1)]; zhen.visible = true; MCPlayer.instance.setMc(zhen, this.callBackFun, []); }// end function public function callBackFun() : void { this._zhenArray[(this._id-1)].gotoAndStop(110); this._buyEBtn.mouseEnabled = true; this._closeBtn.mouseEnabled = true; // this.getRewards(); }// end function private function getRewards() : void { var _loc_1:RewardList = null; if (this._rewards != null) { _loc_1 = RewardFuns.getRewardList(this._rewards); PopMsgManager.instance().show("获得:" + _loc_1.outputAllRewardDesc().toString()); RewardFuns.setRewardToGameObj(_loc_1); } }// end function private function sendStory() : void { var _loc_1:* = this._rewards.amount; var _loc_2:* = LocalData.getIns().getShareConfigString("WB0009"); _loc_2.msg = String(_loc_2.msg).replace("$ITEM$", _loc_1); TxPublicHandler.instance().sendStory(_loc_2); }// end function private function getAllCoin(_arg1:int = 10) : Object { var _loc_2:Object = null; var _loc_3:Object = null; var _loc_4:int = 0; var _loc_5:* = UserData.instance.vipLo.buyCount - UserData.instance.buyMoneyCount; _arg1 = _loc_5 > _arg1 ? (_arg1) : (_loc_5); var _loc_6:* = UserData.instance.buyMoneyCount + _arg1; var _loc_7:* = UserData.instance.buyMoneyCount + 1; while (_loc_7 <= _loc_6) { _loc_3 = LocalData.getIns().getBuyMoneyNeedGold(_loc_7); if (_loc_3) { _loc_4 = _loc_4 + int(_loc_3.costGold); } _loc_7 = _loc_7 + 1; } _loc_2 = {num:_arg1, gold:_loc_4}; return _loc_2; }// end function private function reFun(_arg1:String) : void { if (_arg1 == AlertAs.OK) { AppFrame.instance.sendInstruction(new Instruction(AddUserCmd.ACTION_COMMAND_BUY_MONEY, true)); } }// end function private function updateCards() : void { var _loc_3:int = 0; var _loc_1:* = DepotData.getIns().getAllItemsByIndexExceptTemp("41001"); var _loc_2:int = 0; if (_loc_1) { _loc_3 = 0; while (_loc_3 < _loc_1.length) { _loc_2 = _loc_2 + _loc_1[_loc_3].amount; _loc_3 = _loc_3 + 1; } } this._cardsTF.text = _loc_2 + ""; }// end function override public function recall() : void { GuideData.instance().guideNextStep(GuideType.OPEN_COIN, this); this.startMouseEnable(); }// end function override public function globalUpdate(_arg1:Array) : void { var _loc_2:int = 0; if (this._panel) { } if (_arg1 != null) { _loc_2 = 0; while (_loc_2 < _arg1.length) { switch(_arg1[_loc_2] as int) { case UpdateType.TYPE_VIP_CHANGE: { if (UserData.instance.vip >= 6) { } break; } case UpdateType.TYPE_FCM: { break; } case UpdateType.TYPE_DEPOT: { this.updateCards(); break; } default: { break; } } _loc_2 = _loc_2 + 1; } } }// end function private function sedInstructions(_arg1:String, _arg2:Object, _arg3:Object = null, _arg4:String = null) : void { var _loc_5:* = new Instruction(_arg1, _arg2, 0, _arg3, InstructionType.TYPE_SEND); AppFrame.instance.sendInstruction(_loc_5); }// end function }}
从上面可以看到GameLoader类是加载整个的SWF文件(这里是一个公会转盘的SWF文件),集体的加载过程是在ResFuns类的getRes()方法中,这是一个封装很好的项目,学到了很多东西。。
看到这一句了吗?? this._buyEBtn = new EButton(); this._buyEBtn.changeBtn(this._panel.signBtn);这里是使用了EButton类里面的一个方法changeBtn把this._panel.signBtn这个MC转化为了按钮,使这个MC具有了按钮的所有功能(主要还是监听事件,实现鼠标over和out的效果等),这样EButton类可以实现把任何4帧的MC转化成按钮,提高了代码的复用。。
下面就是EButton这个类:
EButton类
package com.ctrlcc.framework.util{ dynamic public class EButton extends GameSprite { public var _id:int; private var _enabled:Boolean = false; private var _upskin:BitmapData; private var _overskin:BitmapData; private var _downskin:BitmapData; private var _enabledskin:BitmapData; private var _qg:Sprite; private var _bm:Bitmap; private var _shine:Boolean; private var _shineFun:Function; private var _shineModel:Boolean = false; private var _tf:TextField; private var _upfif:Array; private var _upcolor:uint; private var _overfif:Array; private var _overcolor:uint; private var _downfif:Array; private var _downcolor:uint; private var _enabledfif:Array; private var _enabledcolor:uint; private var _label:String = ""; private var _htmlLabel:String = ""; private var _type:String = "OVER"; private var _toolTip:String; private var _clickFun:Function; private var _registerId:int; private var _light:Boolean; private var _lightMc:MovieClip; private var _locateLight:Boolean; private var _lightX:int; private var _lightY:int; public function EButton() { this._shineFun = this.shineDraw; this.init(); }// end function private function init() : void { this._bm = new Bitmap(this._upskin); addChild(this._bm); this._qg = DisplayUtils.getEmptySprite(); addChild(this._qg); this.addEvent(); }// end function public function set TextColor(_arg1:uint) : void { this._upcolor = _arg1; this._overcolor = _arg1; this._downcolor = _arg1; this._enabledcolor = _arg1; this._tf.textColor = _arg1; }// end function protected function addEvent() : void { this.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOverHandler); this.addEventListener(MouseEvent.MOUSE_OUT, this.onMouseOutHandler); this.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDownHandler); this.addEventListener(MouseEvent.MOUSE_UP, this.onMouseOverHandler); this.addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMoveHandler); }// end function private function onm ouseDownHandler(event:MouseEvent) : void { if (this._enabled) { return; } this.setState("DOWN"); }// end function private function onm ouseOutHandler(event:MouseEvent) : void { if (this._enabled) { } else { this.setState("UP"); } GameToolTipsManager2.getInstance().close(); }// end function protected function onm ouseOverHandler(event:MouseEvent) : void { if (this._enabled) { } else { this.setState("OVER"); } }// end function private function onm ouseMoveHandler(event:MouseEvent) : void { if (this._enabled) { } else { this.setState("OVER"); } }// end function public function setState(_arg1:String) : void { switch(_arg1) { case "OVER": { this._bm.bitmapData = http://www.mamicode.com/this._overskin; this._type = _arg1; if (this._tf) { this._tf.textColor = this._overcolor; this._tf.filters = this._overfif; } break; } case "UP": { this._bm.bitmapData = http://www.mamicode.com/this._upskin; this._type = _arg1; if (this._tf) { this._tf.textColor = this._upcolor; this._tf.filters = this._upfif; } break; } case "DOWN": { this._bm.bitmapData = http://www.mamicode.com/this._downskin; this._type = _arg1; if (this._tf) { this._tf.textColor = this._downcolor; this._tf.filters = this._downfif; } break; } case "ENABLED": { this._bm.bitmapData = http://www.mamicode.com/this._enabledskin; this._type = _arg1; if (this._tf) { this._tf.textColor = this._enabledcolor; this._tf.filters = this._enabledfif; } break; } default: { break; } } }// end function public function changeBtn(movieClip:MovieClip) : void { this.setSkin(movieClip); if (movieClip.parent != null) { this.move(movieClip.x, movieClip.y); movieClip.parent.addChildAt(this, movieClip.parent.getChildIndex(movieClip)); movieClip.parent.removeChild(movieClip); } }// end function /** * 设置按钮的皮肤 * @param _arg1 */ public function setSkin(_arg1:MovieClip) : void { var _loc_2:TextField = null; var _loc_3:TextField = null; var _loc_4:TextField = null; var _loc_5:Number = 0; // 跳到第一帧开始运行 _arg1.gotoAndStop(1); if (_arg1.hasOwnProperty("label")) { this._tf = _arg1.label; if (this._tf) { this._label = this._tf.text; this._upfif = this._tf.filters; this._upcolor = this._tf.textColor; _arg1.label.text = ""; } else { this._upfif = null; this._upcolor = 0; } _loc_5 = this._tf.width; addChildAt(this._tf, 1); } else { this._upfif = null; this._upcolor = 0; } var _loc_6:uint = this._upcolor; this._enabledcolor = this._upcolor; this._downcolor = _loc_6; this._overcolor = _loc_6; var loc6:Array = this._upfif; this._enabledfif = this._upfif; this._downfif = loc6; this._overfif = loc6; this._upskin = this.getMcBitmapData(_arg1); _arg1.gotoAndStop(2); if (_arg1.hasOwnProperty("label1")) { _loc_2 = _arg1.label1; _arg1.label1.text = ""; if (_loc_2) { _loc_2.text = ""; this._overfif = _loc_2.filters; this._overcolor = _loc_2.textColor; } } this._overskin = this.getMcBitmapData(_arg1); _arg1.gotoAndStop(3); if (_arg1.hasOwnProperty("label2")) { _loc_3 = _arg1.label2; _arg1.label2.text = ""; if (_loc_3) { _loc_3.text = ""; this._downfif = _loc_3.filters; this._downcolor = _loc_3.textColor; } } this._downskin = this.getMcBitmapData(_arg1); _arg1.gotoAndStop(4); if (_arg1.hasOwnProperty("label3")) { _loc_4 = _arg1.label3; _arg1.label3.text = ""; if (_loc_4) { this._enabledfif = _loc_4.filters; this._enabledcolor = _loc_4.textColor; } } this._enabledskin = this.getMcBitmapData(_arg1); this._qg.width = _arg1.width; this._qg.height = _arg1.height; // 设置按钮状态 this.setState("UP"); this._bm.filters = null; this.label = this._label; this.buttonMode = true; this.useHandCursor = true; this.mouseChildren = false; }// end function public function get shine() : Boolean { return this._shine; }// end function public function set shine(_arg1:Boolean) : void { if (this._shine != _arg1) { this._shine = _arg1; if (this.shine) { this._registerId = GameTimerUtils.getInstance().FunctionRegister(0, this._shineFun); } else { GameTimerUtils.getInstance().removeRegister(this._registerId); this._bm.filters = null; } } }// end function public function get enabled() : Boolean { return this._enabled; }// end function public function set enabled(_arg1:Boolean) : void { this._enabled = _arg1; this.useHandCursor = !_arg1; this.buttonMode = !_arg1; if (this._enabled) { this.setState("ENABLED"); if (this.hasEventListener(MouseEvent.CLICK)) { if (this._clickFun != null) { this.removeEventListener(MouseEvent.CLICK, this._clickFun); } } } else { this.setState("UP"); if (this._clickFun != null) { this.addEventListener(MouseEvent.CLICK, this._clickFun); } } }// end function public function shineDraw() : void { if (this._shineModel) { this._bm.filters = null; } else { this._bm.filters = [this.glowFilter()]; } this._shineModel = !this._shineModel; }// end function public function close() : void { this.removeEventListener(MouseEvent.MOUSE_OVER, this.onMouseOverHandler); this.removeEventListener(MouseEvent.MOUSE_OUT, this.onMouseOutHandler); this.removeEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDownHandler); this.removeEventListener(MouseEvent.MOUSE_UP, this.onMouseOverHandler); this.removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMoveHandler); }// end function private function glowFilter() : BitmapFilter { var _loc_1:GlowFilter = new GlowFilter(); _loc_1.color = 16776960; _loc_1.alpha = 1; _loc_1.blurX = 6; _loc_1.blurY = 6; _loc_1.quality = BitmapFilterQuality.MEDIUM; this._bm.filters = [_loc_1]; return _loc_1; }// end function public function set htmlLabel(_arg1:String) : void { this._htmlLabel = _arg1; if (this._tf) { this._tf.htmlText = this._htmlLabel; } }// end function public function set label(_arg1:String) : void { this._label = _arg1; if (this._tf) { this._tf.text = this._label; } }// end function public function get label() : String { return this._label; }// end function public function get type() : String { return this._type; }// end function public function set type(_arg1:String) : void { this.enabled = true; this.setState(_arg1); }// end function private function getMcBitmapData(movieClip:MovieClip) : BitmapData { var clip:MovieClip = movieClip; var wid:Number = clip.width; var hei:Number = clip.height; var bitmapData:BitmapData = http://www.mamicode.com/new BitmapData(wid / clip.scaleX, hei / clip.scaleY, true, 16777215); bitmapData.draw(clip); return bitmapData; }// end function public function set clickFun(_arg1:Function) : void { if (this._clickFun != null) { this.removeEventListener(MouseEvent.CLICK, this._clickFun); } this._clickFun = _arg1; }// end function public function get light() : Boolean { return this._light; }// end function public function set light(_arg1:Boolean) : void { if (this._light != _arg1) { this._light = _arg1; if (this._light) { this._lightMc = ResFuns.getRes("Res_EButton_FloatEffect"); var _loc_2:Boolean = false; this._lightMc.mouseChildren = false; this._lightMc.mouseEnabled = _loc_2; /*this._lightMc.x = -this._lightMc.height/4; this._lightMc.y = -this._lightMc.width/4;*/ if (this._locateLight) { this._lightMc.x = -this._lightMc.height/4; this._lightMc.y = -this._lightMc.width/4; } //this._lightMc.height = this.width < this.height ? (this.width) : (this.height); //this._lightMc.width = this.width < this.height ? (this.width) : (this.height); addChild(this._lightMc); this._lightMc.play(); } else if (this._lightMc) { this._lightMc.stop(); if (this._lightMc.parent) { this._lightMc.parent.removeChild(this._lightMc); this._lightMc = null; } } } }// end function public function get locateLight() : Boolean { return this._locateLight; }// end function public function setLightPosition(_arg1:Boolean, _arg2:int = -1, _arg3:int = -1) : void { if (_arg1 == true) { this._lightX = _arg2; this._lightY = _arg3; } this._locateLight = _arg1; }// end function }}
这个EButton类同样继承自GameSprite类,GameSprite类就是底层的那个类了继承自Sprite类
package com.ctrlcc.framework.component.display{ import com.ctrlcc.framework.manager.*; import flash.display.*; import flash.events.*; public class GameSprite extends Sprite { private var _toolTip:Object; private var _keep:Boolean; private var mouseInSprite:Boolean = false; public function GameSprite() { }// end function public function onRemovedFromStage(event:Event) : void { this.removeEventListener(MouseEvent.MOUSE_OVER, this.rollOverHandler); this.removeEventListener(MouseEvent.MOUSE_MOVE, this.rollOverHandler); this.removeEventListener(MouseEvent.MOUSE_OUT, this.rollOutHandler); this.removeToolTips(); }// end function public function onAddedToStage(event:Event) : void { if (this._toolTip && this._toolTip != "") { this.addEventListener(MouseEvent.MOUSE_OVER, this.rollOverHandler); this.addEventListener(MouseEvent.MOUSE_MOVE, this.rollOverHandler); this.addEventListener(MouseEvent.MOUSE_OUT, this.rollOutHandler); } }// end function public function get toolTip() { return this._toolTip; }// end function public function set toolTip(value) : void { if (value =http://www.mamicode.com/= this._toolTip) { return; } this._toolTip = value; if (value && value != "") { this.addEventListener(MouseEvent.MOUSE_OVER, this.rollOverHandler); this.addEventListener(MouseEvent.MOUSE_MOVE, this.rollOverHandler); this.addEventListener(MouseEvent.MOUSE_OUT, this.rollOutHandler); this.addEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage); this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage); if (this.mouseInSprite == true) { this.showToolTips(); } } else { this.removeEventListener(MouseEvent.MOUSE_OVER, this.rollOverHandler); this.removeEventListener(MouseEvent.MOUSE_MOVE, this.rollOverHandler); this.removeEventListener(MouseEvent.MOUSE_OUT, this.rollOutHandler); this.removeToolTips(); } }// end function protected function rollOverHandler(event:MouseEvent) : void { this.mouseInSprite = true; this.showToolTips(); }// end function protected function rollOutHandler(event:MouseEvent) : void { this.mouseInSprite = false; this.removeToolTips(); }// end function protected function showToolTips() : void { if (this.toolTip && this.toolTip != "") { GameToolTipsManager2.getInstance().showToolTips(this.toolTip, this.mouseX, this.mouseY); } }// end function protected function removeToolTips() : void { if (this.toolTip && this.toolTip is String) { GameToolTipsManager2.getInstance().close(); return; } if (this.toolTip && this.toolTip != "") { if (this.toolTip is Sprite && GameToolTipsManager2.getInstance().info && GameToolTipsManager2.getInstance().info is Sprite && this.toolTip == GameToolTipsManager2.getInstance().info ) { GameToolTipsManager2.getInstance().close(); } } }// end function public function move(coordinateX:Number, coordinateY:Number) : void { this.x = coordinateX; this.y = coordinateY; }// end function public function setSize(width:int, height:int) : void { this.width = width; this.height = height; }// end function public function get keep() : Boolean { return this._keep; }// end function public function set keep(value:Boolean) : void { this._keep = value; }// end function }}
以上就是怎样在实际的项目开发中把4帧的MC转化成一个按钮,通过这种形式来实现代码的复用。。。
在实际项目中实现把swf资源里面的MC进行转化成按钮,实现代码的复用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。