GameUI.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. cc.Class({
  2. extends: cc.Component,
  3. properties: {
  4. //中间加分条
  5. UIPKScore: {
  6. default: null,
  7. type: cc.Prefab
  8. },
  9. //胜利
  10. UIWin: {
  11. default: null,
  12. type: cc.Prefab
  13. },
  14. //失败
  15. UILose: {
  16. default: null,
  17. type: cc.Prefab
  18. },
  19. //撒花
  20. Fireworks: {
  21. default: null,
  22. type: cc.Prefab
  23. },
  24. //完美
  25. UIPerfect: {
  26. default: null,
  27. type: cc.Prefab
  28. },
  29. //左面距离头像
  30. LeftRivalHead: {
  31. default: null,
  32. type: cc.Prefab
  33. },
  34. //右面距离头像
  35. RightRivalHead: {
  36. default: null,
  37. type: cc.Prefab
  38. },
  39. //跳远 扔标枪 的计数板
  40. UIDistance: {
  41. default: null,
  42. type: cc.Prefab
  43. },
  44. //底部普通加速带提示
  45. TipOfNormalAcceleration: {
  46. default: null,
  47. type: cc.Prefab
  48. },
  49. //底部长加速带提示
  50. TipOfLongAcceleration: {
  51. default: null,
  52. type: cc.Prefab
  53. },
  54. ReadyGoPrefab: {
  55. default: null,
  56. type: cc.Prefab
  57. },
  58. RivelHeadImage: null,
  59. UITop: null,
  60. VideoStatesLeft: cc.Node,
  61. VideoStatesRight: cc.Node,
  62. MyHead: cc.Node,
  63. RiveelHead: cc.Node,
  64. },
  65. onLoad() {
  66. this.UITop = cc.find('Canvas/UITop');
  67. },
  68. // start() {
  69. // this.setTopMyScore("9");
  70. // this.setTopRivalScore("9");
  71. // this.hideTimerPerfect(3);
  72. // this.hideTimerMiddlePKScore("7","8",5);
  73. // this.hideTimerDistanceNum("121.00",8);
  74. // this.hideTimerTimeNum("8.20",2)
  75. // },
  76. setScoreNumImage: function (ScoreNum, numNode) {
  77. var imgName = "";
  78. if (ScoreNum <= 5) {
  79. imgName = "Nums/0to3White/" + ScoreNum;
  80. }
  81. var self = numNode;
  82. cc.loader.loadRes(imgName, cc.SpriteFrame, function (err, spriteFrame) {
  83. self.getComponent(cc.Sprite).spriteFrame = spriteFrame;
  84. });
  85. },
  86. setMiddleMyScore: function (aPrefab, myScore) {//设置自己的分数(图片中部)
  87. this.setScoreNumImage(myScore, aPrefab.getChildByName('MiddleMyScore'));
  88. },
  89. setMiddleRivalScore: function (aPrefab, rivalScore) {//设置对手的分数(图片中部)
  90. this.setScoreNumImage(rivalScore, aPrefab.getChildByName('MiddleRivalScore'));
  91. },
  92. setTopMyScore: function (ScoreNum) {//设置顶部自己分数图片string
  93. this.UITop.getChildByName('TopMyScore').getComponent('TopScore').setScoreNumImage(ScoreNum);
  94. },
  95. setTopRivalScore: function (ScoreNum) {//设置顶部对手PK分数图片string
  96. this.UITop.getChildByName('TopRivalScore').getComponent('TopScore').setScoreNumImage(ScoreNum);
  97. },
  98. setDistanceNum: function (distanceNum) {//成绩:米数string
  99. cc.find('UIDistance').getChildByName('DistanceNum').getComponent(cc.Label).string = distanceNum + "M";
  100. },
  101. setTimeNum: function (secondNum) {//成绩:秒数string
  102. cc.find('UIDistance').getChildByName('TimeNum').getComponent(cc.Label).string = secondNum + "s";
  103. },
  104. //以上是设置数值,以下是设置某UI显示某秒后消失的计时器
  105. setUIActiveTimer: function (actor, timerSecond) {//UI显示消失计时器
  106. actor.active = true;
  107. /**
  108. * timerSecond 表示此UI显示timerSecond秒后消失。当timerSecond为0的时候,此UI一直显示,不会消失。
  109. */
  110. if (timerSecond > 0) {
  111. this.scheduleOnce(function () {
  112. actor.active = false;
  113. }, timerSecond);
  114. }
  115. },
  116. setUIActiveTimer_destroy: function (actor, timerSecond) {//UI显示消失计时器
  117. /**
  118. * timerSecond 表示此UI显示timerSecond秒后消失。当timerSecond为0的时候,此UI一直显示,不会消失。
  119. */
  120. if (actor == null) {
  121. return;
  122. }
  123. if (timerSecond > 0) {
  124. this.scheduleOnce(function () {
  125. actor.destroy();
  126. }, timerSecond);
  127. }
  128. },
  129. setUIActiveShow: function setUIActiveTimer(actor) {
  130. //UI显示消失计时器
  131. actor.active = true;
  132. },
  133. displayTimerMiddlePKScore: function (myScore, rivalScore, timerSecond) {
  134. // console.log("my is "+myScore+" and rival is "+rivalScore);
  135. var aPrefab = cc.instantiate(this.UIPKScore);
  136. aPrefab.setOpacity(0);
  137. aPrefab.parent = cc.director.getScene();//中部PK积分显示后隐藏
  138. // console.log("This scene is "+cc.director.getScene().name);
  139. aPrefab.anchorX = 0;
  140. this.setMiddleMyScore(aPrefab, myScore);
  141. this.setMiddleRivalScore(aPrefab, rivalScore);
  142. this.setUITimer(aPrefab, timerSecond);
  143. aPrefab.setOpacity(255);
  144. // this.scheduleOnce(function() {
  145. // aPrefab.setOpacity(255);
  146. // }, 0.5);
  147. },
  148. hideMiddlePKScore: function () {
  149. var UIScore = cc.find('UIPKScore');
  150. if (UIScore) {
  151. var animState = UIScore.getComponent(cc.Animation).play('UIPKScoreFadeOut');
  152. var duration = animState.duration;
  153. this.scheduleOnce(function () {
  154. // console.log('UIScore=',UIScore.name);
  155. UIScore.destroy();
  156. }, duration);
  157. }
  158. },
  159. hideTimerDistanceNum: function (distanceNum, timerSecond) {//成绩米数显示后隐藏
  160. this.setDistanceNum(distanceNum);
  161. this.setUIActiveTimer(cc.find('UIDistance'), timerSecond)
  162. },
  163. hideTimerTimeNum: function (timeNum, timerSecond) {//成绩秒数显示后隐藏
  164. this.setDistanceNum(timeNum);
  165. this.setUIActiveTimer(cc.find('UIDistance'), timerSecond)
  166. },
  167. hideTimerPerfect: function (timerSecond) {//pererfect显示后隐藏
  168. if (cc.find('UIPerfect') != null) {
  169. cc.find('UIPerfect').destroy();
  170. }
  171. var aPrefab = cc.instantiate(this.UIPerfect);
  172. aPrefab.parent = cc.director.getScene();//中部PK积分显示后隐藏
  173. // aPrefab.anchorY = 0.5;
  174. this.setUITimer(aPrefab, timerSecond);
  175. // cc.find('UIPerfect').active = true;
  176. aPrefab.getChildByName('perfect').getComponent(cc.Animation).play('PerfectMoveUp');
  177. },
  178. displayRivalHead: function (isLeft) {//当对手与自己不在一个屏幕的时候,显示对手头像
  179. if (isLeft) {
  180. if (cc.find("RightRivalHead") != null) {
  181. cc.find('RightRivalHead').destroy();
  182. }
  183. if (cc.find("LeftRivalHead") == null) {
  184. // cc.find('LeftRivalHead').destroy();
  185. var aPrefab = cc.instantiate(this.LeftRivalHead);
  186. aPrefab.parent = cc.director.getScene();
  187. aPrefab.getChildByName('HeadMask').getChildByName('tx').getComponent(cc.Sprite).spriteFrame = this.RivelHeadImage;
  188. }
  189. } else {
  190. if (cc.find("LeftRivalHead") != null) {
  191. cc.find('LeftRivalHead').destroy();
  192. }
  193. if (cc.find("RightRivalHead") == null) {
  194. // cc.find('LeftRivalHead').destroy();
  195. var aPrefab = cc.instantiate(this.RightRivalHead);
  196. aPrefab.parent = cc.director.getScene();
  197. aPrefab.getChildByName('HeadMask').getChildByName('tx').getComponent(cc.Sprite).spriteFrame = this.RivelHeadImage;
  198. }
  199. }
  200. // cc.find("LeftRivalHead").active = isLeft;
  201. // cc.find("RightRivalHead").active = !isLeft;
  202. },
  203. hideRivalHead: function () {//当对手与自己在一个屏幕的时候,隐藏对手头像
  204. if (cc.find("LeftRivalHead") != null) {
  205. cc.find('LeftRivalHead').destroy();
  206. }
  207. if (cc.find("RightRivalHead") != null) {
  208. cc.find('RightRivalHead').destroy();
  209. }
  210. // cc.find("LeftRivalHead").active = false;
  211. // cc.find("RightRivalHead").active = false;
  212. },
  213. displayUIWin: function () {//显示胜利界面
  214. var aPrefab = cc.instantiate(this.UIWin);
  215. aPrefab.parent = cc.director.getScene();//中部PK积分显示后隐藏
  216. // cc.find('UIWin').active = true;
  217. },
  218. displayUILose: function () {//显示失败界面
  219. var aPrefab = cc.instantiate(this.UILose);
  220. aPrefab.parent = cc.director.getScene();//中
  221. // cc.find('UILose').active = true;
  222. },
  223. displayUIDistance: function () {//显示跳远 扔标枪
  224. var aPrefab = cc.instantiate(this.UIDistance);
  225. if (cc.find("UIDistance") != null) {
  226. return aPrefab
  227. }
  228. aPrefab.parent = cc.director.getScene();//中
  229. // cc.find('UILose').active = true;
  230. return aPrefab;
  231. },
  232. hideUIDistance: function () {//当对手与自己在一个屏幕的时候,隐藏对手头像
  233. if (cc.find("UIDistance") != null) {
  234. cc.find('UIDistance').destroy();
  235. }
  236. },
  237. playFireworks: function () {
  238. // cc.find('Fireworks').active = true;
  239. var aPrefab = cc.instantiate(this.Fireworks);
  240. aPrefab.parent = cc.director.getScene();//
  241. },
  242. hideTimerTipOfNormalAcceleration: function (timerSecond) {//开局时加速带提示
  243. var aPrefab = cc.instantiate(this.TipOfNormalAcceleration);
  244. aPrefab.parent = cc.director.getScene();
  245. aPrefab.anchorX = 0;
  246. aPrefab.x = 360;
  247. aPrefab.y = 640;
  248. this.setUITimer(aPrefab, timerSecond);
  249. },
  250. hideTimerTipOfLongAcceleration: function (timerSecond) {//长加速带提示
  251. var aPrefab = cc.instantiate(this.TipOfLongAcceleration);
  252. aPrefab.parent = cc.director.getScene();
  253. aPrefab.anchorX = 0;
  254. aPrefab.x = 360;
  255. aPrefab.y = 640;
  256. this.setUITimer(aPrefab, timerSecond);
  257. },
  258. setImage_Player1: function (src) {
  259. cc.loader.load({ url: src, type: 'jpg' }, function (err, texture) {
  260. var frame = new cc.SpriteFrame(texture);
  261. if (err) {
  262. console.log('玩家1的头像:', err);
  263. }
  264. //cc.find('Canvas/UITop/MyHeadMask').getChildByName('MyHead').getComponent(cc.Sprite).spriteFrame = frame;
  265. this.MyHead.getComponent(cc.Sprite).spriteFrame = frame;
  266. }.bind(this));
  267. },
  268. setImage_Player2: function (src) {
  269. // console.log('玩家2的头像:',src);
  270. // var self = this;
  271. cc.loader.load({ url: src, type: 'jpg' }, function (err, texture) {
  272. var frame = new cc.SpriteFrame(texture);
  273. if (err) {
  274. console.log('玩家2的头像:', err);
  275. }
  276. // cc.find('Canvas/UITop/RivalHeadMask').getChildByName('RivalHead').getComponent(cc.Sprite).spriteFrame = frame;
  277. this.RiveelHead.getComponent(cc.Sprite).spriteFrame = frame;
  278. this.RivelHeadImage = frame;
  279. }.bind(this));
  280. },
  281. setName_Player1: function (name) {
  282. // cc.find('UITop').getChildByName('MyName').getComponent(cc.Label).String = name;
  283. var MyName = this.UITop.getChildByName('MyName');
  284. var myLabel = MyName.getComponent(cc.Label);
  285. // myLabel.string = this.subString('HH昵称最多七1个字',14);
  286. myLabel.string = this.subString(name, 14);
  287. },
  288. setName_Player2: function (name) {
  289. //todo 设置文字的坑
  290. // cc.find('UITop').getChildByName('RivalName').getComponent(cc.Label).String = name;
  291. var RivalName = this.UITop.getChildByName('RivalName');
  292. var myLabel = RivalName.getComponent(cc.Label);
  293. myLabel.string = this.subString(name, 14);
  294. },
  295. subString: function (str, subNum) {//截取str的subNum个字符。中文算2个,中文之外算1个。
  296. var currentNum = 0;
  297. var subStr = '';
  298. for (var i = 0; i < str.length; i++) {
  299. if (currentNum <= subNum) {
  300. if (currentNum + 3 > subNum) {
  301. subStr += "...";
  302. break;
  303. }
  304. else {
  305. var strI = str[i];
  306. if (/^[\u4e00-\u9fa5]*$/.test(strI)) {//中文字符
  307. currentNum += 2;
  308. subStr += strI;
  309. // cc.log(strI+'++++++++++');
  310. }
  311. else {//除了中文外的其它字符
  312. currentNum += 1;
  313. subStr += strI;
  314. // cc.log(strI+'*********');
  315. }
  316. }
  317. }
  318. }
  319. if (str.length) {
  320. }
  321. return subStr;
  322. },
  323. setName_Gender1: function (name) {
  324. // cc.find('UITop').getChildByName('MyName').getComponent(cc.Label).String = name;
  325. var MyName = this.UITop.getChildByName('GenderPlayer1');
  326. var src = "nan";
  327. if (name == "1") {
  328. } else {
  329. src = "nv";
  330. }
  331. cc.loader.loadRes(src, cc.SpriteFrame, function (err, spriteFrame) { //这个方法
  332. MyName.getComponent(cc.Sprite).spriteFrame = spriteFrame;
  333. });
  334. // cc.loader.load(src,function (err, texture) {
  335. //
  336. // var frame=new cc.SpriteFrame(texture);
  337. // MyName.getComponent(cc.Sprite).spriteFrame=frame;
  338. // });
  339. },
  340. setName_Gender2: function (name) {
  341. //todo 设置文字的坑
  342. // cc.find('UITop').getChildByName('RivalName').getComponent(cc.Label).String = name;
  343. var MyName = this.UITop.getChildByName('GenderPlayer2');
  344. var src = "nan";
  345. if (name == "1") {
  346. } else {
  347. src = "nv";
  348. }
  349. cc.loader.loadRes(src, cc.SpriteFrame, function (err, spriteFrame) { //这个方法
  350. MyName.getComponent(cc.Sprite).spriteFrame = spriteFrame;
  351. });
  352. },
  353. setUITimer: function (actor, timerSecond) {//UI显示消失计时器
  354. // actor.active=true;
  355. /**
  356. * timerSecond 表示此UI显示timerSecond秒后消失。当timerSecond为0的时候,此UI一直显示,不会消失。
  357. */
  358. if (timerSecond > 0) {
  359. this.scheduleOnce(function () {
  360. if (actor.name != "") {
  361. actor.destroy();
  362. // console.log(actor.name+" destroy.");
  363. }
  364. }, timerSecond);
  365. }
  366. },
  367. resetGame: function () {//再来一次游戏
  368. var Hero = cc.find("Hero");
  369. var NetworkSocket = Hero.getComponent('NetworkSocket');
  370. NetworkSocket.endGame();
  371. // if (cc.director.isPaused()) {
  372. // //暂停游戏
  373. // cc.director.resume();
  374. // } else {
  375. // //暂停游戏
  376. // cc.director.pause();
  377. // }
  378. //测试切换头像
  379. // var GameStates = cc.find('Canvas').getComponent('GameStates');
  380. // if (GameStates.bServer) {
  381. // console.log(111);
  382. // var randomSkin = cc.find("randomSkin");
  383. // randomSkin.getComponent("upDateSkin").setRandomSkinss();
  384. // }
  385. //重新读取场景
  386. // cc.director.loadScene("camera_Mutiplayers");
  387. },
  388. CreateReadyGo: function () {
  389. var aPrefab = cc.instantiate(this.ReadyGoPrefab);
  390. aPrefab.parent = cc.director.getScene();
  391. aPrefab.anchorX = 0.5;
  392. aPrefab.x = 355;
  393. aPrefab.y = 918;
  394. aPrefab.setLocalZOrder(1000);
  395. return aPrefab;
  396. },
  397. //设置语音状态
  398. setPlayerVoice(isSelf, isOpen) {
  399. if (isSelf == 1) {
  400. //自己
  401. if (isOpen == 1) {
  402. this.VideoStatesLeft.getChildByName('Open').active = true;
  403. this.VideoStatesLeft.getChildByName('Close').active = false;
  404. } else if (isOpen == 0) {
  405. this.VideoStatesLeft.getChildByName('Open').active = false;
  406. this.VideoStatesLeft.getChildByName('Close').active = true;
  407. }
  408. } else if (isSelf == 2) {
  409. //对手
  410. if (isOpen == 1) {
  411. this.VideoStatesRight.getChildByName('Open').active = true;
  412. this.VideoStatesRight.getChildByName('Close').active = false;
  413. } else if (isOpen == 0) {
  414. this.VideoStatesRight.getChildByName('Open').active = false;
  415. this.VideoStatesRight.getChildByName('Close').active = true;
  416. }
  417. }
  418. }
  419. });