UtilsNode.js 16 KB

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