UtilsNode.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. window.UtilsNode = {
  2. PrefabsName: [
  3. "prefab/share",
  4. "prefab/show",
  5. "prefab/login",
  6. "prefab/loading",
  7. "prefab/stock",
  8. ],
  9. sharePrefab: [
  10. "prefab/share",
  11. "prefab/sharepublic",
  12. "prefab/MoneyFlyAfterShare",
  13. ],
  14. put: function (e, v) {
  15. if (window.localStorage) {
  16. window.localStorage[e] = v;
  17. }
  18. },
  19. get: function (e) {
  20. if (window.localStorage) {
  21. let re = "";
  22. if (window.localStorage[e] == null) {
  23. return re;
  24. } else {
  25. return window.localStorage[e];
  26. }
  27. }
  28. },
  29. //产生演员
  30. startActor: function (bgArr) {
  31. let defXx = 100;
  32. let defX = defXx;
  33. let defY = 300;
  34. let defaddX = 180;
  35. let defaddY = 180;
  36. let itemXCount = 4;
  37. let itemXCount2 = 1;
  38. //总演员个数
  39. let itemMaxCount = 101;
  40. //总个数 是 itemMaxCount + itemYCount 想要的个数+多少行
  41. let itemYCount = itemMaxCount / itemXCount;
  42. let postions = [];
  43. let tempitemXCount = itemXCount;
  44. let tempxarr = [];
  45. let bgArrs = [];
  46. for (let i = 0; i < itemYCount; i++) {
  47. // if (i%2 == 0) {
  48. // tempitemXCount = itemXCount;
  49. // }else{
  50. // tempitemXCount = itemXCount2;
  51. // }
  52. if (tempxarr.length == 0) {
  53. for (let x = 0; x < tempitemXCount; x++) {
  54. if (x == 0) {
  55. defX = defXx
  56. } else {
  57. defX += defaddX;
  58. }
  59. tempxarr.push(defX);
  60. let p = {x: tempxarr[x], y: defY};
  61. postions.push(p);
  62. }
  63. let p = {x: tempxarr[tempxarr.length - 1], y: defY += defaddY, q: 0};
  64. postions.push(p);
  65. bgArrs.push(p);
  66. } else if (tempxarr.length > 0 && i % 2 != 0) {
  67. if (postions.length > itemMaxCount) {
  68. break;
  69. }
  70. defY += defaddY;
  71. // 奇数
  72. for (let z = tempxarr.length - 1; z >= 0; z--) {
  73. let p = {x: tempxarr[z], y: defY};
  74. postions.push(p);
  75. if (postions.length > itemMaxCount) {
  76. break;
  77. }
  78. }
  79. if (postions.length > itemMaxCount) {
  80. break;
  81. }
  82. let p = {x: tempxarr[0], y: defY += defaddY, w: 0};
  83. postions.push(p);
  84. bgArrs.push(p);
  85. } else if (tempxarr.length > 0 && i % 2 == 0) {
  86. if (postions.length > itemMaxCount) {
  87. break;
  88. }
  89. defY += defaddY;
  90. //偶数
  91. for (let z = 0; z < tempitemXCount; z++) {
  92. let p = {x: tempxarr[z], y: defY};
  93. postions.push(p);
  94. if (postions.length > itemMaxCount) {
  95. break;
  96. }
  97. }
  98. if (postions.length > itemMaxCount) {
  99. break;
  100. }
  101. let p = {x: tempxarr[tempxarr.length - 1], y: defY += defaddY, q: 0};
  102. postions.push(p);
  103. bgArrs.push(p);
  104. }
  105. }
  106. // for (let x = 0; x < tempitemXCount; x++) {
  107. // if (x==0) {
  108. // defX = defXx
  109. // }else{
  110. // defX+=defaddX;
  111. // }
  112. //
  113. //
  114. // let p = {x:defX,y:defY};
  115. // postions.push(p);
  116. // }
  117. if (bgArr != null) {
  118. bgArr(bgArrs, postions);
  119. }
  120. return postions;
  121. },
  122. // ①、// 生成 [n,m),包含n但不包含m的正整数:  parseInt(Math.random()*(m-n)+n)
  123. //
  124. // ②、// 生成 (n,m],不包含n但包含m的正整数:  parseInt(Math.random()*(m-n)+n)+1
  125. //
  126. // ③、// 生成 [n,m],包含n和m的随机数:             parseInt(Math.random()*(m-n+1)+n)
  127. //
  128. // ④、// 生成 (n,m),不包含n和m的正整数:         parseInt(Math.random()*(m-n-1)+n+1)
  129. getRandom: function (type, n, m, arr) {
  130. let random;
  131. switch (type) {
  132. case 0:
  133. random = parseInt(Math.random() * (m - n) + n);
  134. break;
  135. case 1:
  136. random = parseInt(Math.random() * (m - n) + n) + 1;
  137. break;
  138. case 2:
  139. random = parseInt(Math.random() * (m - n + 1) + n);
  140. break;
  141. case 3:
  142. random = parseInt(Math.random() * (m - n - 1) + n + 1);
  143. break;
  144. case 4:
  145. random = parseInt((Math.random() * arr.length));
  146. break;
  147. }
  148. return random;
  149. },
  150. // this.node.setScale(1,-1);
  151. // this.node.setRotation(180);
  152. // console.log("我应该脸往左",temp,temp1);
  153. // this.node.setScale(1,1);
  154. // this.node.setRotation(0);
  155. // console.log("我应该脸往右",temp,temp1);
  156. setMyRotation: function (type, node) {
  157. switch (type) {
  158. case 0:
  159. node.setScale(1, -1);
  160. node.setRotation(180);
  161. // console.log("我应该脸往左",temp,temp1);
  162. break;
  163. case 1:
  164. node.setScale(1, 1);
  165. node.setRotation(0);
  166. // console.log("我应该脸往右",temp,temp1);
  167. break;
  168. }
  169. },
  170. //获取 百分比
  171. getPercentage: function (n, m) {
  172. let num = parseInt(n);
  173. let max = parseInt(m);
  174. if (isNaN(num) || isNaN(max)) {
  175. return 0;
  176. }
  177. return max <= 0 ? 0 : parseInt((Math.round(num / max * 10000) / 100.00));
  178. },
  179. //随机生成演员
  180. actorBirth: function (parentNode, arrp, actorType, startActor, endActor) {
  181. let nodearr = [];
  182. for (var i = 0; i < arrp.length; i++) {
  183. let mInt = parseInt(Math.random() * (actorType.length - 0));
  184. let tag = true;
  185. if (i < 9) {
  186. switch (i) {
  187. case 1:
  188. mInt = 0;
  189. break;
  190. case 2:
  191. mInt = 1;
  192. break;
  193. case 3:
  194. mInt = 2;
  195. break;
  196. case 4:
  197. mInt = 0;
  198. break;
  199. case 5:
  200. mInt = 1;
  201. break;
  202. case 6:
  203. mInt = 0;
  204. break;
  205. case 7:
  206. mInt = 2;
  207. break;
  208. case 8:
  209. mInt = 1;
  210. break;
  211. }
  212. // mInt = 1;
  213. } else {
  214. while (tag) {
  215. //如果当前地板是陷阱 检查上一个是不是陷阱如果是 就再随机一个
  216. if (i > 0 && actorType[mInt].data.getComponent("actor").type == 2) {
  217. if (nodearr[i - 1].getComponent("actor").type == 2) {
  218. mInt = parseInt(Math.random() * (actorType.length - 0));
  219. } else {
  220. tag = false;
  221. }
  222. // 大于三的时候 如果 没有陷阱 我就创建一个陷阱
  223. } else if (i > 3 && (actorType[mInt].data.getComponent("actor").type == 1 || actorType[mInt].data.getComponent("actor").type == 0)) {
  224. let tempi = 0;
  225. for (var x = 1; x < 4; x++) {
  226. if (nodearr[i - x].getComponent("actor").type != 2) {
  227. tempi++;
  228. }
  229. }
  230. if (tempi == 3) {
  231. mInt = parseInt(Math.random() * (actorType.length - 0));
  232. } else {
  233. tag = false;
  234. }
  235. } else {
  236. tag = false;
  237. }
  238. }
  239. }
  240. let node = cc.instantiate(actorType[mInt]);
  241. if (i == 0) {
  242. node = cc.instantiate(startActor);
  243. } else if (i == arrp.length - 1) {
  244. node = cc.instantiate(endActor);
  245. }
  246. parentNode.addChild(node);
  247. node.setPosition(arrp[i].x, arrp[i].y);
  248. nodearr.push(node);
  249. }
  250. return nodearr;
  251. },
  252. init: function (parentNode) {
  253. this.parentNode = parentNode;
  254. return this;
  255. },
  256. getPrefabsName: function () {
  257. return this.prefabsName;
  258. },
  259. addPrefabs: function (prefabsName, parentNode, callback) {
  260. cc.loader.loadRes(prefabsName, function (err, texture) {
  261. var node = this.parentNode;
  262. if (parentNode != null) {
  263. node = parentNode;
  264. }
  265. var prefab = cc.instantiate(texture);
  266. // this.prefabsName = prefab.name;
  267. this.removePrefabs(node, prefab.name);
  268. if (node != null) {
  269. node.addChild(prefab);
  270. } else {
  271. return;
  272. }
  273. // this.dialogLuckView.active = false;
  274. // this.GivePrize = this.getNode("GivePrize",this.showDialogs);
  275. // //转
  276. // this.Sunshine = this.getNode("Sunshine",this.GivePrize);
  277. // //物品
  278. // this.Rewards = this.getNode("Rewards",this.GivePrize);
  279. // //关闭按钮
  280. // this.close = this.getNode("close",this.showDialogs);
  281. if (callback != null) {
  282. callback(prefab);
  283. }
  284. }.bind(this));
  285. },
  286. addPrefabsName: function (prefabsName, parentNode, name, callback) {
  287. cc.loader.loadRes(prefabsName, function (err, texture) {
  288. var node = this.parentNode;
  289. if (parentNode != null) {
  290. node = parentNode;
  291. }
  292. var prefab = cc.instantiate(texture);
  293. let prefabsName = "";
  294. if (name != null) {
  295. prefabsName = name;
  296. } else {
  297. prefabsName = prefab.name;
  298. }
  299. // this.prefabsName = prefab.name;
  300. // this.removePrefabs(node,prefabsName);
  301. if (node != null) {
  302. node.addChild(prefab);
  303. } else {
  304. return;
  305. }
  306. // this.dialogLuckView.active = false;
  307. // this.GivePrize = this.getNode("GivePrize",this.showDialogs);
  308. // //转
  309. // this.Sunshine = this.getNode("Sunshine",this.GivePrize);
  310. // //物品
  311. // this.Rewards = this.getNode("Rewards",this.GivePrize);
  312. // //关闭按钮
  313. // this.close = this.getNode("close",this.showDialogs);
  314. if (callback != null) {
  315. callback(prefab);
  316. }
  317. }.bind(this));
  318. },
  319. removePrefabs: function (parentNode, prefabsName, success, fail) {
  320. var node = this.parentNode;
  321. if (parentNode != null) {
  322. node = parentNode;
  323. }
  324. if (node != null) {
  325. if (node.getChildByName(prefabsName) != null) {
  326. node.getChildByName(prefabsName).off(cc.Node.EventType.TOUCH_END);
  327. node.getChildByName(prefabsName).destroy();
  328. if (success != null) {
  329. success();
  330. }
  331. } else {
  332. if (fail != null) {
  333. fail();
  334. }
  335. }
  336. }
  337. },
  338. setOff: function (node) {
  339. node.off(cc.Node.EventType.TOUCH_END);
  340. },
  341. setOn: function (node, callback) {
  342. if (node != null) {
  343. node.on(cc.Node.EventType.TOUCH_END, callback);
  344. } else {
  345. console.log("当前被放进来的node节点没有可以点击的名字 请关注此段警告");
  346. }
  347. return this;
  348. },
  349. getNode: function (name, parent) {
  350. if (parent == null) {
  351. if (this.parentNode.getChildByName(name) == null) {
  352. // console.log("在",this.parentNode,"没有找到名字为"+name+"的node");
  353. }
  354. return this.parentNode.getChildByName(name);
  355. } else {
  356. if (parent.getChildByName(name) == null) {
  357. // console.log("在",parent,"没有找到名字为"+name+"的node");
  358. }
  359. return parent.getChildByName(name);
  360. }
  361. },
  362. //开始分享
  363. /**
  364. UtilsPrefabs.startSharePrefab(function () {
  365. this.Building_t();
  366. }.bind(this),function () {
  367. this.Building_t();
  368. }.bind(this));
  369. * @param Success
  370. * @param fail
  371. */
  372. startSharePrefab: function (Success, fail, callbackNode) {
  373. var Canvas = cc.find("Canvas/UICamera");
  374. UtilsPrefabs
  375. .init(Canvas)
  376. .addPrefabs(UtilsPrefabs.sharePrefab[1], null, function (node) {
  377. if (callbackNode != null) {
  378. callbackNode(node);
  379. }
  380. // node.y = -200;
  381. // node.x = -20;
  382. var gosharebtn = UtilsPrefabs.getNode("gosharebtn", node);
  383. var img1 = UtilsPrefabs.getNode("img1", node);
  384. var NewLabel1 = UtilsPrefabs.getNode("New Label", img1);
  385. var img2 = UtilsPrefabs.getNode("img2", node);
  386. var NewLabel2 = UtilsPrefabs.getNode("New Label", img2);
  387. // NewLabel1.getComponent(cc.Label).string = 'x'+GlobalD.GameData.publicGive[0];
  388. // NewLabel2.getComponent(cc.Label).string = 'x'+GlobalD.GameData.publicGive[1];
  389. var close = UtilsPrefabs.getNode("close", node);
  390. UtilsPrefabs.setOn(close, function () {
  391. UtilsPrefabs.removePrefabs(Canvas, node.name);
  392. // if (task.taskCursor == 4) {
  393. // task.addTaskCount();
  394. // task.removeTaskNodes();
  395. // }
  396. if (fail != null) {
  397. fail();
  398. }
  399. }.bind(this));
  400. UtilsPrefabs.setOn(gosharebtn, function () {
  401. // console.log("点击分享按钮");
  402. UtilsWX.sharebtn(function () {
  403. UtilsPrefabs.setOff(gosharebtn);
  404. GlobalD.GameData.PlusGolden(GlobalD.GameData.publicGive[0]);
  405. GlobalD.GameData.PlusDiamond(GlobalD.GameData.publicGive[1]);
  406. UtilsPrefabs
  407. .init(Canvas)
  408. .addPrefabs(UtilsPrefabs.sharePrefab[2], null, function (node) {
  409. // cc.log(node.name+"***********000");
  410. node.zIndex = 10000;
  411. var MoneyFlyAnim = node.getChildByName('MoneyAnim').getComponent(cc.Animation);
  412. MoneyFlyAnim.RemoveNode = function () {
  413. node.destroy();
  414. // cc.log('111111111',node);
  415. }
  416. });
  417. if (Success != null) {
  418. Success();
  419. }
  420. }.bind(this));
  421. UtilsPrefabs.removePrefabs(Canvas, node.name);
  422. // if (task.taskCursor == 4) {
  423. // task.addTaskCount();
  424. // task.removeTaskNodes();
  425. // }
  426. }.bind(this))
  427. }.bind(this));
  428. },
  429. loadResSpriteFrame: function (src, callback) {
  430. cc.loader.loadRes(src, cc.SpriteFrame, function (err, texture) {
  431. if (callback != null) {
  432. callback(texture);
  433. }
  434. }.bind(this));
  435. },
  436. }