写微信小游戏时写的,功能可能还有待改进。目前这里只是在逻辑层获取了当前的系统时间,所以离开页面倒计时会停止(所以想要完成场景跳转之后仍在继续倒计时的功能,必须在逻辑层取回离开某个场景时的时间来做条件运算,就可以得到一个动态显示倒计时的功能),但可以实现倒计时的功能,下面的代码是每5分钟,恢复一次体力值的代码,体力恢复到10就停止恢复(有更好建议的人,欢迎留言。由于代码是截取出来的,也不排除存在错误的可能性):
---------------------------------------------------------------------------------------------------------------------------------
timeCount.js
---------------------------------------------------------------------------------------------------------------------------------
var cg = require("logic");
properties: {
powerDisplay: cc.Prefab, //cocos中放预制的
powerCount: cc.Label, //cocos中显示能量的结点
timeDisplay: cc.Label, cocos中显示时间的结点
power_button: cc.Node, //点击的按钮
},
onload(){
this.powerNum = 8; //初始体力设置为8点
this.clickTime = 0; //获取点击体力按钮时的时间
this.timeLimit = 300; //一个体力恢复的时间
this.timeCount = this.timeLimit; //倒计时的时间
this.minute = Math.floor(this.timeLimit / 60); // 存放倒计时的分钟数 floor() 向下舍入
this.second = this.timeLimit % 60; //存放倒计时的秒数
}
power_click: function () {
console.log("点击能量按钮");
cg.power.setTime();
this.clickTime = cg.power.getTime();
//如果体力值小于体力值上限,开始显示下一次体力恢复的时间与当前的体力值
this.powerCount.string = this.powerNum + " /10";
this.schedule(function () {
if (this.timeCount != this.timeLimit) {
//刚点击时的时间显示
if (this.second < 10) {
this.timeDisplay.string = "0" + this.minute + ":" + "0" + this.second+"后回复1点体力";
}
else {
this.timeDisplay.string = "0" + this.minute + ":" + this.second+"后回复1点体力";
}
}
if (this.powerNum < 10) {
//cg.power.getNextTime() 逻辑层中取回下一次体力恢复的时间,也可以直接写成需要的秒数
if (this.clickTime < cg.power.getNextTime()) {
this.clickTime++;
this.timeCount--; //控制点击时倒计时的显示
if (this.timeCount != this.timeLimit) {
if (this.second == 0) {
if(this.minute == 0){
this.minute =4;
this.second = 60; //重置的时间从60秒开始就不会从58 秒开始显示时间
}else {
this.minute--;
this.second = 60;
}
}
else if (this.second < 10) {
this.timeDisplay.string = "0" + this.minute + ":" + "0" + this.second+"后回复1点体力";
}
else {
this.timeDisplay.string = "0" + this.minute + ":" + this.second+"后回复1点体力";
}
this.second--;
}
this.powerCount.string = this.powerNum + " /10";
}
else {
this.powerNum++; //体力恢复的时间到了,且体力未回复满,体力数量加1
this.clickTime = this.clickTime - this.timeLimit;
this.powerCount.string = this.powerNum + " /10";
this.timeCount = this.timeLimit;
this.minute = 4;
this.second = 60;
if (this.timeCount != this.timeLimit) {
if (this.second == 0) {
if(this.minute == 0){
this.minute =4;
this.second = 60;
}else {
this.minute--;
this.second = 60;
}
}
else if (this.second < 10) {
this.timeDisplay.string = "0" + this.minute + ":" + "0" + this.second+"后回复1点体力";
}
else {
this.timeDisplay.string = "0" + this.minute + ":" + this.second+"后回复1点体力";
}
}
}
}
else if (this.powerNum == 10) {
this.clickTime = this.clickTime - this.timeLimit;
this.powerCount.string = this.powerNum + " /10";
this.timeDisplay.string = "体力恢复已满";
return;
}
}, 1);
},
---------------------------------------------------------------------------------------------------------------------------------
logic_power.js 逻辑层的一个子模块脚本
---------------------------------------------------------------------------------------------------------------------------------
var cg = require("logic");
var Power = function () {
module = this;
var date = new Date();
var time = 0;
var nextTime = 0;
module.setTime = function () {
time = date.getTime() / 1000;//date.getTime()获取点击时的系统时间,并计算下一次体力恢复的时间 getTime()得到的是毫秒
console.log(time);
};
module.getTime = function () {
return time;
};
module.getNextTime = function () {
//nextTime=time+5*60;
nextTime = time + 300;
console.log(nextTime);
return nextTime;
};
};
cg.power = new Power();
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
logic.js 逻辑层脚本
-----------------------------------------------------------------------------------------
var cg = {};
module.exports = cg;