Wheel.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. spinBtn: {
  5. default: null, // The default value will be used only when the component attachin // to a node for the first time
  6. type: cc.Button, // optional, default is typeof default
  7. visible: true, // optional, default is true
  8. displayName: 'SpinBtn', // optional
  9. },
  10. wheelSp: {
  11. default: null,
  12. type: cc.Sprite,
  13. tooltip: "转盘图片"
  14. },
  15. background: {
  16. default: null,
  17. type: cc.Sprite,
  18. tooltip: "背景图片"
  19. },
  20. pointer: {
  21. default: null,
  22. type: cc.Sprite,
  23. tooltip: "指针图片"
  24. },
  25. title: {
  26. default: null,
  27. type: cc.Sprite,
  28. tooltip: "欢天喜地幸运大转盘"
  29. },
  30. startbtn: {
  31. default: null,
  32. type: cc.Sprite,
  33. tooltip: "开始按钮"
  34. },
  35. maxSpeed: {
  36. default: 5,
  37. type: cc.Float,
  38. max: 20,
  39. min: 2,
  40. },
  41. duration: {
  42. default: 0.1,
  43. type: cc.Float,
  44. max: 2,
  45. min: 0.1,
  46. tooltip: "减速前旋转时间"
  47. },
  48. acc: {
  49. default: 2,
  50. type: cc.Float,
  51. max: 20,
  52. min: 0.01,
  53. tooltip: "加速度"
  54. },
  55. targetID: {
  56. default: 0,
  57. type: cc.Integer,
  58. max: 7,
  59. min: 0,
  60. tooltip: "指定结束时的齿轮"
  61. },
  62. springback: {
  63. default: false,
  64. tooltip: "旋转结束是否回弹"
  65. },
  66. ShowRewards: cc.Node,
  67. Rewards: cc.Node,
  68. RewardDetail: {
  69. default: [],
  70. type: [cc.String],
  71. },
  72. Businesslicense: {
  73. default: null,
  74. type: cc.SpriteFrame,
  75. },
  76. LandDevelopmentCertificate: {
  77. default: null,
  78. type: cc.SpriteFrame,
  79. },
  80. Golden10: {
  81. default: null,
  82. type: cc.SpriteFrame,
  83. },
  84. Golden50: {
  85. default: null,
  86. type: cc.SpriteFrame,
  87. },
  88. Golden100: {
  89. default: null,
  90. type: cc.SpriteFrame,
  91. },
  92. ManageUI: cc.Node,
  93. ManageMap: cc.Node,
  94. },
  95. // use this for initialization
  96. onLoad: function () {
  97. //消耗钻石数量
  98. this.Diamond = 100;
  99. this.spin = this.getNode("spin");
  100. this.Sprite = this.getNode("New Sprite", this.spin);
  101. this.wheelState = 0;
  102. this.curSpeed = 0;
  103. this.spinTime = 0; //减速前旋转时间
  104. this.gearNum = 8;
  105. this.defaultAngle = 360 / this.gearNum / 2; //修正默认角度
  106. this.gearAngle = 360 / this.gearNum; //每个齿轮的角度
  107. this.wheelSp.node.angle = -this.defaultAngle;
  108. // console.log("修正默认角度",this.defaultAngle);
  109. this.finalAngle = 0; //最终结果指定的角度
  110. this.effectFlag = 0; //用于音效播放
  111. this.restMaxSpeed = this.maxSpeed;
  112. this.duration = 1;
  113. if (!cc.sys.isBrowser) {
  114. cc.loader.loadRes('Sound/game_turntable', function (err, res) {
  115. if (err) {
  116. // cc.log('...err:' + err);
  117. }
  118. });
  119. }
  120. this.spinBtn.node.on(cc.Node.EventType.TOUCH_END, function (event) {
  121. // cc.log("begin spin");
  122. if (this.wheelState !== 0) {
  123. return;
  124. }
  125. // if (GlobalD.GameData.GetDiamond()>=this.Diamond) {
  126. // GlobalD.GameData.PlusDiamond(-this.Diamond);
  127. //
  128. // this.startLuck();
  129. //
  130. //
  131. // }else{
  132. // // cc.log("没"+this.Diamond+"钻石转不了");
  133. //
  134. // this._showDialog(function () {
  135. // this.watchvideo.on(cc.Node.EventType.TOUCH_END, function (event) {
  136. // this.startLuck();
  137. // this._closeDialog();
  138. // }.bind(this));
  139. // this.close.on(cc.Node.EventType.TOUCH_END, function (event) {
  140. // this._closeDialog();
  141. // }.bind(this));
  142. // }.bind(this));
  143. // }
  144. //每日签到
  145. let myDate = new Date();
  146. //'2019/05/01'
  147. let Today = myDate.toLocaleDateString();
  148. let LastTimeLuckDate = GlobalD.GameData.GetLastTimeLuckDate();
  149. // LastTimeLuckDate = "2019/3/20";
  150. // let s = "2019/3/20"
  151. // console.log("today", Today);
  152. // console.log("LastTimeLuckDate", LastTimeLuckDate);
  153. // GlobalD.GameData.SetLotteryTimes(10);
  154. if (Today == LastTimeLuckDate && LastTimeLuckDate != '0000/00/00') {
  155. }
  156. else {
  157. // console.log("走着么");
  158. //过了一天
  159. GlobalD.GameData.SetLotteryTimes(10);
  160. // GlobalD.GameData.SetLastTimeLuckDate(Today);
  161. GlobalD.GameData.SetLastTimeLuckDate(Today);
  162. }
  163. if (GlobalD.GameData.GetLotteryTimes() > 0) {
  164. // cc.log("没"+this.Diamond+"钻石转不了");
  165. this._showDialog(function () {
  166. this.watchvideo.on(cc.Node.EventType.TOUCH_END, function (event) {
  167. this.startLuck();
  168. this._closeDialog();
  169. }.bind(this));
  170. this.close.on(cc.Node.EventType.TOUCH_END, function (event) {
  171. this._closeDialog();
  172. }.bind(this));
  173. }.bind(this));
  174. // cc.log('11111111=='+GlobalD.GameData.GetLotteryTimes())
  175. GlobalD.GameData.PlusLotteryTimes(-1);
  176. }
  177. else {
  178. cc.loader.loadRes('resUI/ShowNotEnoughMoney', function (err, texture) {
  179. var prefab = cc.instantiate(texture);
  180. prefab.getComponent('ShowNotEnoughMoney').Text('明天再来试试手气吧!');
  181. this.node.addChild(prefab);
  182. }.bind(this));
  183. }
  184. }.bind(this));
  185. this.initNetwork();
  186. },
  187. initNetwork: function () {
  188. UtilsPrefabs.loadResSpriteFrame('taskres/diamond_1', function (texture) {
  189. }.bind(this));
  190. UtilsPrefabs.loadResSpriteFrame('taskres/diamond_2', function (texture) {
  191. }.bind(this));
  192. },
  193. startLuck: function () {
  194. this.getRandom();
  195. this.decAngle = 1 * 360; // 减速旋转两圈
  196. this.wheelState = 1;
  197. this.curSpeed = 0;
  198. this.spinTime = 0;
  199. this.maxSpeed = this.restMaxSpeed;
  200. },
  201. caculateFinalAngle: function (targetID) {
  202. this.finalAngle = 360 - this.targetID * this.gearAngle + this.defaultAngle;
  203. if (this.springback) {
  204. this.finalAngle += this.gearAngle;
  205. }
  206. },
  207. editBoxDidBegin: function (edit) {
  208. },
  209. editBoxDidChanged: function (text) {
  210. },
  211. editBoxDidEndEditing: function (edit) {
  212. var res = parseInt(edit.string);
  213. if (isNaN(res)) {
  214. if (cc.sys.isBrowser) {
  215. // alert('please input a number!');
  216. } else {
  217. // cc.log(".....invalid input");
  218. }
  219. this.targetID = Math.round(Math.random() * (this.gearNum - 1));
  220. // cc.log("停止的位置是", this.targetID);
  221. return;
  222. }
  223. this.targetID = res;
  224. },
  225. getRandom: function () {
  226. let TerritoryStateArray = GlobalD.GameData.GetTerritoryStateArray();
  227. var BuildingStateArray = GlobalD.GameData.BuildingStateArray;
  228. var tempcout = 0;
  229. var tempcout7 = 0;
  230. var probability = 0.1;
  231. var probability7 = 0.1;
  232. var probability100 = 0.1;
  233. var probability10 = 0.15;
  234. for (let i = 0; i < TerritoryStateArray.length; i++) {
  235. // console.log("结构遍历",TerritoryStateArray[i]);
  236. if (TerritoryStateArray[i] == 1) {
  237. tempcout++;
  238. }
  239. }
  240. for (let i = 0; i < BuildingStateArray.length; i++) {
  241. // console.log("结构遍历",BuildingStateArray[i]);
  242. if (BuildingStateArray[i] == 1) {
  243. tempcout7++;
  244. }
  245. }
  246. if (tempcout == TerritoryStateArray.length) {
  247. //没有能开的了
  248. probability = 0;
  249. probability100 = 0.1;
  250. probability10 = 0.2;
  251. }
  252. if (tempcout7 == BuildingStateArray.length) {
  253. //没有能开的了
  254. probability7 = 0;
  255. probability100 = 0.1;
  256. probability10 = 0.2;
  257. }
  258. // console.log("设置概率",tempcout,probability);
  259. // 随机数范围 :40~900
  260. let random = new GLRandom(0, this.gearNum);
  261. // 0 金币 x10
  262. // 1 随机建筑 x1
  263. // 2 金币 x50
  264. // 3 土地开发证件 x1
  265. // 4 金币 x10
  266. // 5 金币 x100
  267. // 6 金币 x10
  268. // 7 经营许可证 x1
  269. // 调整概率
  270. random.percentage = new Map([
  271. [0, 0.2], // 调整值为41的数值,概率为20%
  272. [1, 0.05], // 调整值为41的数值,概率为20%
  273. [2, 0.1], // 调整值为41的数值,概率为20%
  274. [3, 0.1], // 调整值为41的数值,概率为20%
  275. [4, 0.2], // 调整值为41的数值,概率为20%
  276. [5, 0.1], // 调整值为41的数值,概率为20%
  277. [6, 0.2], // 调整值为41的数值,概率为20%
  278. [7, 0.05], // 调整值为46的数值,概率为50%
  279. ]);
  280. // 生成值区间
  281. random.range();
  282. this.targetID = random.create();
  283. // 生成概率随机数
  284. // console.log("停止的位置是区间随机数", this.targetID);
  285. // this.targetID = Math.round(Math.random() * (this.gearNum - 1));
  286. // cc.log("停止的位置是", this.targetID);
  287. },
  288. // called every frame, uncomment this function to activate update callback
  289. update: function (dt) {
  290. if (this.wheelState === 0) {
  291. return;
  292. }
  293. // cc.log('......update');
  294. // cc.log('......state=%d',this.wheelState);
  295. // 播放音效有可能卡
  296. // this.effectFlag += this.curSpeed;
  297. // if (!cc.sys.isBrowser && this.effectFlag >= this.gearAngle) {
  298. // if (this.audioID) {
  299. // // cc.audioEngine.pauseEffect(this.audioID);
  300. // }
  301. // // this.audioID = cc.audioEngine.playEffect(this.effectAudio,false);
  302. // this.audioID = cc.audioEngine.playEffect(cc.url.raw('resources/Sound/game_turntable.mp3'));
  303. // this.effectFlag = 0;
  304. // }
  305. if (this.wheelState == 1) {
  306. // cc.log('....加速,speed:' + this.curSpeed);
  307. this.spinTime += dt;
  308. this.wheelSp.node.angle = Math.abs(this.wheelSp.node.angle) + this.curSpeed;
  309. if (this.curSpeed <= this.maxSpeed) {
  310. this.curSpeed += this.acc;
  311. } else {
  312. if (this.spinTime < this.duration) {
  313. return;
  314. }
  315. // cc.log('....开始减速');
  316. //设置目标角度
  317. this.finalAngle = 360 - this.targetID * this.gearAngle - this.defaultAngle;
  318. this.maxSpeed = this.curSpeed;
  319. if (this.springback) {
  320. this.finalAngle += this.gearAngle;
  321. }
  322. this.wheelSp.node.angle = this.finalAngle;
  323. this.wheelState = 2;
  324. }
  325. } else if (this.wheelState == 2) {
  326. // cc.log('......减速');
  327. var curRo = Math.abs(this.wheelSp.node.angle); //应该等于finalAngle
  328. var hadRo = curRo - this.finalAngle;
  329. this.curSpeed = this.maxSpeed * ((this.decAngle - hadRo) / this.decAngle) + 0.1;
  330. this.wheelSp.node.angle = curRo + this.curSpeed;
  331. if ((this.decAngle - hadRo) <= 0) {
  332. // cc.log('....停止');
  333. this.wheelState = 0;
  334. this.wheelSp.node.angle = this.finalAngle;
  335. if (this.springback) {
  336. //倒转一个齿轮
  337. // var act = new cc.rotateBy(0.6, -this.gearAngle);
  338. var act = cc.rotateBy(0.6, -this.gearAngle);
  339. var seq = cc.sequence(cc.delayTime(0.2), act, cc.callFunc(this.showRes, this));
  340. this.wheelSp.node.runAction(seq);
  341. } else {
  342. this.showRes();
  343. }
  344. }
  345. }
  346. },
  347. showRes: function () {
  348. // console.log("随机转盘是",this.targetID);
  349. // this.targetID = 1;
  350. // this.targetID=7;
  351. this.Rewards.removeAllChildren();
  352. // 0 金币 x10
  353. // 1 随机建筑 x1
  354. // 2 金币 x50
  355. // 3 土地开发证件 x1
  356. // 4 金币 x10
  357. // 5 金币 x100
  358. // 6 金币 x10
  359. // 7 经营许可证 x1
  360. switch (this.targetID) {
  361. case 0:
  362. case 4:
  363. case 6:
  364. GlobalD.GameData.PlusGolden(10000);
  365. this.Rewards.getComponent(cc.Sprite).spriteFrame = this.Golden10;
  366. this.Rewards.scale = 2;
  367. break;
  368. case 1:
  369. this.myScale = 0.2;
  370. // [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0];
  371. this.myScales = [
  372. 0.2,
  373. 0.2,
  374. 0.2,
  375. //3
  376. 1,
  377. 1,
  378. 1,
  379. 1,
  380. //7
  381. 1,
  382. 1,
  383. 1,
  384. 1,
  385. //11
  386. 1,
  387. 1,
  388. 1,
  389. 1,
  390. //15
  391. 1,
  392. 1,
  393. 1,
  394. 1.5,
  395. //19
  396. 1.5,
  397. 1,
  398. 1,
  399. 1
  400. ];
  401. let BuildingLockStateArray = GlobalD.GameData.BuildingLockStateArray;
  402. let HadBuildingArray = [];
  403. for (let i = 3; i < BuildingLockStateArray.length; i++) {
  404. if (parseInt(BuildingLockStateArray[i]) == 1) {
  405. HadBuildingArray.push(i);
  406. }
  407. }
  408. let RandomIndex = this.GetRandomRange(0, HadBuildingArray.length - 1);
  409. let BuildingNumArray = GlobalD.GameData.BuildingNumArray;
  410. let num = parseInt(BuildingNumArray[HadBuildingArray[RandomIndex]]) + 1;
  411. BuildingNumArray[HadBuildingArray[RandomIndex]] = num;
  412. GlobalD.GameData.SetBuildingNumArray(BuildingNumArray);
  413. let SelectedBuildingIndex = HadBuildingArray[RandomIndex];
  414. let SelectedBuilding = this.ManageUI.getComponent('ManageBuildings').BuildingArray[SelectedBuildingIndex];
  415. SelectedBuilding.getChildByName('Name').getChildByName('Num').getComponent(cc.Label).string = num;
  416. this.Rewards.getComponent(cc.Sprite).spriteFrame = "";
  417. if (SelectedBuildingIndex == 6) {
  418. var node = new cc.Node();
  419. var sprite = node.addComponent(cc.Sprite);
  420. sprite.spriteFrame = SelectedBuilding.getChildByName('New Sprite(Splash)').getComponent(cc.Sprite).spriteFrame;
  421. this.Rewards.addChild(node);
  422. var cnode = new cc.Node();
  423. var csprite = cnode.addComponent(cc.Sprite);
  424. cnode.y = 20;
  425. csprite.spriteFrame = SelectedBuilding.getChildByName('New Sprite(Splash)').getComponent(cc.Sprite).spriteFrame;
  426. node.addChild(cnode);
  427. // console.log("现在是什么1", SelectedBuildingIndex);
  428. } else {
  429. this.Rewards.getComponent(cc.Sprite).spriteFrame = SelectedBuilding.getChildByName('New Sprite(Splash)').getComponent(cc.Sprite).spriteFrame;
  430. // console.log("现在是什么2", SelectedBuildingIndex);
  431. }
  432. // SelectedBuildingIndex
  433. // console.log('this.myScales[SelectedBuildingIndex]=',this.myScales[SelectedBuildingIndex]);
  434. if (this.myScales[SelectedBuildingIndex])
  435. this.Rewards.scale = this.myScales[SelectedBuildingIndex];
  436. else
  437. this.Rewards.scale = 1;
  438. break;
  439. case 2:
  440. GlobalD.GameData.PlusGolden(15000);
  441. this.Rewards.getComponent(cc.Sprite).spriteFrame = this.Golden50;
  442. this.Rewards.scale = 2;
  443. break;
  444. case 3:
  445. case 7:
  446. // let TerritoryStateArray = GlobalD.GameData.GetTerritoryStateArray();
  447. // for(let i=0;i<TerritoryStateArray.length;i++)
  448. // {
  449. // // console.log("结构遍历",TerritoryStateArray[i]);
  450. // if(TerritoryStateArray[i]==0)
  451. // {
  452. // TerritoryStateArray[i]=1;
  453. //
  454. // GlobalD.GameData.SetTerritoryStateArray(TerritoryStateArray);
  455. //
  456. // let _ManageMap = this.ManageMap.getComponent('ManageMap');
  457. // let Territory = _ManageMap.Territories[i];
  458. // //开土地后清空土地的占位信息
  459. // _ManageMap.onClearSolidArea(i);
  460. //
  461. //
  462. // // let Territory = this.ManageMap.getComponent('ManageMap').Territories[i];
  463. // Territory.enabled = false;
  464. // this.Rewards.getComponent(cc.Sprite).spriteFrame = this.LandDevelopmentCertificate;
  465. // // console.log("结构",this.Rewards);
  466. // this.Rewards.scale=2;
  467. // break;
  468. // }
  469. // }
  470. if (this.targetID == 3) {
  471. GlobalD.GameData.PlusDiamond(250);
  472. UtilsPrefabs.loadResSpriteFrame('taskres/diamond_1', function (texture) {
  473. // var prefab = cc.instantiate(texture);
  474. this.Rewards.getComponent(cc.Sprite).spriteFrame = texture;
  475. this.Rewards.scale = 2;
  476. }.bind(this));
  477. } else if (this.targetID == 7) {
  478. GlobalD.GameData.PlusDiamond(350);
  479. UtilsPrefabs.loadResSpriteFrame('taskres/diamond_2', function (texture) {
  480. // var prefab = cc.instantiate(texture);
  481. this.Rewards.getComponent(cc.Sprite).spriteFrame = texture;
  482. this.Rewards.scale = 2;
  483. }.bind(this));
  484. }
  485. break;
  486. case 5:
  487. GlobalD.GameData.PlusGolden(30000);
  488. this.Rewards.getComponent(cc.Sprite).spriteFrame = this.Golden100;
  489. this.Rewards.scale = 2;
  490. break;
  491. // case 7:
  492. //
  493. // var BuildingStateArray = GlobalD.GameData.BuildingStateArray;
  494. //
  495. // for(let i=3;i<BuildingStateArray.length;i++)
  496. // {
  497. // cc.log('BuildingStateArray[i]='+BuildingStateArray[i]);
  498. // if(BuildingStateArray[i]==0)
  499. // {
  500. // BuildingStateArray[i]=1;
  501. // GlobalD.GameData.SetBuildingStateArray(BuildingStateArray);
  502. //
  503. // let Building = this.ManageUI.getComponent('ManageBuildings').BuildingArray[i];
  504. // Building.active = true;
  505. // this.Rewards.getComponent(cc.Sprite).spriteFrame = Building.getChildByName('New Sprite(Splash)').getComponent(cc.Sprite).spriteFrame;
  506. // this.Rewards.scale=1;
  507. //
  508. // break;
  509. // }
  510. // }
  511. //
  512. // break;
  513. default:
  514. break;
  515. }
  516. // console.log("结果是",this.RewardDetail[this.targetID]);
  517. this.showDialog();
  518. },
  519. isTask: function () {
  520. if (task.TaskIconCountClick == 5) {
  521. task.addTaskIconCountClick();
  522. task.removeTaskNodes();
  523. }
  524. },
  525. _showShareDialog: function (callback) {
  526. cc.loader.loadRes("prefab/share", function (err, texture) {
  527. this.shares = cc.instantiate(texture);
  528. this._closeShareDialog();
  529. this.node.parent.addChild(this.shares);
  530. this.gosharebtn = this.getNode("gosharebtn", this.shares);
  531. // this.close = this.getNode("close",this.dialogLuckView);
  532. this.gosharebtn.off(cc.Node.EventType.TOUCH_END);
  533. var img1 = UtilsPrefabs.getNode("img1", this.shares);
  534. var NewLabel1 = UtilsPrefabs.getNode("New Label", img1);
  535. var img2 = UtilsPrefabs.getNode("img2", this.shares);
  536. var NewLabel2 = UtilsPrefabs.getNode("New Label", img2);
  537. NewLabel1.getComponent(cc.Label).string = 'x' + GlobalD.GameData.shareGive[0];
  538. NewLabel2.getComponent(cc.Label).string = 'x' + GlobalD.GameData.shareGive[1];
  539. if (callback != null) {
  540. callback();
  541. }
  542. }.bind(this));
  543. },
  544. showDialog: function () {
  545. this.node.active = false;
  546. var Sunshine = this.ShowRewards.getChildByName('GivePrize').getChildByName('Sunshine');
  547. var GivePrize = this.ShowRewards.getChildByName('GivePrize');
  548. Sunshine.scaleX = 0;
  549. Sunshine.scaleY = 0;
  550. GivePrize.scaleX = 0;
  551. GivePrize.scaleY = 0;
  552. Sunshine.stopAllActions();
  553. GivePrize.stopAllActions();
  554. this.scheduleOnce(function () {
  555. this.ShowRewards.opacity = 255;
  556. this.ShowRewards.active = true;
  557. var sc2 = cc.scaleTo(0.5, 1.2, 1.2);
  558. Sunshine.runAction(sc2);
  559. var sc3 = cc.scaleTo(0.5, 1, 1);
  560. GivePrize.runAction(sc3);
  561. var repeat = cc.repeatForever(cc.rotateBy(5.0, 360));
  562. Sunshine.runAction(repeat);
  563. }, 0.1);
  564. },
  565. GetRandomRange(n, m) {
  566. var random = Math.floor(Math.random() * (m - n + 1) + n);
  567. return random;
  568. },
  569. _showDialog: function (callback) {
  570. cc.loader.loadRes("prefab/dialogLuckView", function (err, texture) {
  571. this.dialogLuckView = cc.instantiate(texture);
  572. if (this.node.getChildByName(this.dialogLuckView.name) != null) {
  573. this.node.getChildByName(this.dialogLuckView.name).destroy();
  574. }
  575. this.node.addChild(this.dialogLuckView);
  576. // this.dialogLuckView.active = false;
  577. this.dialogLuckView.y = -100;
  578. this.watchvideo = this.getNode("watchvideo", this.dialogLuckView);
  579. this.close = this.getNode("close", this.dialogLuckView);
  580. if (callback != null) {
  581. callback();
  582. }
  583. }.bind(this));
  584. },
  585. _closeDialog: function () {
  586. if (this.node.getChildByName(this.dialogLuckView.name) != null) {
  587. this.node.getChildByName(this.dialogLuckView.name).destroy();
  588. }
  589. },
  590. _closeShareDialog: function () {
  591. if (this.node.parent.getChildByName(this.shares.name) != null) {
  592. this.node.parent.getChildByName(this.shares.name).destroy();
  593. }
  594. },
  595. getNode: function (name, parent) {
  596. if (parent == null) {
  597. return this.node.getChildByName(name);
  598. } else {
  599. return parent.getChildByName(name);
  600. }
  601. },
  602. });
  603. class GLRandom {
  604. /**
  605. * 构造函数
  606. * @param {number} min 最小整数值
  607. * @param {number} max 最大整数值
  608. * @param {Map} percentage 概率数 [值,百分比]
  609. */
  610. constructor(min, max, percentage = new Map()) {
  611. this.min = Math.trunc(min);
  612. this.max = Math.trunc(max);
  613. this.MATH_RANGE = 100; // 分成100份
  614. this.percentage = percentage;
  615. }
  616. get percentage() {
  617. if (!this._percentage) {
  618. this._percentage = new Map();
  619. }
  620. return this._percentage;
  621. }
  622. /**
  623. * 分配比例
  624. * @param {Map} map 设置 值-百分比
  625. */
  626. set percentage(map) {
  627. let result = Array.from(map.values()).reduce((p, v, a) => {
  628. return p - v;
  629. }, 1);
  630. for (let i = this.min; i < this.max; i++) {
  631. if (map.has(i)) {
  632. this.percentage.set(i, map.get(i));
  633. } else {
  634. this.percentage.set(i, result / (this.max - this.min - map.size));
  635. }
  636. }
  637. }
  638. /**
  639. * 根据比例生成取值范围
  640. */
  641. range() {
  642. let [start, random, keys] = [0, this.MATH_RANGE, Array.from(this.percentage.keys())];
  643. for (let i = 0; i < keys.length; i++) {
  644. let temp = this.percentage.get(keys[i]);
  645. this.percentage.set(keys[i], [start, start += temp * random]);
  646. }
  647. }
  648. /**
  649. * 生成随机数
  650. */
  651. create() {
  652. let num = Math.random() * this.MATH_RANGE;
  653. for (let data of this.percentage.entries()) {
  654. if (num >= data[1][0] && num < data[1][1]) {
  655. return data[0];
  656. }
  657. }
  658. return null;
  659. }
  660. }