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

本文详细介绍了Cocos2d-x 3.2版本中手游聊天系统的实现,包括ListView的事件监听、富文本显示以及文本输入框的处理。通过Lua实现动态添加列表项、处理键盘事件以实现文本删除功能,并展示了如何在ListView中插入自定义项。此外,还探讨了富文本的概念及其在Cocos2d-x中的应用,如展示颜色文字、图片和动画。
摘要由CSDN通过智能技术生成

for i = 1, items_count do

– 返回一个索引和参数相同的项.

local item = worldList:getItem( i - 1 )

local button = item:getChildByName(“Title Button”)

local index = worldList:getIndex(item)

button:setTitleText(array[index + 1])

end

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

– 设置ListView的监听事件

worldList:addScrollViewEventListener(scrollViewEvent)

worldList:addEventListener(listViewEvent)

– ListView点击事件回调

local function listViewEvent(sender, eventType)

– 事件类型为点击结束

if eventType == ccui.ListViewEventType.ONSELECTEDITEM_END then

print("select child index = ",sender:getCurSelectedIndex())

if dialog:isVisible() == true then

dialog:setVisible(false)

else

ChatScene.showDialog()

end

end

end

– 滚动事件方法回调

local function scrollViewEvent(sender, eventType)

– 滚动到底部

if eventType == ccui.ScrollviewEventType.scrollToBottom then

print(“SCROLL_TO_BOTTOM”)

– 滚动到顶部

elseif eventType == ccui.ScrollviewEventType.scrollToTop then

print(“SCROLL_TO_TOP”)

end

end

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


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

代码实现:

–[[

==================

RichText

富文本

=================

]]–

function ChatScene.RichText()

local richText = ccui.RichText:create()

richText:ignoreContentAdaptWithSize(false)

richText:setContentSize(cc.size(100, 100))

local re1 = ccui.RichElementText:create( 1, cc.c3b(255, 255, 255), 255, "This color is white. ", “Helvetica”, 10 )

local re2 = ccui.RichElementText:create( 2, cc.c3b(255, 255, 0), 255, "And this is yellow. ", “Helvetica”, 10 )

local re3 = ccui.RichElementText:create( 3, cc.c3b(0, 0, 255), 255, "This one is blue. ", “Helvetica”, 10 )

local re4 = ccui.RichElementText:create( 4, cc.c3b(0, 255, 0), 255, "And green. ", “Helvetica”, 10 )

local re5 = ccui.RichElementText:create( 5, cc.c3b(255, 0, 0), 255, "Last one is red ", “Helvetica”, 10 )

local reimg = ccui.RichElementImage:create( 6, cc.c3b(255, 255, 255), 255, “cocosui/sliderballnormal.png” )

– 添加ArmatureFileInfo, 由ArmatureDataManager管理

ccs.ArmatureDataManager:getInstance():addArmatureFileInfo( “cocosui/100/100.ExportJson” )

local arr = ccs.Armature:create( “100” )

arr:getAnimation():play( “Animation1” )

local recustom = ccui.RichElementCustomNode:create( 1, cc.c3b(255, 255, 255), 255, arr )

local re6 = ccui.RichElementText:create( 7, cc.c3b(255, 127, 0), 255, "Have fun!! ", “Helvetica”, 10 )

richText:pushBackElement(re1)

richText:insertElement(re2, 1)

richText:pushBackElement(re3)

richText:pushBackElement(re4)

richText:pushBackElement(re5)

richText:insertElement(reimg, 2)

richText:pushBackElement(recustom)

richText:pushBackElement(re6)

richText:setLocalZOrder(10)

return richText

end

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


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

— 键盘事件监听回调方法

local function onkeyPressed(keycode, event)

print(“keypress”)

if keycode == cc.KeyCode.KEY_BACKSPACE then

local str = inputBox:getStringValue()

str = string.sub( str, 0, string.len( str ) - 1 )

inputBox:setText( str )

end

end

– 键盘监听事件

local keyListener = cc.EventListenerKeyboard:create()

keyListener:registerScriptHandler(onkeyPressed,cc.Handler.EVENT_KEYBOARD_PRESSED)

local eventDispatcher = ChatScene.uiLayer:getEventDispatcher()

eventDispatcher:addEventListenerWithSceneGraphPriority(keyListener, ChatScene.uiLayer)

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

六、动态往ListView添加列表项


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

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

if sender:getTag() == TAG_SEND_BUTTON then

print(“sendText…”)

– 获得输入框的文本

local value = inputBox:getStringValue()

local textView = ccui.Text:create(value,“Arial”,20)

print(“value:”…value)

if eventType == ccui.TouchEventType.began then

– local custom_text = ChatScene.RichText()

local custom_item = ccui.Layout:create()

custom_item:setContentSize( textView:getContentSize() )

textView:setPosition( cc.p( custom_item:getContentSize().width / 2.0, custom_item:getContentSize().height / 2.0 ) )

custom_item:addChild( textView )

– 如果当前Tag为世界

if ChatScene.getCurrentTag() == TAG_WORLD then

– 插入自定义项

worldList:insertCustomItem( custom_item, 0 )

– worldList:addChild(custom_item)

elseif ChatScene.getCurrentTag() == TAG_PARTY then

– partyList:addChild(custom_item)

partyList:insertCustomItem( custom_item, 0 )

elseif ChatScene.getCurrentTag() == TAG_CHAT then

– chatList:addChild(custom_item)

chatList:insertCustomItem( custom_item, 0 )

end

end

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

–[[

===============

ChatSence

聊天系统模块

========

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值