写微信小游戏时写的,功能可能还有待改进。目前这里只是在逻辑层获取了当前的系统时间,所以离开页面倒计时会停止(所以想要完成场景跳转之后仍在继续倒计时的功能,必须在逻辑层取回离开某个场景时的时间来做条件运算,就可以得到一个动态显示倒计时的功能),但可以实现倒计时的功能,下面的代码是每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;

cocos creator JS 实现微信小游戏体力倒计时恢复的代码相关推荐

  1. cocos creator 3.7微信小游戏开发云函数和云托管部署

    cocos creator开发跨平台游戏能力不错,可以一次开发适用于多个平台,如andriod,ios原生,网页,各类小程序等等.就实际情况而言,能够跨1-2个平台已经不容易了,这是因为在跨平台开发中 ...

  2. Cocos Creator 发布到微信小游戏的资源管理

    环境:Cocos Creator v1.9.1: 微信web开发者工具 v1.02.0808300,线上基础调试库 1.9.92. 以下,ccc指cocos creator.资源均指Texture.A ...

  3. pixi.js v5 微信小游戏版本

    pixi.js v5 微信小游戏群 881784250 历尽千辛万苦, pixi.js v5版本出了. pixi.js是相当完美的 性能是webgl 2d中最强的之一,也许可以把之一去掉 免费不收费 ...

  4. Cocos Creator 3D发布QQ小游戏并打APK包

    Cocos Creator 3D版本 : 1.2.0 Visual Studio版本:2017,typeScripts. 1. 构建QQ小游戏 1. 在菜单栏的项目里打开构建发布 因为QQ跟微信的平台 ...

  5. PIXI.JS兼容微信小游戏

    来自:PIXI兼容微信小游戏 - 天之道利而不害 - 博客园 PIXI兼容微信小游戏 首先导入官方的weapp-adapter,然后导入pixi.min.js,微信小程序使用ES6的module引用模 ...

  6. 用 Node.js 写微信小游戏辅助

    似乎使用 Python 写各种辅助的居多,我们来走一波 JS 流吧! GitHub:weixin-game-helper ? 微信小游戏辅助合集 目前已有的游戏辅助: 包你懂我 大家来找茬腾讯版 头脑 ...

  7. Cocos Creator开发制作的小游戏《5人足球》

    作为一个足球爱好者,眼看卡塔尔世界杯要来了,身残志坚的我总想着做点事满足一下"私欲".于是在每个夜深人静的时候狂敲120分钟,大概耗了2520分钟敲完了第一版.目前已经上线微信小游 ...

  8. 微信小游戏开发入门:示例代码介绍

    什么是微信小游戏? 看前几天的科技新闻,微信中的"跳一跳"小游戏从去年12月发布,截至到今年3月份已经积累了3.9亿玩家,这是一个多么恐怖的数字,"跳一跳"游戏 ...

  9. Cocos Creator 入门篇-描摹小游戏(描摹数字,描摹英语字母)

    cocos creator写游戏还是很友好的,官方文档给的也挺全.今天带大家来用cocoscreator实现描摹数字/描摹英文字母/描摹直线曲线的功能. 先看下效果图 实现思路 这里我们已描摹数字2为 ...

最新文章

  1. Shell脚本示例代码
  2. Webpack原理与实践
  3. linux 下面安装 .sh 格式的软件
  4. 6.1 引言-系统数据文件和信息
  5. C#性能优化:延迟初始化LazyT
  6. JavaSE各阶段练习题----多线程
  7. [转载] Python3 open()函数
  8. iOS 手势操作和事件传递响应链
  9. [3-6] xp系统主题下载最新xp系统软件更新 [yeskm整理]
  10. 计算机等级考试网络工程师介绍,全国计算机等级考试网络工程师
  11. Android对话框的使用总结
  12. 极好用的c++ http server和client库
  13. 听小S教你如何瘦小腿
  14. Halcon union_straight_contours_xld详解
  15. Office(Word/Excel/PPT)问题集
  16. git推送拉取需要验证信息
  17. wps将word转换成html,wps如何转换成word(word转换成wps的方法)
  18. 好玩的猜数游戏(不是二分查找!四位数)
  19. 单链表操作之合并两个单链表
  20. 武汉理工大学计算机学院转专业细则,武汉理工大学本科四条途径调整专业

热门文章

  1. PHP 伪静态seo,伪静态、静态、动态网站对SEO的影响有多大?
  2. doc es 中type_Elasticsearch基础——概念和基本API操作
  3. php 当前时间转换成农历,php如何实现公、农历日期的想互转换的实例
  4. 博弈论进阶之Anti-SG游戏与SJ定理(转载)
  5. .php过程化cms的理解,CMS是什么意思
  6. 0516编写西游记人物类
  7. App上传到应用宝的一些问题
  8. 软件测试学习心得体会
  9. 深圳月薪2300工厂小妹,逆天改命成google程序员,因为疫情失业了...孙玲:甘于平凡,但从不轻易认输...
  10. String类型转化为Double类型以及java代码去除金额千分号