加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x 手游聊天系统Demo实现(Lua实现)

发布时间:2020-12-14 16:55:31 所属栏目:百科 来源:网络整理
导读:转载请注明:IT_xiao小巫 本篇博客给大家分享的是一个手游聊天系统,笔者也是第一次使用Cocos2d-x来实现这样一个模块,其中有很多不清楚不明白的地方都是通过自己不断摸索实现的,前面笔者对聊天系统做的简单的需求分析,只是对聊天的一些元素进行的说明还不

转载请注明:IT_xiao小巫

本篇博客给大家分享的是一个手游聊天系统,笔者也是第一次使用Cocos2d-x来实现这样一个模块,其中有很多不清楚不明白的地方都是通过自己不断摸索实现的,前面笔者对聊天系统做的简单的需求分析,只是对聊天的一些元素进行的说明还不太够专业。本篇博客会给大家介绍如何实现一个手游聊天Demo,会从代码层面上给大家做相关的说明,如有不对或者错漏的地方请各位明确指出并纠正。


首先来给大家看一下动态效果图:



本篇博客内容大纲:

1. 加载Cocostudio制作的UI

2. Button的触摸事件监听

3. ListView添加列表项并设置列表点击事件

4. 富文本实现(可显示颜色文字和图片、动画)

5. 文本输入框实现(解决pc键盘无法删除字符的bug)

6. 动态往ListView添加列表项



一、加载Cocostudio制作的UI

笔者所分享的这个Demo是通过Cocostudio的UI编辑器制作的,童鞋们也可自己制作更加好看的UI,不过一般都会有美工帮我们做好让我使用的。如下图所示:



UI制作完之后,导出项目,然后把资源复制到我们项目的res目录下,笔者这里是把ChatUI_1复制到了res下,然后我们使用Lua代码实现加载json文件到我们的程序中去:

[javascript] view plain copy
  1. ChatScene.widget=ccs.GUIReader:getInstance():widgetFromJsonFile("ChatUI_1/ChatUI_1.json")

我们在编辑器添加了多个对象:

WorldPanel、PartyPanel、ChatPanel分别对应世界、公会、私聊三个板块,板块下面对应其相应的子节点:WordList、PartyList、ChatList。

我们需要在程序中找到它们:

copy
    --[[
  1. ============================
  2. findViews()
  3. 找到UI控件
  4. ============================
  5. ]]--
  6. functionChatScene.findViews()
  7. ChatScene.widget=ccs.GUIReader:getInstance():widgetFromJsonFile("ChatUI_1/ChatUI_1.json")
  8. ChatScene.widget:setPosition(cc.p(40,40))
  9. loadListViewItemFromJson()
  10. --获得UI界面上的3个按钮
  11. worldButton=ChatScene.widget:getChildByTag(6)
  12. partyButton=ChatScene.widget:getChildByTag(7)
  13. chatButton=ChatScene.widget:getChildByTag(8)
  14. --获得三个每个按钮对应的三个面板
  15. wordPanel=ChatScene.widget:getChildByTag(5)
  16. partyPanel=ChatScene.widget:getChildByTag(9)
  17. chatPanel=ChatScene.widget:getChildByTag(10)
  18. --获得每个面板的ListView
  19. worldList=wordPanel:getChildByTag(13)
  20. partyList=partyPanel:getChildByTag(14)
  21. chatList=chatPanel:getChildByTag(15)
  22. --获得输入框
  23. inputBox=ChatScene.widget:getChildByTag(11)
  24. sendButton=ChatScene.widget:getChildByTag(12)
  25. dialog=ChatScene.widget:getChildByTag(20)
  26. chat=dialog:getChildByTag(21)
  27. lahei=dialog:getChildByTag(22)
  28. closeButton=dialog:getChildByTag(27)
  29. end

每个UI对象有相应的Tag属性,我们可以通过找到其父节点,然后调用getChildByTag传进tag的值找到控件。只有找到这些控件,我们才能去使用它。


二、Button的触摸事件监听

笔者这个demo,通过监听“世界”、“公会”、“私聊”三个按钮来分别切换不同的板块,按钮的触摸监听事件很简单:

