cocos2d-lua 手游之好友系统
来源:互联网 发布:centos man 中文 编辑:程序博客网 时间:2024/05/02 04:55
好友系统
特此声明:由于本人只完成了好友系统的demo,所以此好友系统和服务器没有任何交互。
本人只实现了如下几个功能,具体需求自行扩充
1.申请好友列表有接受,不接受两个选项
2.可查询自己已经在好友列表里面的好友
3.好友列表可查看信息(未详细完成)
首先好友的信息全部由FriendManager进行管理
FriendStruct设置好友信息
ListViewTool 实现让ListView实现TableView的功能(只加载显示区域的item)
FriendStruct=class("FriendStruct")
function FriendStruct:ctor(data)if not data then returnendself.user_id=data.user_id or self.user_idself.user_name=data.user_name or self.user_name or ""self.user_age=data.user_age or self.user_age or ""self.user_level=data.user_level or self.user_level or ""self.daily_world=data.daily_world or self.daily_world or ""end---获取用户IDfunction FriendStruct:getUserID()return self.user_idend---获取用户的名字function FriendStruct:getUserName()return self.user_nameend---获取用户的agefunction FriendStruct:getUserAge()return self.user_ageend---获取用户的等级function FriendStruct:getUserLevel()return self.user_levelend---获取用户的签名function FriendStruct:getUserDailyWorld()return self.daily_worldend---移除用户的Namefunction FriendStruct:removeUserName()self.user_name=""end---移除用户的agefunction FriendStruct:removeUserAge()self.user_age=""end---移除用户的user_levelfunction FriendStruct:removeUserLevel()self.user_level=""end---移除用户的daily_worldfunction FriendStruct:removeDailyWorld()self.daily_world=""endfunction FriendStruct:updateData(data)self:ctor(data)endfunction FriendStruct:onRelease()endreturn FriendStruct
FriendMgr 的具体实现
local FriendStruct=require("app.Test.FriendStruct")
FriendsManager=class("FreiendsManager")local _instance=nillocal _friends_map=nillocal _invite_friends_map=nilfunction FriendsManager:ctor()_friends_map={}_invite_friends_map={}endfunction FriendsManager:getInstance()if _instance==nil then _instance=FriendsManager.new()endreturn _instanceend--------------------------------------邀请好友数据---------------------------------------------------------function FriendsManager:addInviteFriends(invite_friend_data_map)if not invite_friend_data_map thenreturnendfor data_index,invite_friend_data in pairs(invite_friend_data_map) doinvite_friend_data=invite_friend_data_map[data_index]if not invite_friend_data thenreturnendif not invite_friend_data.user_id thenreturnend_invite_friends_map[invite_friend_data.user_id]=FriendStruct.new(invite_friend_data)end-- dump(DataCenter)DataCenter:sendMsg(DataEvent.FRIEND.FRIENDSHENQ, param)endfunction FriendsManager:getInviteFriends()if not _invite_friends_map thenreturnendlocal invite_friends_data =self:setDateToList(_invite_friends_map)if not invite_friends_data thenreturnendlocal invite_friends_sort_data=nilif #invite_friends_data >=2 theninvite_friends_sort_data=self:setSortData(invite_friends_data)elseinvite_friends_sort_data=invite_friends_dataendreturn invite_friends_sort_dataendfunction FriendsManager:removeInviteFriendsById(friend_tag)if not _invite_friends_map thenreturnend_invite_friends_map[friend_tag]=nilendfunction FriendsManager:removeAllInviteFriends()if not _invite_friends_map thenreturnend_invite_friends_map=nilendfunction FriendsManager:getInviteNameById(friend_tag)if not _invite_friends_map thenreturnendif not _invite_friends_map[friend_tag] thenreturnendreturn _invite_friends_map[friend_tag]:getUserName()end---------------------------------------------好友数据---------------------------------------------------function FriendsManager:addFriends(friend_data)if not friend_data thenreturnendif not friend_data.user_id thenreturnend_friends_map[friend_data.user_id]=FriendStruct.new(friend_data)DataCenter:sendMsg(DataEvent.FRIEND.FRIENDS, param)endfunction FriendsManager:getFriendsData()if not _friends_map thenreturnendlocal friends_data =self:setDateToList(_friends_map)if not friends_data thenreturnendlocal friends_sort_data=nilif #friends_data >=2 thenfriends_sort_data=self:setSortData(friends_data)elsefriends_sort_data=friends_dataendreturn friends_sort_dataendfunction FriendsManager:removeFriends(friend_tag)if not _friends_map thenreturnend_friends_map[friend_tag]=nilendfunction FriendsManager:removeAllFriends()if not _friends_map thenreturnend_friends_map={}endfunction FriendsManager:getFriendByTag(friend_tag)if not _friends_map thenreturnendif not _friends_map[friend_tag] thenreturnendreturn _friends_map[friend_tag]endfunction FriendsManager:getFriendNameByTag(friend_tag)if not _friends_map thenreturnendif not _friends_map[friend_tag] thenreturnendreturn _friends_map[friend_tag]:getUserName()end-- function FriendsManager:getFriendAgeByTag(friend_tag)-- if not _friends_map then-- return-- end-- if not _friends_map[friend_tag] then-- return-- end-- return _friends_map[friend_tag]:getUserAge()-- end-- function FriendsManager:getFriendLevelByTag(friend_tag)-- if not _friends_map then-- return-- end-- if not _friends_map[friend_tag] then-- return-- end-- return _friends_map[friend_tag]:getUserLevel()-- endfunction FriendsManager:removeFriendNameByTag(friend_tag)if not _friends_map thenreturnendif not _friends_map[friend_tag] thenreturnendreturn _friends_map[friend_tag]:removeUserName()endfunction FriendsManager:setSortData(friend_data)if not friend_data thenreturnendtable.sort(friend_data,function(a,b)return (a.user_id<b.user_id) end)return friend_dataendfunction FriendsManager:setDateToList(_friends_map)if not _friends_map thenreturnendlocal friend_data_list={}for index,friend_data in pairs(_friends_map) dotable.insert(friend_data_list,friend_data)endreturn friend_data_listendfunction FriendsManager:updateData(data)if not data thenreturnendif not _friends_map thenreturnendif not data.user_id thenreturnendif not _friends_map[data.user_id] thenself:addFriends(data)end_friends_map[data.user_id]:updateData(data)endfunction FriendsManager:onRelease()_friends_map={}_invite_friends_map={}end
UIFriendLayer
local scheduler = require("framework.scheduler")require("app.Test.FriendsManager")local ListViewTool=require("app.Test.ListViewTool")UIFriendLayer=class("UIFriendLayer", function() return display.newLayer()end)UIFriendLayer.FILE_NAME= "csb/FriendLayer.csb"function UIFriendLayer:ctor()endfunction UIFriendLayer:onCreate() DataCenter:addRegister(self) self:init()endfunction UIFriendLayer:init() local replace_node=cc.uiloader:seekNodeByName(self,"replace_node") local outside_node=cc.CSLoader:createNode("csb/_outside_node.csb") local outside_node_panel=cc.uiloader:seekNodeByName(outside_node,"_outside_panel") outside_node:addTo(replace_node) self:setScale(2.5) self._haoyou_list=cc.uiloader:seekNodeByName(self,"haoyou_list") self._shengqing_list=cc.uiloader:seekNodeByName(self,"shengqing_list") self._chazhao_list=cc.uiloader:seekNodeByName(self,"chazhao_list") local haoyou_button=cc.uiloader:seekNodeByName(outside_node,"haoyou_btn") local shengqing_button=cc.uiloader:seekNodeByName(outside_node,"shengqing_btn") local chazhao_button=cc.uiloader:seekNodeByName(outside_node,"chazhao_btn") self._shengqing_list:setVisible(false) self._chazhao_list:setVisible(false) haoyou_button:addClickEventListener(handler(self, self.haoyouCallback)) shengqing_button:addClickEventListener(handler(self, self.shenqingCallback)) chazhao_button:addClickEventListener(handler(self, self.chazhaoCallback)) self._shengqing_list_panel=cc.uiloader:seekNodeByName(self,"shengqing_panel") local add_button=cc.uiloader:seekNodeByName(self,"add_btn") local remove_button=cc.uiloader:seekNodeByName(self,"remove_btn") add_button:addClickEventListener(handler(self, self.add)) remove_button:addClickEventListener(handler(self, self.remove)) self._chaxun_panel=cc.uiloader:seekNodeByName(self,"chaxun_panel") self._chaxun_panel:hide()---------------------输入框的键盘监听事件---------------- self._shuru_text=cc.uiloader:seekNodeByName(self,"shuru_Txf") local function onkeyPressed(keycode, event) print("keypress") if keycode == cc.KeyCode.KEY_BACKSPACE then local str = self._shuru_text:getStringValue() str = string.sub( str, 0, string.len( str ) - 1 ) self._shuru_text:setString( str ) end end local text_layer=display.newLayer() text_layer:addTo(self) local keyListener = cc.EventListenerKeyboard:create() keyListener:registerScriptHandler(onkeyPressed,cc.Handler.EVENT_KEYBOARD_PRESSED) text_layer:getEventDispatcher():addEventListenerWithSceneGraphPriority(keyListener,text_layer)---------------------------------------------------------------------------------------- local chaxun_button=cc.uiloader:seekNodeByName(self,"chaxun_btn") chaxun_button:addClickEventListener(handler(self,self.serch_user)) self._shengqing_list_panel_map={} self._haoyou_panel={} self._event_tag=false self._haoyou_list_panel=cc.uiloader:seekNodeByName(self,"haoyou_panel") self._list_tool = ListViewTool:new() self._friend_list_tool=ListViewTool:new()endfunction UIFriendLayer:destory()DataCenter:removeRegister(self)endfunction UIFriendLayer:haoyouCallback() self._haoyou_list:setVisible(true) self._chazhao_list:setVisible(false) self._shengqing_list:setVisible(false) self._chaxun_panel:hide()endfunction UIFriendLayer:shenqingCallback() self._shengqing_list:setVisible(true) self._haoyou_list:setVisible(false) self._chazhao_list:setVisible(false) self._chaxun_panel:hide()endfunction UIFriendLayer:chazhaoCallback() self._chazhao_list:setVisible(true) self._shengqing_list:setVisible(false) self._haoyou_list:setVisible(false) self._chaxun_panel:show()end--------------------------------------外来数据的加载---------------------function UIFriendLayer:add() local _shenqing_data={} local time=os.time() print("---------1111111-------"..time) for index=1,12 do _shenqing_data[index]={user_id=index,user_name=tostring("人物"..index),user_level=5} end FriendsManager:getInstance():addInviteFriends(_shenqing_data) end---------------------------------------数据进来的时候通知(接受通知)---------------function UIFriendLayer:onDataChange(eventTag, param) -- dump(eventTag) if eventTag== DataEvent.FRIEND.FRIENDSHENQ then print("-----------更新申请列表---------------------") self:upDateShenQ() elseif eventTag== DataEvent.FRIEND.FRIENDS then print("-----------更新好友列表--------------------") self:upDateHaoYou() endend-------------------------------------------通知之后的update--------------------- function UIFriendLayer:change(param)-- if self._haoyou_list:isVisible() then-- self:upDateHaoYou()-- elseif self._shengqing_list:isVisible() then-- self:upDateShenQ()-- else-- self:upDateChaZhao()-- end-- end--[-- 申请列表 需要几个要求-- 1.数据加进来的时候 插在列表的最后面-- 2.判断数据是否已经在列表里面有的无需加入 没有的话加入---- 好友列表 需要几个要去--1.数据从申请列表加进来的时候是否带ID的数据加入 还是自己按加入的顺序排序-- 如果是带ID的话 会导致工具类中加入的数据 如果没有ID项的话会出现空的panel-- (上述这条不可取)-- 如果是自己加入的话 FriendsManager 里面要进行数据的处理 --list里面动态的数据加载 需要滑动之后才会加载(有问题) 再往list里面加数据的时候会导致显示问题--获取滑动条的位置有问题--]--------------------------------------好友ListView-------------------------function UIFriendLayer:upDateHaoYou() print(" --------------好友列表正在更新。。。-------------------") local friends_data=FriendsManager:getInstance():getFriendsData() print("--------------打印申请列表接受后传递过来的数据-----------") dump(friends_data) local itemModel =self._haoyou_list_panel:clone() self._friend_list_tool:RegisterListView(self._haoyou_list) self._friend_list_tool:RegisterCallback(handler(self, self.upDataFriend)) self._friend_list_tool:tableShow(itemModel,friends_data) endfunction UIFriendLayer:upDataFriend(data,index) local user_id=data.user_id local user_name=FriendsManager:getInstance():getFriendNameByTag(user_id) print("----------好友列表的----------user_id.."..user_id) local clone_item=self._haoyou_list_panel:clone() clone_item:getChildByName("name_text"):setString(user_name) clone_item:getChildByName("id_text"):setString(user_id) return clone_itemend-----------------------------------好友申请ListView------------------------function UIFriendLayer:upDateShenQ() local sort_data=FriendsManager:getInstance():getInviteFriends() local itemModel=self._shengqing_list_panel self._list_tool:RegisterListView(self._shengqing_list) self._list_tool:RegisterCallback(handler(self,self.upDateCallBack)) self._list_tool:tableShow(itemModel, sort_data) print(os.time())end--------------------------------好友申请接受按钮回调---------------------------function UIFriendLayer:jieShouCallBack(sender) local jieshou_button_tag=sender:getTag() print("-----------申请列表接受按钮的tag-------------"..jieshou_button_tag) local temp_data=self._list_tool:getLayoutByTag(jieshou_button_tag) FriendsManager:getInstance():removeInviteFriendsById(jieshou_button_tag) self._list_tool:removeLayoutByTag(jieshou_button_tag) FriendsManager:getInstance():addFriends(temp_data) end--------------------------------查找ListView(此处只查找好友列表的好友并且是单个查询)----------------------------------function UIFriendLayer:upDateChaZhao() end-------------------------------好友申请ListView的回调--------------------------function UIFriendLayer:upDateCallBack(data,index) local user_id=data.user_id local user_name=FriendsManager:getInstance():getInviteNameById(user_id) print("--------申请列表的---------------user_name.."..user_name) local clone_item=self._shengqing_list_panel:clone() clone_item:getChildByName("user_name_text"):setString(user_name) clone_item:getChildByName("user_id_text"):setString(user_id) self._jieshou_button=clone_item:getChildByName("jieshou_btn") self._jieshou_button:setTag(index) self._jieshou_button:addClickEventListener(handler(self,self.jieShouCallBack)) self._remove_shenqing_btn=clone_item:getChildByName("del_btn") self._remove_shenqing_btn:setTag(index) self._remove_shenqing_btn:addClickEventListener(handler(self,self.delItemCallBack)) return clone_itemend-------------------------------好友申请删除好友请求按钮的回调--------------------------------function UIFriendLayer:delItemCallBack(sender) local remove_button_tag=sender:getTag() print("----删除按钮的tag------"..remove_button_tag) FriendsManager:getInstance():removeFriends(remove_button_tag) self._list_tool:removeLayoutByTag(remove_button_tag)end--------------------------------外来接口移除数据----------------------------------------------function UIFriendLayer:remove() local user_id=1 FriendsManager:getInstance():removeFriends(user_id) dump(self._list_tool)end----------------------------------查询ListView中查询按钮的回调------------------------------function UIFriendLayer:serch_user() local string_value=self._shuru_text:getString() local user_name=FriendsManager:getInstance():getFriendNameByTag(tonumber(string_value)) if user_name then self._chazhao_panel=self.shengqing_list_panel:clone() self._chazhao_panel:getChildByName("user_name_text"):setString(tostring(user_name)) self._chazhao_list:removeAllChildren() self._chazhao_list:pushBackCustomItem(self._chazhao_panel) endend-----------------------------------UIFriendLayer的刷新------------------------------------function UIFriendLayer:update(dt)end
ListViewTool 的实现
local scheduler = require(cc.PACKAGE_NAME .. ".scheduler")local ListViewTool=class("ListViewTool")ListViewTool._list_view=nilListViewTool._layout_list=nilListViewTool._callback=nilListViewTool._show_layout=nilfunction ListViewTool:ctor() self._layout_list={} self._show_layout={}end-- 创建空白的panelfunction ListViewTool:createLayout(index,data) local layout= ccui.Layout:create() layout:setContentSize(self._gridSize) layout:setVisible(true) layout:setPosition(cc.p(0, 0)) self._layout_list[index]=layout layout.data=data layout:addTo(self._list_view) layout:setTag(index) end-- -- function ListViewTool:RegisterListView(list) self._list_view=listendfunction ListViewTool:RegisterCallback(callback) self._callback=callbackendfunction ListViewTool:setData(data) dump(data) local layout_list_num=table.nums(self._layout_list) print("--------------列表中数据的个数-------------------"..layout_list_num) local data_num=table.nums(data) print("--------------数据的总数------------------"..data_num) for index=1,data_num do if self._layout_list[index] then self._layout_list[index].data=data[index] end end if data_num>=layout_list_num then for index=1,data_num do if not self._layout_list[index] then print("-------列表中加载第几条------"..index) self:createLayout(index,data[index]) end end else for index=data_num,layout_list_num do if self._layout_list[index] then print("-------多余的item移除------------") self:removeLayoutByTag(index) end endend self:update() self:upDateShowItem()endfunction ListViewTool:getLayoutByTag(layout_tag) if not self._layout_list then return end if not self._layout_list[layout_tag] then return end if not self._layout_list[layout_tag].data then return end return self._layout_list[layout_tag].dataendfunction ListViewTool:tableShow(itemModel,data) --参数说明 模型 数据 if data == nil or itemModel == nil then return end self._gridSize = itemModel:getContentSize() self:setData(data) dump(self._layout_list) self._list_view:refreshView() self._list_view:addScrollViewEventListener(handler(self, self.listViewCallBack))endfunction ListViewTool:getShowNum() local size = self._list_view:getContentSize() local direction = self._list_view:getDirection() local offset = self._list_view:getItemsMargin() local showNum = 0 if direction == cc.SCROLLVIEW_DIRECTION_HORIZONTAL then offset = offset + self._gridSize.width showNum = math.ceil(size.width / offset) + 4 return showNum elseif direction == cc.SCROLLVIEW_DIRECTION_VERTICAL then offset = offset + self._gridSize.height showNum = math.ceil(size.height / offset) + 4 return showNum else return endendfunction ListViewTool:listViewCallBack() self:update() end function ListViewTool:getCurCount() local cur_index=0 local innerSize = self._list_view:getInnerContainerSize() local pos=cc.p(self._list_view:getInnerContainer():getPosition()) local offset = self._list_view:getItemsMargin() local direction=self._list_view:getDirection() if direction == cc.SCROLLVIEW_DIRECTION_HORIZONTAL then cur_index=math.ceil((innerSize.width-math.abs(pos.x))/(offset + self._gridSize.width)) elseif direction == cc.SCROLLVIEW_DIRECTION_VERTICAL then print("--------------y轴上的位置:"..pos.y) cur_index=math.ceil((innerSize.height-math.abs(pos.y))/(offset + self._gridSize.height)) end return cur_indexendfunction ListViewTool:releaseItemData(layout_index,layout) if layout._isRelease then return end print('--release item in row--'..layout_index) layout._isInit = false layout._isRelease = true self._show_layout[layout_index]=nilendfunction ListViewTool:loadItemData(layout_index,layout) if layout._isInit then return end print('--load layout in row--'..layout_index) layout._isInit = true layout._isRelease = false -- layout:setVisible(true) local temp_node=self._callback(layout.data,layout_index) temp_node:addTo(layout) self._show_layout[layout_index]=layoutendfunction ListViewTool:upDateShowItem()local cur_index=self:getCurCount() print("---------当前显示第几项-------------"..cur_index) local showNum=self:getShowNum() local topItem=cur_index-showNum+2 local bottomItem=cur_index+2 for layout_index,layout in pairs(self._layout_list) do if layout_index<=bottomItem and layout_index>=topItem then print("--------需要加载第几条-----------"..layout_index) self:loadItemData(layout_index,layout) else self:releaseItemData(layout_index,layout) end endendfunction ListViewTool:update(dt) print("----------------update-----------------")self:upDateShowItem()endfunction ListViewTool:removeLayoutByTag(layout_tag) if not self._layout_list then return end if not self._layout_list[layout_tag] then return end self._layout_list[layout_tag]:removeFromParent() self._layout_list[layout_tag]=nil dump(self._layout_list) print("---------列表中还剩余几条数据------------"..table.nums(self._show_layout))endfunction ListViewTool:clearUp() self:removeAllItems() self._layout_list = {} end function ListViewTool:getShowItems() return self._show_layout or {} end return ListViewTool
-----------------------
其实好友系统很简单 具体可以参考上面来写 也可以进行扩展 添加其他功能 例如点击好友头像 出现一大堆的功能 具体需求依照策划来进行
0 0
- cocos2d-lua 手游之好友系统
- cocos2d-lua 手游之RichText的改写
- Cocos2d-x 手游聊天系统Demo实现(Lua实现)
- Cocos2d-x 手游聊天系统Demo实现(Lua实现)
- Cocos2d-x 手游聊天系统Demo实现(Lua实现)
- 关于cocos2d-x 手游lua文件加密的解决方案,cocos2d-x lua
- Cocos2d-x 手游聊天系统需求分析
- cocos-lua 手游之游戏新手引导
- 【Cocos2d-x 手游研发小技巧】图片资源加密,Lua文件加密
- 【cocos2d-x 手游研发小技巧(7)图片资源加密,Lua文件加密】
- 【cocos2d-x 手游研发小技巧(7)图片资源加密,Lua文件加密】
- Cocos2d-Lua之函数
- Cocos2d-Lua之赋值
- Cocos2d-Lua之table
- Cocos2d-Lua之string
- Cocos2d-Lua之随机数
- cocos2d手游之微信分享SDK接入指南
- cocos2d-x之写自己三国手游(一)
- 在Android Studio中读取properties文件
- linux运维之ps、netstat、nmap和pmap
- jstl c:choose><c:when><c:otherwise>标签
- C语言变量长度在32位和64位处理器上的关系
- JavaScript事件代理和委托
- cocos2d-lua 手游之好友系统
- 关于C语言头文件包含的常用函数介绍
- C/C++创建动态数组
- MySql 如何查询某一天内的数据
- Eclipse和PyDev搭建完美Python开发环境(Windows篇)
- IOS开发地区列表
- 程序员最容易犯的几个技术上的错误
- 龟兔赛跑
- HDU 1128 self numbers