ResultBar.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. var webView = require("../WebView");
  2. var lib = require("../Library");
  3. cc.Class({
  4. extends: cc.Component,
  5. properties: {
  6. playerCarorieBar: {
  7. default: null,
  8. type: cc.Node,
  9. serializable: true,
  10. },
  11. turntable: {
  12. default: null,
  13. type: cc.Node,
  14. serializable: true,
  15. },
  16. costTime: {
  17. default: null,
  18. type: cc.Node,
  19. serializable: true,
  20. },
  21. },
  22. init()
  23. {
  24. if (lib.openInWebview())
  25. {
  26. // 在app内Webview打开
  27. webView.register(this.node);
  28. this.node.on('onFruitInfo',this.onFruitInfo,this);
  29. }
  30. else {
  31. this.node.active = true;
  32. }
  33. this.calorie = this.playerCarorieBar.getChildByName('CalorieRichtext');
  34. this.calorieAvatar = this.playerCarorieBar.getChildByName('Mask').getChildByName('Avatar');
  35. this.timeRichText = this.costTime.getChildByName('TimeRichtext');
  36. this.scrollView1 = this.turntable.getChildByName('ScrollView1').getComponent(cc.ScrollView);
  37. this.scrollView2 = this.turntable.getChildByName('ScrollView2').getComponent(cc.ScrollView);
  38. this.scrollView3 = this.turntable.getChildByName('ScrollView3').getComponent(cc.ScrollView);
  39. this.scrollViewArr = [this.scrollView1,this.scrollView2,this.scrollView3];
  40. },
  41. updateAvatar()
  42. {
  43. lib.setImageBase64(webView.avatarBase64, function (texture2D) {
  44. this.calorieAvatar.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture2D);
  45. }.bind(this));
  46. },
  47. updateCalorie(calorie)
  48. {
  49. console.log('1111=',calorie);
  50. if (lib.openInWebview())
  51. {
  52. // 在app内Webview打开
  53. webView.getFruitInfo(calorie);
  54. }
  55. // <outline color=white width=2><size=50>300</></><size=30>卡</>
  56. this.calorie.getComponent(cc.RichText).string = '<outline color=white width=2><size=50>'+calorie+'</></><size=30>卡</>';
  57. },
  58. onFruitInfo(data) {
  59. /**
  60. * 获取水果图片信息
  61. * fruitBase64Url: "",// 水果雪碧图 base64
  62. * unitWidth: 100,// 雪碧图 单张图片宽
  63. * unitHeight: 100,// 雪碧图 单张图片高
  64. * unit: "px",// 雪碧图 单位
  65. * imageStartPosY: 0,// 雪碧图 起始图 Y方向位置
  66. * imageEndPosY: -1200,// 雪碧图 结束图 Y方向位置
  67. * fruitIndexArray: [0,0,0] // 输入卡路里后计算的返回结果,当前的数组
  68. */
  69. this.updateTurntable(data);
  70. console.log('22222=',data);
  71. },
  72. updateTurntable(data)
  73. {
  74. this.node.active = true;
  75. this.scheduleOnce(function () {
  76. let fArr = data.fruitIndexArray;
  77. for(let i = 0 ;i<fArr.length; i++)
  78. {
  79. let offset = fArr[i];
  80. if(offset!=0)
  81. {
  82. this.scrollViewArr[i].scrollToOffset(cc.v2(0, (offset+1)*100), (offset+1)/2, true);
  83. }
  84. }
  85. }, 1);
  86. },
  87. updateCostTime(time)
  88. {
  89. // <size=30>耗时: </><size=25>1天4小时5分3秒</>
  90. this.timeRichText.getComponent(cc.RichText).string = '<size=30>耗时: </><size=25>'+this.formatSeconds(time)+'</>';
  91. },
  92. /**
  93. * 格式化秒
  94. * @param int value 总秒数
  95. * @return string result 格式化后的字符串
  96. */
  97. formatSeconds(value) {
  98. let theTime = parseInt(value);// 需要转换的时间秒
  99. let theTime1 = 0;// 分
  100. let theTime2 = 0;// 小时
  101. let theTime3 = 0;// 天
  102. if (theTime > 60) {
  103. theTime1 = parseInt(theTime / 60);
  104. theTime = parseInt(theTime % 60);
  105. if (theTime1 > 60) {
  106. theTime2 = parseInt(theTime1 / 60);
  107. theTime1 = parseInt(theTime1 % 60);
  108. if (theTime2 > 24) {
  109. //大于24小时
  110. theTime3 = parseInt(theTime2 / 24);
  111. theTime2 = parseInt(theTime2 % 24);
  112. }
  113. }
  114. }
  115. let result = '';
  116. if (theTime > 0) {
  117. result = "" + parseInt(theTime) + "秒";
  118. }
  119. if (theTime1 > 0) {
  120. result = "" + parseInt(theTime1) + "分" + result;
  121. }
  122. if (theTime2 > 0) {
  123. result = "" + parseInt(theTime2) + "小时" + result;
  124. }
  125. if (theTime3 > 0) {
  126. result = "" + parseInt(theTime3) + "天" + result;
  127. }
  128. return result;
  129. }
  130. });