copy
    --设置按钮监听事件
  1. worldButton:addTouchEventListener(touchEvent)
  2. partyButton:addTouchEventListener(touchEvent)
  3. chatButton:addTouchEventListener(touchEvent)

copy
    touchEvent
  1. 触摸事件回调方法
  2. localfunctiontouchEvent(sender,eventType)
  3. ifsender:getTag()==TAG_WORLD_BUTTONthen
  4. wordPanel:setVisible(true)
  5. partyPanel:setVisible(false)
  6. chatPanel:setVisible(false)
  7. dialog:setVisible( ChatScene.setCurrentTag(TAG_WORLD)
  8. elseifsender:getTag()==TAG_PARTY_BUTTONthen
  9. partyPanel:setVisible( wordPanel:setVisible( ChatScene.setCurrentTag(TAG_PARTY)
  10. elseifsender:getTag()==TAG_CHAT_BUTTONthen
  11. ChatScene.setCurrentTag(TAG_CHAT)
  12. end

以上面这种方式就可以实现切换三个板块了。


三、ListView添加列表项并设置列表点击事件

我们可以看到效果图里面每个板块下面有对应的列表,它是使用Cocos2d-x UI中的ListView所呈现的。

笔者感觉使用ListView比较麻烦,这里笔者给出相应的使用方法供大家参考:

--首先我们为ListView提供三组数据

copy
    --初始化三组数据
  1. localarray={}
  2. fori=1,20do
  3. array[i]=string.format("请叫我巫大大%d",i-1)
  4. end
  5. localarray1={}
  6. do
  7. array1[i]=string.format("公会开放啦%d",i-1)
  8. end
  9. localarray2={}
  10. array2[i]=string.format("私聊列表项%d",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> end

--设置默认模型

copy
    --创建模型
  1. localdefault_button=ccui.Button:create("cocosui/backtotoppressed.png","cocosui/backtotopnormal.png")
  2. default_button:setName("TitleButton")
  3. --创建默认item
  4. localdefault_itme=ccui.Layout:create()
  5. default_itme:setTouchEnabled( default_itme:setContentSize(default_button:getContentSize())
  6. default_button:setPosition(cc.p(default_itme:getContentSize().width/2.0,default_itme:getContentSize().height/2.0))
  7. default_itme:addChild(default_button)
  8. --设置模型
  9. worldList:setItemModel(default_itme)

--添加自定义项

copy
    --获得数组的大小
  1. localcount=table.getn(array)
  2. print("count:"..count)
  3. --添加自定义item
  4. --创建一个Button
  5. localcustom_button=ccui.Button:create("cocosui/button.png","cocosui/buttonHighlighted.png")
  6. --设置Button名字
  7. custom_button:setName("TitleButton")
  8. --设置按钮使用九宫(scale9)渲染器进行渲染
  9. custom_button:setScale9Enabled( --设置内容尺寸
  10. custom_button:setContentSize(default_button:getContentSize())
  11. --创建一个布局
  12. localcustom_item=ccui.Layout:create()
  13. --设置内容大小
  14. custom_item:setContentSize(custom_button:getContentSize())
  15. --设置位置
  16. custom_button:setPosition(cc.p(custom_item:getContentSize().width/2.0,custom_item:getContentSize().height/2.0))
  17. --往布局中添加一个按钮
  18. custom_item:addChild(custom_button)
  19. --往ListView中添加一个布局
  20. worldList:addChild(custom_item)
  21. end

--每一项数据

copy
    --设置itemdata
  1. items_count=table.getn(worldList:getItems())
  2. --返回一个索引和参数相同的项.
  3. localitem=worldList:getItem(i-1)
  4. localbutton=item:getChildByName("TitleButton")
  5. localindex=worldList:getIndex(item)
  6. button:setTitleText(array[index+1])
  7. end

--设置ListView的点击事件和滚动事件

copy
    --设置ListView的监听事件
  1. worldList:addScrollViewEventListener(scrollViewEvent)
  2. worldList:addEventListener(listViewEvent)

copy
    --ListView点击事件回调
  1. localfunctionlistViewEvent(sender,eventType)
  2. --事件类型为点击结束
  3. ifeventType==ccui.ListViewEventType.ONSELECTEDITEM_ENDthen
  4. print("selectchildindex=",sender:getCurSelectedIndex())
  5. ifdialog:isVisible()==truethen
  6. dialog:setVisible(else
  7. ChatScene.showDialog()
  8. --滚动事件方法回调
  9. functionscrollViewEvent(sender,248)"> --滚动到底部
  10. ifeventType==ccui.ScrollviewEventType.scrollToBottomthen
  11. print("SCROLL_TO_BOTTOM")
  12. --滚动到顶部
  13. elseifeventType==ccui.ScrollviewEventType.scrollToTopthen
  14. print("SCROLL_TO_TOP")
  15. end

四、富文本实现(可显示颜色文字和图片、动画)

何为富文本?笔者的理解是有着丰富文本的展示方式,比如可以展示颜色文本、图片、动画、还有超链接的这种就叫富文本。以前旧的版本Cocos2d-x可能并未提供这方面的支持,至于是哪个版本支持的笔者也没有去深究,笔者这里使用版本是Cocos2d-x 3.2,它就提供了类似富文本的类,满足基本的需求。



代码实现:

copy
    ==================
  1. RichText
  2. 富文本
  3. =================
  4. functionChatScene.RichText()
  5. localrichText=ccui.RichText:create()
  6. richText:ignoreContentAdaptWithSize( richText:setContentSize(cc.size(100,100))
  7. localre1=ccui.RichElementText:create(1,cc.c3b(255,255),"Thiscoloriswhite.","Helvetica",10)
  8. localre2=ccui.RichElementText:create(2,0),"Andthisisyellow.",10)
  9. localre3=ccui.RichElementText:create(3,cc.c3b(0,"Thisoneisblue.",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> localre4=ccui.RichElementText:create(4,"Andgreen.",248)"> localre5=ccui.RichElementText:create(5,"Lastoneisred",248)"> localreimg=ccui.RichElementImage:create(6,"cocosui/sliderballnormal.png")
  10. --添加ArmatureFileInfo,由ArmatureDataManager管理
  11. ccs.ArmatureDataManager:getInstance():addArmatureFileInfo("cocosui/100/100.ExportJson")
  12. localarr=ccs.Armature:create("100")
  13. arr:getAnimation():play("Animation1")
  14. localrecustom=ccui.RichElementCustomNode:create(1,arr)
  15. localre6=ccui.RichElementText:create(7,127,"Havefun!!",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> richText:pushBackElement(re1)
  16. richText:insertElement(re2,1)
  17. richText:pushBackElement(re3)
  18. richText:pushBackElement(re4)
  19. richText:pushBackElement(re5)
  20. richText:insertElement(reimg,2)
  21. richText:pushBackElement(recustom)
  22. richText:pushBackElement(re6)
  23. richText:setLocalZOrder(10)
  24. returnrichText
  25. end

五、文本输入框实现(解决pc键盘无法删除字符的bug)

CocostudioUI编辑器提供TextField(输入框),笔者在这里也对它进行了实现,聊天系统一般需要玩家输入信息,所以这里提供了一个输入框。但笔者在使用这个UI的时候,发现在win32平台不能对输入的文本进行删除,但在移动设备可以使用输入法对它进行编辑,所以笔者在这里做了相关的处理把这个bug修正了。

copy
    ---键盘事件监听回调方法
  1. functiononkeyPressed(keycode,event)
  2. print("keypress")
  3. ifkeycode==cc.KeyCode.KEY_BACKSPACEthen
  4. localstr=inputBox:getStringValue()
  5. str=string.sub(str,string.len(str)-1)
  6. inputBox:setText(str)
  7. --键盘监听事件
  8. localkeyListener=cc.EventListenerKeyboard:create()
  9. keyListener:registerScriptHandler(onkeyPressed,cc.Handler.EVENT_KEYBOARD_PRESSED)
  10. localeventDispatcher=ChatScene.uiLayer:getEventDispatcher()
  11. eventDispatcher:addEventListenerWithSceneGraphPriority(keyListener,ChatScene.uiLayer)

通过以上方式,我们就可以使用简拼的BackSpace对字符进行删除了。大家请叫我活雷锋。


六、动态往ListView添加列表项

笔者想到聊天系统的列表是不断刷新的,所以可能需要实现动态添加列表项,其实这个实现很简单的,只需要在代码中监听相应的事件,然后往ListView添加一项就可以了。

这里我监听了发送按钮的点击事件,然后获取到输入框的文本,在把文本添加到列表项中去。

copy
    ifsender:getTag()==TAG_SEND_BUTTONthen
  1. print("sendText...")
  2. --获得输入框的文本
  3. localvalue=inputBox:getStringValue()
  4. localtextView=ccui.Text:create(value,"Arial",20)
  5. print("value:"..value)
  6. ifeventType==ccui.TouchEventType.beganthen
  7. --localcustom_text=ChatScene.RichText()
  8. localcustom_item=ccui.Layout:create()
  9. custom_item:setContentSize(textView:getContentSize())
  10. textView:setPosition(cc.p(custom_item:getContentSize().width/2.0,custom_item:getContentSize().height/2.0))
  11. custom_item:addChild(textView)
  12. --如果当前Tag为世界
  13. ifChatScene.getCurrentTag()==TAG_WORLDthen
  14. --插入自定义项
  15. worldList:insertCustomItem(custom_item,0)
  16. --worldList:addChild(custom_item)
  17. elseifChatScene.getCurrentTag()==TAG_PARTYthen
  18. --partyList:addChild(custom_item)
  19. partyList:insertCustomItem(custom_item,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> elseifChatScene.getCurrentTag()==TAG_CHATthen
  20. --chatList:addChild(custom_item)
  21. chatList:insertCustomItem(custom_item,0)
  22. end


以上基本是笔者这个聊天系统的重要内容,下面把完整的实现代码给大家:

copy
    ===============
  1. ChatSence
  2. 聊天系统模块
  3. ===============
  4. --类
  5. localChatScene={}
  6. ChatScene.uiLayer=nil
  7. ChatScene.widget=nil
  8. --窗口大小
  9. localwinSize=nil
  10. localworldButton=nil
  11. localpartyButton=nil
  12. localchatButton=nil
  13. localwordPanel=nil
  14. localpartyPanel=nil
  15. localchatPanel=nil
  16. localworldList=nil
  17. localpartyList=nil
  18. localchatList=nil
  19. --列表项
  20. locallistview_item=nil
  21. localhead_icon=nil
  22. locallevel=nil
  23. localname=nil
  24. localtext=nil
  25. --列表项个数
  26. localitems_count=nil
  27. localinputBox=nil
  28. localsendButton=nil
  29. --弹出对话框
  30. localdialog=nil
  31. localchat=nil
  32. locallahei=nil
  33. localcloseButton=nil
  34. --三个标记
  35. localflag=nil
  36. localTAG_WORLD=1--标识世界
  37. localTAG_PARTY=2--标识公会
  38. localTAG_CHAT=3--标识私聊
  39. --一些按钮的Tag
  40. localTAG_WORLD_BUTTON=1
  41. localTAG_PARTY_BUTTON=2
  42. localTAG_CHAT_BUTTON=3
  43. localTAG_SEND_BUTTON=4
  44. localTAG_CHAT_BUTTON2=5
  45. localTAG_LAHEI_BUTTON=6
  46. localTAG_CLOSE_BUTTON=7
  47. --场景创建
  48. ChatScene.create=function()
  49. localscene=cc.Scene:create()
  50. scene:addChild(ChatScene.createChatLayer())
  51. returnscene
  52. --[[
  53. elseifsender:getTag()==TAG_SEND_BUTTONthen
  54. print("sendText...")
  55. --获得输入框的文本
  56. localvalue=inputBox:getStringValue()
  57. localtextView=ccui.Text:create(value,20)
  58. print("value:"..value)
  59. ifeventType==ccui.TouchEventType.beganthen
  60. --localcustom_text=ChatScene.RichText()
  61. custom_item:setContentSize(textView:getContentSize())
  62. textView:setPosition(cc.p(custom_item:getContentSize().width/2.0,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> custom_item:addChild(textView)
  63. --如果当前Tag为世界
  64. ifChatScene.getCurrentTag()==TAG_WORLDthen
  65. --插入自定义项
  66. worldList:insertCustomItem(custom_item,248)"> --worldList:addChild(custom_item)
  67. elseifChatScene.getCurrentTag()==TAG_PARTYthen
  68. --partyList:addChild(custom_item)
  69. partyList:insertCustomItem(custom_item,248)"> elseifChatScene.getCurrentTag()==TAG_CHATthen
  70. --chatList:addChild(custom_item)
  71. chatList:insertCustomItem(custom_item,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> elseifsender:getTag()==TAG_CHAT_BUTTON2then
  72. chatPanel:setVisible(true)
  73. elseifsender:getTag()==TAG_LAHEI_BUTTONthen
  74. print("我就把你拉黑,逗比")
  75. elseifsender:getTag()==TAG_CLOSE_BUTTONthen
  76. elseifsender:getTag()==8then
  77. ifeventType==ccui.TouchEventType.endedthen
  78. ChatScene.widget:setVisible(notChatScene.widget:isVisible())
  79. functiononExit(strEventName)
  80. ChatScene.uiLayer:release()
  81. ChatScene.uiLayer=nil
  82. --[[
  83. addOpenButton
  84. 添加一个打开的按钮
  85. =================
  86. ]]--
  87. functionChatScene.addOpenButton()
  88. localopenButton=ccui.Button:create()--创建一个按钮
  89. openButton:setTouchEnabled(true)--设置可触摸
  90. openButton:loadTextures("cocosui/animationbuttonnormal.png","cocosui/animationbuttonpressed.png","")--加载纹理
  91. openButton:setAnchorPoint(cc.p(0,0))
  92. openButton:setPosition(cc.p(winSize.width-100,winSize.height-50))
  93. ChatScene.uiLayer:addChild(openButton,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> openButton:setTag(8)
  94. openButton:addTouchEventListener(touchEvent)
  95. ==============
  96. textFieldEvent
  97. 输入框监听事件回调方法
  98. ==============
  99. functiontextFieldEvent(sender,153); font-weight:bold; background-color:inherit">ifeventType==ccui.TextFiledEventType.attach_with_imethen
  100. print("attach_with_ime")
  101. elseifeventType==ccui.TextFiledEventType.detach_with_imethen
  102. print("detach_with_ime")
  103. elseifeventType==ccui.TextFiledEventType.insert_textthen
  104. print("insert_text")
  105. elseifeventType==ccui.TextFiledEventType.delete_backwardthen
  106. print("delete_backward")
  107. --ListView点击事件回调
  108. ====================
  109. createChatLayer
  110. 创建聊天层
  111. ====================
  112. functionChatScene.createChatLayer()
  113. ChatScene.uiLayer=cc.Layer:create()--创建ui层
  114. print("getReferenceCount1:"..ChatScene.uiLayer:getReferenceCount())
  115. winSize=cc.Director:getInstance():getWinSize()--获得屏幕大小
  116. ChatScene.setCurrentTag(TAG_WORLD)
  117. ChatScene.addOpenButton()
  118. ChatScene.findViews()
  119. ChatScene.setTouchEnabled()
  120. ChatScene.setTags()
  121. ChatScene.addTouchEventListener()
  122. --初始化三组数据
  123. localarray={}
  124. array[i]=string.format("请叫我巫大大%d",248)"> localarray1={}
  125. array1[i]=string.format("公会开放啦%d",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> localarray2={}
  126. array2[i]=string.format("私聊列表项%d",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> --创建模型
  127. worldList:setItemModel(default_itme)
  128. --这里是5项
  129. -- --print("i:"..i)
  130. ----压栈一个默认项(通过克隆创建的)进listView.
  131. --worldList:pushBackDefaultItem()
  132. --end
  133. --
  134. ----插入默认项
  135. -- ----插入一个默认项(通过克隆创建的)进listView.
  136. --worldList:insertDefaultItem(0)
  137. --使用cleanup清空容器(container)中的所有子节点(children)
  138. --worldList:removeAllChildren()
  139. --localtestSprite=cc.Sprite:create("cocosui/backtotoppressed.png")
  140. --testSprite:setPosition(cc.p(200,200))
  141. --worldList:addChild(testSprite)
  142. --获得数组的大小
  143. localcount=table.getn(array)
  144. print("count:"..count)
  145. --添加自定义item
  146. --创建一个Button
  147. localcustom_button=ccui.Button:create("cocosui/button.png","cocosui/buttonHighlighted.png")
  148. --设置Button名字
  149. custom_button:setName("TitleButton")
  150. --设置按钮使用九宫(scale9)渲染器进行渲染
  151. custom_button:setScale9Enabled( --设置内容尺寸
  152. custom_button:setContentSize(default_button:getContentSize())
  153. --创建一个布局
  154. --设置内容大小
  155. custom_item:setContentSize(custom_button:getContentSize())
  156. --设置位置
  157. custom_button:setPosition(cc.p(custom_item:getContentSize().width/2.0,248)"> --往布局中添加一个按钮
  158. custom_item:addChild(custom_button)
  159. --往ListView中添加一个布局
  160. worldList:addChild(custom_item)
  161. --localfunctioncustomButtonListener(sender,touchType)
  162. ifsender:getTag()==1then
  163. --dialog:setVisible( --end
  164. custom_button:setName("wwj")
  165. partyList:addChild(custom_item)
  166. custom_button:setName("wwj")
  167. chatList:addChild(custom_item)
  168. localcustom_text=ChatScene.RichText()
  169. custom_item:setTouchEnabled( custom_item:setContentSize(custom_text:getContentSize())
  170. custom_text:setPosition(cc.p(custom_item:getContentSize().width/2.0,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> custom_item:addChild(custom_text)
  171. chatList:addChild(custom_item)
  172. --localcustom_button=ccui.Button:create("cocosui/button.png",248)"> --custom_button:setName("wwj")
  173. --custom_button:setScale9Enabled( --custom_button:setContentSize(default_button:getContentSize())
  174. --localcustom_item2=ccui.Layout:create()
  175. --custom_item2:setContentSize(custom_button:getContentSize())
  176. --custom_button:setPosition(cc.p(custom_item2:getContentSize().width/0.6,custom_item2:getContentSize().height/0.6))
  177. --custom_item2:addChild(custom_button)
  178. --custom_button:setTag(i)
  179. --custom_button:addTouchEventListener(customButtonListener)
  180. --chatList:addChild(custom_item2)
  181. --插入自定义item
  182. localitems=worldList:getItems()--返回项的集合
  183. --获得项的个数
  184. localitems_count=table.getn(items)
  185. --custom_button:setName("TitleButton")--改变widget的名字,使用名字可以更轻松地识别出该widget
  186. true)--设置按钮使用九宫(scale9)渲染器进行渲染
  187. --localcustom_item=ccui.Layout:create()
  188. --custom_item:setContentSize(custom_button:getContentSize())
  189. --custom_button:setPosition(cc.p(custom_item:getContentSize().width/2.0,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> --custom_item:addChild(custom_button)
  190. --custom_item:setTag(1)
  191. --worldList:insertCustomItem(custom_item,items_count)
  192. --设置itemdata
  193. items_count=table.getn(worldList:getItems())
  194. --返回一个索引和参数相同的项.
  195. localitem=worldList:getItem(i-1)
  196. localbutton=item:getChildByName("TitleButton")
  197. localindex=worldList:getIndex(item)
  198. button:setTitleText(array[index+1])
  199. localpartyListItems_count=table.getn(partyList:getItems())
  200. localitem=partyList:getItem(i-1)
  201. localbutton=item:getChildByName("wwj")
  202. localindex=partyList:getIndex(item)
  203. button:setTitleText(array1[index+1])
  204. localchatListItems_count=table.getn(chatList:getItems())
  205. localitem=chatList:getItem(i-1)
  206. localbutton=item:getChildByName("wwj")
  207. localindex=chatList:getIndex(item)
  208. button:setTitleText(array2[index+1])
  209. --移除Tag=1的子节点
  210. --worldList:removeChildByTag(1)
  211. --移除项byindex
  212. --items_count=table.getn(worldList:getItems())
  213. --worldList:removeItem(items_count-1)
  214. --设置ListView对齐方式为横向居中
  215. worldList:setGravity(ccui.ListViewGravity.centerVertical)
  216. --setitemsmargin
  217. worldList:setItemsMargin(2.0)
  218. worldList:setBounceEnabled( --设置ListView对齐方式为横向居中
  219. partyList:setGravity(ccui.ListViewGravity.centerVertical)
  220. --setitemsmargin
  221. partyList:setItemsMargin(2.0)
  222. inputBox:addEventListener(textFieldEvent)
  223. ChatScene.uiLayer:addChild(ChatScene.widget)
  224. ChatScene.widget:setVisible( --ChatScene.uiLayer:registerScriptHandler(onExit)
  225. returnChatScene.uiLayer
  226. functionListViewItem()
  227. locallayout=ccui.Layout:create()
  228. layout:setSizePercent(cc.p(200,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> layout:setBackGroundColorType(ccui.LayoutBackGroundColorType.solid)
  229. layout:setBackGroundColor(cc.c3b(255,248)"> localimage=ccui.ImageView:create("")
  230. layout:addChild(image)
  231. returnlayout
  232. functionloadListViewItemFromJson()
  233. listview_item=ccs.GUIReader:getInstance():widgetFromJsonFile("res/listview_item/listview_item.ExportJson")
  234. head_icon=listview_item:getChildByTag(6)
  235. level=listview_item:getChildByTag(7)
  236. name=listview_item:getChildByTag(8)
  237. text=listview_item:getChildByTag(9)
  238. ===================
  239. 设置相关标记
  240. functionChatScene.setTags()
  241. worldButton:setTag(TAG_WORLD_BUTTON)
  242. partyButton:setTag(TAG_PARTY_BUTTON)
  243. chatButton:setTag(TAG_CHAT_BUTTON)
  244. sendButton:setTag(TAG_SEND_BUTTON)
  245. chat:setTag(TAG_CHAT_BUTTON2)
  246. lahei:setTag(TAG_LAHEI_BUTTON)
  247. closeButton:setTag(TAG_CLOSE_BUTTON)
  248. addTouchEventListener
  249. 添加触摸事件
  250. ==================
  251. functionChatScene.addTouchEventListener()
  252. --设置按钮监听事件
  253. worldButton:addTouchEventListener(touchEvent)
  254. partyButton:addTouchEventListener(touchEvent)
  255. chatButton:addTouchEventListener(touchEvent)
  256. sendButton:addTouchEventListener(touchEvent)
  257. chat:addTouchEventListener(touchEvent)
  258. lahei:addTouchEventListener(touchEvent)
  259. closeButton:addTouchEventListener(touchEvent)
  260. --设置ListView的监听事件
  261. worldList:addEventListener(listViewEvent)
  262. partyList:addScrollViewEventListener(scrollViewEvent)
  263. partyList:addEventListener(listViewEvent)
  264. chatList:addScrollViewEventListener(scrollViewEvent)
  265. chatList:addEventListener(listViewEvent)
  266. ---键盘事件监听回调方法
  267. functiontextFieldCompleteHandler()
  268. =====================
  269. setTouchEnabled
  270. 设置一些控件可触摸
  271. functionChatScene.setTouchEnabled()
  272. --设置可触摸
  273. worldButton:setTouchEnabled( partyButton:setTouchEnabled( chatButton:setTouchEnabled( sendButton:setTouchEnabled( chat:setTouchEnabled( lahei:setTouchEnabled( closeButton:setTouchEnabled( inputBox:setTouchEnabled( setCurrentTag
  274. 设置当前Tag
  275. functionChatScene.setCurrentTag(tag)
  276. flag=tag;
  277. ================
  278. 获得当前Tag
  279. functionChatScene.getCurrentTag()
  280. returnflag
  281. 显示dialog
  282. functionChatScene.showDialog()
  283. localpopup=cc.Sequence:create(cc.ScaleTo:create(0.0,0.0),
  284. cc.ScaleTo:create(0.06,1.05),
  285. cc.ScaleTo:create(0.08,0.95),248)"> cc.ScaleTo:create(0.08,1.0),108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> nil)
  286. dialog:runAction(popup)
  287. --返回场景
  288. returnChatScene

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读