加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d js 3.2 技能冷却按钮的简单实现

发布时间:2020-12-14 20:19:40 所属栏目:百科 来源:网络整理
导读:一个简单的技能冷却按钮的实现 var CoolButton = cc.Node.extend({// 需要做成Node 否则会无法addchildcallback : null,// 点击后的回调coolInterval : null,// 动画时间progressCooling : null,// 进度条sprNormal : null,sprStencil : null,menuBtn : null

一个简单的技能冷却按钮的实现

var CoolButton = cc.Node.extend({	// 需要做成Node 否则会无法addchild
	callback : null,// 点击后的回调
	coolInterval : null,// 动画时间
	progressCooling : null,// 进度条
	sprNormal : null,sprStencil : null,menuBtn : null,ctor : function(resNormal,resPressed,resStencil,coolInterval,callback) {
		this._super();
		
		this.callback = callback;
		this.coolInterval = coolInterval;
		
		// menu item
		var btnItem = new cc.MenuItemImage(
				resNormal,this.onBtnClick,this);

		// menu 默认在画面中间
		this.menuBtn = new cc.Menu(btnItem);
		this.menuBtn.attr({
			x : 0,y : 0
		});
		this.addChild(this.menuBtn,0);
		
		// 图片覆盖在按钮上  造成无法点击的假象
		this.sprNormal = new cc.Sprite(resNormal);
		this.sprNormal.attr({
			x : 0,y : 0
		});
		this.addChild(this.sprNormal,1);
		this.sprStencil = new cc.Sprite(resStencil);
		this.sprStencil.attr({
			x : 0,y : 0
		});
		this.addChild(this.sprStencil,2);
		
		
		this.progressCooling = new cc.ProgressTimer(this.sprNormal);
		this.progressCooling.setType(cc.ProgressTimer.TYPE_RADIAL);
		this.progressCooling.setPercentage(0);	// 回复到0
		this.progressCooling.attr({
			x : 0,y : 0
		});
		this.addChild(this.progressCooling,5);
		
		this.progressCooling.setVisible(false);
		this.sprNormal.setVisible(false);
		this.sprStencil.setVisible(false);
	},onBtnClick : function() {
		// 设置按钮不可按
		this.menuBtn.setVisible(false);
		// 开始倒计时
		this.progressCooling.setVisible(true);
		this.sprNormal.setVisible(true);
		this.sprStencil.setVisible(true);
		this.progressCooling.runAction(cc.sequence(cc.progressTo(this.coolInterval,100),cc.callFunc(this.coolEndCallback,this)));
		
		// 调用回调
		this.runAction(cc.callFunc(this.callback,this));
	},coolEndCallback : function() {
		this.menuBtn.setVisible(true);
		
		this.progressCooling.setVisible(false);
		this.progressCooling.setPercentage(0);	// 回复到0
		this.sprNormal.setVisible(false);
		this.sprStencil.setVisible(false);
	}
});


调用示例:

var btn = new CoolButton(res.SkillNormal_png,res.SkillPressed_png,res.SkillStencil_png,4,this.skill);

参数依次为:

普通状态图片资源,按下状态图片资源,遮罩层图片资源,冷却时间,按钮按下回调

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读