Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d js 3.2 技能冷却按钮的简单实现大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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);

参数依次为:

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

大佬总结

以上是大佬教程为你收集整理的cocos2d js 3.2 技能冷却按钮的简单实现全部内容,希望文章能够帮你解决cocos2d js 3.2 技能冷却按钮的简单实现所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。