ChatPrefab.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. cc.Class({
  2. extends: cc.BaseClass,
  3. properties:
  4. {
  5. m_ChatEditbox: cc.EditBox,
  6. m_SendNode: cc.Node,
  7. m_ViewNode: cc.Node,
  8. m_PhraseNode: cc.Node,
  9. m_PhraseArr: [cc.Node],
  10. m_FacesNode: cc.Node,
  11. m_FacesArr: [cc.Node],
  12. m_UserChatNode: cc.Node,
  13. m_UserFacesNode: cc.Node,
  14. // m_atlas:cc.SpriteAtlas,
  15. },
  16. ctor: function () {
  17. // 默认快捷短语文字
  18. this.m_szDefText = new Array(
  19. '不好意思,來個電話',
  20. '出門沒洗手,要啥啥沒有',
  21. '都別走,我們決戰到天亮',
  22. '幹啥呢,別墨蹟',
  23. '就是娛樂別那麼認真',
  24. '快點吧,一趟北京都回來了',
  25. '說啥都沒用,就是闖',
  26. '我去,這牌都沒贏到你',
  27. '我說朋友,你是偷的網嗎?',
  28. '我這也是真幸,要啥來啥呀',
  29. '要牌,要快,別墨蹟',
  30. '這牌也真是沒誰了',
  31. );
  32. this.m_szText = new Array();
  33. this.m_CntDownArr = new Array();
  34. this.m_UserChat = new Array();
  35. this.m_UserFaces = new Array();
  36. this.m_GameClientEngine = null;
  37. },
  38. onLoad: function () {
  39. for (var i = 0; i < GameDef.GAME_PLAYER; i++) {
  40. this.m_CntDownArr[i] = 0;
  41. }
  42. this.schedule(this.CheckShow, 1);
  43. },
  44. CheckShow: function () {
  45. for (var i = 0; i < GameDef.GAME_PLAYER; i++) {
  46. if (this.m_UserChat[i] == null) return
  47. if (this.m_CntDownArr[i] > 0) {
  48. this.m_CntDownArr[i]--;
  49. } else {
  50. this.m_UserChat[i].active = false;
  51. this.m_UserFaces[i].node.active = false;
  52. }
  53. }
  54. },
  55. InitHook: function (Hook) {
  56. if (this.m_GameClientEngine != null) return;
  57. this.m_Hook = Hook;
  58. this.m_GameClientEngine = Hook.m_GameClientEngine;
  59. if (this.m_GameClientEngine.m_szText) this.m_szText = this.m_GameClientEngine.m_szText;
  60. else this.m_szText = this.m_szDefText;
  61. this.InitPhraseList();
  62. this.m_bNeedUpdate = true;
  63. this.m_SendNode.active = false;
  64. },
  65. InitPhraseList: function () {
  66. //快捷短语
  67. if (this.m_PhraseArr.length == 1) {
  68. for (var i = 0; i < this.m_szText.length; i++) {
  69. if (this.m_PhraseArr[i] == null) {
  70. this.m_PhraseArr[i] = cc.instantiate(this.m_PhraseArr[0]);
  71. this.m_PhraseNode.addChild(this.m_PhraseArr[i]);
  72. }
  73. this.m_PhraseArr[i].getComponent(cc.Button).clickEvents[0].customEventData = i + 1;
  74. this.m_PhraseArr[i].getChildByName('Label').getComponent(cc.Label).string = cutstr(this.m_szText[i], 13);
  75. }
  76. }
  77. //表情
  78. if (this.m_FacesArr.length == 1) {
  79. for (var i = 0; i < 27; i++) {
  80. if (this.m_FacesArr[i] == null) {
  81. this.m_FacesArr[i] = cc.instantiate(this.m_FacesArr[0]);
  82. this.m_FacesNode.addChild(this.m_FacesArr[i]);
  83. }
  84. this.m_FacesArr[i].getComponent(cc.Button).clickEvents[0].customEventData = 1000 + i;
  85. // this.m_FacesArr[i].getChildByName('Sprite').getComponent(cc.Sprite).spriteFrame = this.m_atlas.getSpriteFrame('faces'+i);
  86. cc.gPreLoader.LoadRes('Image_faces' + i, 'Chat_2', function (sf, Param) {
  87. var Index = Param.Index;
  88. this.m_FacesArr[Index].getChildByName('Sprite').getComponent(cc.Sprite).spriteFrame = sf;
  89. }.bind(this), { Index: i });
  90. }
  91. }
  92. //聊天气泡
  93. if (this.m_UserChat[0] == null) {
  94. this.m_UserChat[0] = this.m_UserChatNode.getChildByName('0');
  95. this.m_UserFaces[0] = this.$('0@AniPrefab', this.m_UserFacesNode);
  96. this.m_UserFaces[0].Init(this);
  97. this.m_UserFaces[0].node.active = false;
  98. for (var i = 1; i < GameDef.GAME_PLAYER; i++) {
  99. this.m_UserChat[i] = cc.instantiate(this.m_UserChat[0]);
  100. this.m_UserChatNode.addChild(this.m_UserChat[i]);
  101. //表情动画节点
  102. var NdTemp = cc.instantiate(this.m_UserFaces[0].node);
  103. this.m_UserFacesNode.addChild(NdTemp);
  104. this.m_UserFaces[i] = this.$('@AniPrefab', NdTemp);
  105. this.m_UserFaces[i].Init(this);
  106. }
  107. }
  108. },
  109. // 关闭
  110. OnButtonClickedClose: function (event, szCustom) {
  111. cc.gSoundRes.PlaySound('Button');
  112. this.m_SendNode.active = false;
  113. },
  114. // 快捷短语
  115. OnButtonClickedShortcutPhrase: function (event, szCustom) {
  116. cc.gSoundRes.PlaySound('Button');
  117. if (this.m_CntDownArr[GameDef.MYSELF_VIEW_ID] > 0) {
  118. this.ShowTips("請不要頻繁說話!")
  119. return;
  120. }
  121. this.m_GameClientEngine.OnSendPhrase(Number(szCustom), 0);
  122. this.m_ChatEditbox.string = '';
  123. this.ShowSendChat(false);
  124. },
  125. // 发送聊天
  126. OnButtonClickedSendChat: function () {
  127. cc.gSoundRes.PlaySound('Button');
  128. if (this.m_CntDownArr[GameDef.MYSELF_VIEW_ID] > 0) {
  129. this.ShowTips("請不要頻繁說話!")
  130. return;
  131. }
  132. var szText = '';
  133. szText = this.m_ChatEditbox.string;
  134. if (szText.length <= 0) return;
  135. if (this.m_GameClientEngine)
  136. this.m_GameClientEngine.OnSendChat(szText);
  137. this.m_ChatEditbox.string = '';
  138. this.ShowSendChat(false);
  139. },
  140. ShowSendChat: function (bShow) {
  141. this.m_SendNode.active = bShow;
  142. },
  143. // 显示泡泡
  144. ShowBubblePhrase: function (wViewChairID, wItemIndex, sex,wChairID) {
  145. if (wViewChairID < 0 || wViewChairID >= GameDef.GAME_PLAYER) return false;
  146. this.m_CntDownArr[wViewChairID] = 3;
  147. // 短语声音
  148. if (wItemIndex < 1000) {
  149. // cc.gSoundRes.PlaySoundPhrase((wItemIndex+"L"+wChairID), sex, this.m_GameClientEngine.m_szText != null);
  150. cc.gSoundRes.PlaySoundPhrase(wItemIndex, sex, this.m_GameClientEngine.m_szText != null);
  151. this.ShowBubble(wViewChairID, this.m_szText[wItemIndex - 1]);
  152. } else if (wItemIndex < 2000) {
  153. this.ShowFaces(wViewChairID, wItemIndex - 1000);
  154. }
  155. return true;
  156. },
  157. // 显示泡泡
  158. ShowBubbleChat: function (wViewChairID, szText) {
  159. if (wViewChairID < 0 || wViewChairID >= GameDef.GAME_PLAYER) return false;
  160. this.m_CntDownArr[wViewChairID] = 3;
  161. this.ShowBubble(wViewChairID, szText);
  162. return true;
  163. },
  164. ShowFaces: function (ViewID, index) {
  165. //this.m_UserFaces[ViewID].getComponent(cc.Sprite).spriteFrame = this.m_atlas.getSpriteFrame('faces'+index);
  166. this.m_UserFaces[ViewID].node.setPosition(this.m_Hook.m_UserFaceArr[ViewID]);
  167. this.m_UserFaces[ViewID].node.active = true;
  168. var pScaleTo = cc.scaleTo(0.1, 1, 1);
  169. this.m_UserFaces[ViewID].node.setScale(0, 0);
  170. this.m_UserFaces[ViewID].node.runAction(pScaleTo);
  171. this.m_UserFaces[ViewID].PlayAni('bq' + (index + 1), 1)
  172. return true;
  173. },
  174. ShowBubble: function (ViewID, szText) {
  175. this.m_UserChat[ViewID].getChildByName('Label').getComponent(cc.Label).string = szText;
  176. var scalex = this.m_Hook.m_UserChatArr[ViewID].x >= 200 ? -1 : 1;
  177. this.m_UserChat[ViewID].setPosition(this.m_Hook.m_UserChatArr[ViewID]);
  178. this.m_UserChat[ViewID].children[0].setScale(scalex, 1);
  179. this.m_UserChat[ViewID].active = true;
  180. var pScaleTo = cc.scaleTo(0.1, scalex, 1);
  181. this.m_UserChat[ViewID].setScale(0, 0);
  182. this.m_UserChat[ViewID].runAction(pScaleTo);
  183. return true;
  184. },
  185. OnToggleSelView: function () {
  186. this.m_bNeedUpdate = true;
  187. },
  188. update: function () {
  189. if (this.m_bNeedUpdate) {
  190. this.m_bNeedUpdate = false;
  191. } else {
  192. return;
  193. }
  194. this.m_PhraseNode.parent.active = this.m_ViewNode.getChildByName('0').getComponent(cc.Toggle).isChecked;
  195. this.m_FacesNode.parent.active = this.m_ViewNode.getChildByName('1').getComponent(cc.Toggle).isChecked;
  196. },
  197. AniFinish: function () {
  198. this.m_Hook.AniFinish(this);
  199. },
  200. });