Quick-Cocos2d-x v3.3里面提供了两种长连接WebSockets、SocketTCP,这里说一下SocketTCP的用法。


1
2
3
localnet=require( "framework.cc.net.init" )
localByteArray=require( "framework.cc.utils.ByteArray" )
require( "framework.cc.utils.bit" )


-- 网络初始化,添加侦听函数

3
4
5
6
7
8
9
10
11
12
13
14
15
16
functionscnet.init()
local time =net.socketTCP.getTime()
print( "sockettime:" .. )
localsocket=net.socketTCP. new ()
socket:setName( "HeroGameTcp" )
socket:setTickTime(1)
socket:setReconnTime(6)
socket:setConnFailTime(4)
socket:addEventListener(net.socketTCP.EVENT_DATA,scnet.receive)
socket:addEventListener(net.socketTCP.EVENT_CLOSE,scnet.tcpClose)
socket:addEventListener(net.socketTCP.EVENT_CLOSED,scnet.tcpClosed)
socket:addEventListener(net.socketTCP.EVENT_CONNECTED,scnet.tcpConnected)
socket:addEventListener(net.socketTCP.EVENT_CONNECT_FAILURE,scnet.error)
scnet.socket=socket
end

--发送数据给服务器

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
functionscnet.send(msgid,data)
--encodeData此方法是根据需要发送的数据安装与服务器定义好的消息格式去write
local_ba=scnet.encodeData(msgid,data)
"scnet.send_ba" ,_ba:getLen())
if not_bathen
"发送数据出错了......." ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,msgid)
return
end
_ba:setPos(1)
localbyteList={}
localbyteCount=0
--把数据读出来,加密
for i=1,#_ba._buf do
localtmpBit=string.byte(_ba:readRawByte())
byteCount=byteCount+tmpBit
tmpBit=bit.band(tmpBit+80,255)
tmpBit=bit.band(bit.bnot(bit.band(tmpBit,255)),255)
byteList<i>=tmpBit
end
byteCount=byteCount%256
--最后再组成一个新的ByteArray
localresult=ByteArray. (ByteArray.ENDIAN_BIG)
result:writeShort(_ba:getLen()+3)
result:writeByte(byteCount)
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,#byteList do
result:writeByte(byteList<i>)
end
--把数据发送给服务器
scnet.socket:send(result:getPack())
-- 根据messageid来确定数据格式
functionscnet.encodeData(msgid,monospace!important; font-size:1em!important; min-height:auto!important; color:black!important; background:none!important">msgidthen
localba=ByteArray. (ByteArray.ENDIAN_BIG)
localfmt=InfoUtil:getMsgFmt(msgid)--此处为读取消息格式看下面的MessageType里面会有定义
ba:writeStringUShort( "token" )--此处为用户token,没有就为 "" ,此处可以判断用户是否重新登陆啊等等.......
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,#fmt do
scnet.writeData(ba,fmt<i>,data)
localbaLength=ba:getLen()
localbt=ByteArray. (ByteArray.ENDIAN_BIG)
bt:writeShort(baLength+4)--2为messagelength2为messagetype
bt:writeShort(msgid)
bt:writeBytes(ba)
return bt
end

-- write 数据

28
functionscnet.writeData(ba,msg_type,monospace!important; font-size:1em!important; min-height:auto!important; background:none!important">
localkey=msg_type.key
"scnet.writeData" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, "key" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,key)
keyanddata[key]then
local_type=msg_type[ "fmt" ]
type(_type)== "string" then
_type== then
ba:writeStringUShort(data[key])
elseif_type== "number" then
ba:writeLuaNumber(data[key])
"int" then
ba:writeInt(data[key])
"short" then
ba:writeShort(data[key])
end
else
ba:writeShort(#data[key])
k,vinpairs(data[key]) do
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,#_type do
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,_type<i>,v)
end
end
end
else
"找不到对应的key" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,msg_type.key,data)
end
-- 读取数据

-- 接收消息

12
functionscnet.receive(event)
(ByteArray.ENDIAN_BIG)
ba:writeBuf(event.data)
ba:setPos(1)
--有连包的情况,所以要读取数据
while ba:getAvailable()<=ba:getLen() scnet.decodeData(ba)
ba:getAvailable()==0then
break
end
end
-- 消息数据解析
24
functionscnet.decodeData(ba)
locallen=ba:readShort()--读数据总长度
localtotal=ba:readByte()--一个用于验证的数子
localbyteList={}
localtmpTotal=0
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,len-3 do --去除前两个长度
localtmpBit=ba:readByte()
localenByte=scnet.decodeByte(tmpBit)
tmpTotal=tmpTotal+enByte
byteList<i>=enByte
end
(ByteArray.ENDIAN_BIG)
do
result:writerawByte(string. char (byteList<i>))
end
result:setPos(1)
(tmpTotal%256)==totalthen
scnet.decodeMsg(result)
else
"scnet.decodeDatatotalerror" )
end
-- 根据格式解析数据
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
functionscnet.decodeMsg(byteArray)
localrData={}
locallen=byteArray:readShort()
localmsgid=byteArray:readShort()
localroleString=byteArray:readStringUShort()
localfmt=InfoUtil:getMsgFmt(msgid)
do
scnet.readData(byteArray,rData)
end
rData[ "result" ]~=0then
"resulthandlerishere" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,rData[key])
return
else
NetManager:receiveMsg(msgid,rData)
end
--readData
functionscnet.readData(ba,data)
localkey=msg_type.key
keythen
data[key]=data[key]or{}
]
then
then
data[key]=ba:readStringUShort()
then
data[key]=ba:readLuaNumber()
then
data[key]=ba:readInt()
then
data[key]=ba:readShort()
end
key== then--当结果不为零的时候,说明有错误
data[key]~=0then
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,data[key])
return
end
end
else
local_len=ba:readShort()--读取数组长度
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,_len do
localtmp={}
j=1,153)!important; background:none!important">do
scnet.readData(ba,_type[j],tmp)
end
table.insert(data[key],tmp)
end
end
else
"找不到对应的keyscnet.readData" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,data)
end
-- 数据解密
5
functionscnet.decodeByte(byte)
localtmp=bit.band(bit.bnot(bit.band(byte,255)
tmp=bit.band((tmp+256-80),255)
tmp
消息格式
46
--发送
MsgFmt[ "1001" ]={
{
key= "list" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
fmt={
{
"id" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,fmt= "int"
},
{
"name" "string"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"level" "int"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"sex" "int"
}
}
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"userid" "int"
}
}
--返回
"5001" ]={
{
"int"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
fmt={
{
"int"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"string"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"int"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
{
"int"
}
}
}
}

网络管理NetManager

管理网络的发送与接收

58
59
--初始化
functionNetManager:init()
self._listenerList={}
scnet.init()
scnet.connect(host,port)
end
--注册消息
--注册之后接受到服务器消息之后进行广播,谁注册,谁相应
functionNetManager:registerMsg(msgid,callBack)
self._listenerList[msgid]=self._listenerList[msgid]or{}
localisExist=self:findCallBack(msgid,callBack)
notisExistthen
table.insert(self._listenerList[msgid],callBack)
--移除注册消息
functionNetManager:removeRegister(msgid,callBack)
self._listenerList[msgid]then
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,vinpairs(self._listenerList) do
v==callBackthen
self._listenerList[msgid][k]=nil
end
end
end
--发送消息
--整理数据发送给服务器
functionNetManager:sendMsg(msgid,data)
scnet.send(msgid,data)
end
--接受消息
--派发事件(数据)
functionNetManager:receiveMsg(msgid,data)
self._listenerList[msgid]then
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,vinpairs(self._listenerList[msgid]) do
v(data)
end
end
end
functionNetManager:findCallBack(msgid,callBack)
do
v==callBackthen
return true
end
end
false
test
19
--监听事件
functionMainScene:onEnter()
NetManager:registerMsg(MsgType.SC_LOGIN,handler(self,self.receiveHandler))
end
--移除坚挺
functionMainScene:onExit()
NetManager:removeRegister(MsgType.SC_LOGIN,self.receiveHandler))
end
--发送数据,根据MsgFmt构造数据
localdata={}
data.list={}
table.insert(data.list,{id=1001,name= "小房" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,level=1,sex=1})
"小田" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,level=11,sex=2})
"2222" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,level=21,sex=1})
"3333" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,level=31,sex=2})
data.userid=10001
NetManager:sendMsg(MsgType.CS_LOGIN,data)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


如何使用CCRenderTexture创建动态纹理 Cocos2d-x 2 1 4
    本文实践自 RayWenderlich、Ali Hafizji 的文章《How To Create Dynamic Textures with CCRenderTexture in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建实时纹理、如何用Gimp创建无缝拼接纹
Cocos-code-ide使用入门学习
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@163.com微信公众号:HopToad 欢迎转载,转载标注出处:http://blog.csdn.netotbaron/article/details/424343991.  软件准备 下载地址:http://cn.cocos2d-x.org/download 2.  简介2.1         引用C
Cocos2D-x-3.0 编译(Win7)
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从Cocos2D-x官网上下载,进入网页http://www.cocos2d-x.org/download,点击Cocos2d-x以下的Download  v3.0,保存到自定义的文件夹2:从python官网上下载。进入网页https://www.python.org/downloads/,我当前下载的是3.4.0(当前最新
quick-cocos2d-x实例之挑战记忆极限设计文档
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《最强大脑》娱乐节目。将2048改造成一款挑战玩家对数字记忆的小游戏。邮箱:appdevzw@163.com微信公众号:HopToadAPK下载地址:http://download.csdn.net/detailotbaron/8446223源码下载地址:http://download.csdn.net/
Cocos2d-x 3 X CMake MinGW版本编译运行
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试以QtCreatorIDE来进行CMake构建。Cocos2d-x3.X地址:https://github.com/cocos2d/cocos2d-x1.打开QtCreator,菜单栏→"打开文件或项目...",打开cocos2d-x目录下的CMakeLists.txt文件;2.弹出CMake向导,如下图所示:设置
vs 2013 编译cocos2d-x-3.9
 下载地址:链接:https://pan.baidu.com/s/1IkQsMU6NoERAAQLcCUMcXQ提取码:p1pb下载完成后,解压进入build目录使用vs2013打开工程设置平台工具集,打开设置界面设置: 点击开始编译等待编译结束编译成功在build文件下会出现一个新文件夹Debug.win32,里面就是编译
Cocos2d-x CCControlPotentiometer之圆形音量button及特效
1. 圆形音量button事实上作者的本意应该是叫做“电位计button”。可是我觉得它和我们的圆形音量button非常像,所以就这么叫它吧~先看效果:好了,不多解释,本篇到此为止。(旁白: 噗。就这样结束了?)啊才怪~我们来看看代码:[cpp] viewplaincopyprint?CCContro
Cocos2d-x入门教程二简单的静态显示对象
原文链接:http://www.cnblogs.com/physwf/archive/2013/04/26/3043912.html为了进一步深入学习贯彻Cocos2d,我们将自己写一个场景类,但我们不会走的太远,凡是都要循序渐进,哪怕只前进一点点,那也至少是前进了,总比贪多嚼不烂一头雾水的好。在上一节中我们建
  • • 如何使用CCRenderTexture创建动态纹理 …
  • • Cocos-code-ide使用入门学习
  • • Cocos2D-x-3.0 编译(Win7)
  • • Cocos2d-x 2 0 在Windows平台下的使用
  • • quick-cocos2d-x实例之挑战记忆极限设计…
  • • Cocos2d-x 3 X CMake MinGW版本编译运行
  • • vs 2013 编译cocos2d-x-3.9
  • • cocos2d-x游戏开发系列教程-超级玛丽01…
  • • Cocos2d-x CCControlPotentiometer之圆…
  • • Cocos2d-x入门教程二简单的静态显示对象
  • • cocos2d-x中CCScale9Sprite的另一种实现
  • • Cocos2d-x v2.2.2版本+Win7+VS2010环境…
  • • Ubuntu14.04+eclipse下cocos2d-x3.0正式…
  • • 分别基于WIN32 API界面编程和Cocos2d-x…
  • • Cocos2d-x 开发小记二:控件
  • • Cocos2d-x 开发小记一:基本动作
  • • 买Cocos2d-x视频课程送纸质图书
  • • ‎Cocos2d-x 学习笔记(11.10) Spawn

Quick-cocos2d-x v3.3 SocketTCP链接的更多相关文章

  1. 用canvas做一个DVD待机动画的实现代码

    这篇文章主要介绍了用canvas做一个DVD待机动画的实现代码的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  2. 使用Html5多媒体实现微信语音功能

    这篇文章主要介绍了使用Html5多媒体实现微信语音功能,需要的朋友可以参考下

  3. HTML5自定义视频播放器源码

    这篇文章主要介绍了HTML5自定义视频播放器源码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

  4. Html5 滚动穿透的方法

    这篇文章主要介绍了Html5 滚动穿透的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  5. HTML5自定义mp3播放器源码

    这篇文章主要介绍了HTML5自定义mp3播放器源码,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

  6. 详解HTML5中CSS外观属性

    这篇文章主要介绍了HTML5中CSS外观属性的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,,需要的朋友可以参考下

  7. CSS中实现动画效果-附案例

    这篇文章主要介绍了 CSS中实现动画效果并附上案例代码及实现效果,就是CSS动画样式处理,动画声明需要使用@keyframes name,后面的name是人为定义的动画名称,下面我们来看看文章的具体实现内容吧,需要的小伙伴可以参考一下

  8. html5默认气泡修改的代码详解

    这篇文章主要介绍了html5默认气泡修改的代码详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  9. 基于Html5 canvas实现裁剪图片和马赛克功能及又拍云上传图片 功能

    这篇文章主要介绍了基于Html5 canvas实现裁剪图片和马赛克功能及又拍云上传图片 功能,需要的朋友可以参考下

  10. Html5移动端适配IphoneX等机型的方法

    这篇文章主要介绍了Html5移动端适配IphoneX等机型的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

随机推荐

  1. 【cocos2d-x 3.x 学习笔记】对象内存管理

    Cocos2d-x的内存管理cocos2d-x中使用的是上面的引用计数来管理内存,但是又增加了一些自己的特色。cocos2d-x中通过Ref类来实现引用计数,所有需要实现内存自动回收的类都应该继承自Ref类。下面是Ref类的定义:在cocos2d-x中创建对象通常有两种方式:这两中方式的差异可以参见我另一篇博文“对象创建方式讨论”。在cocos2d-x中提倡使用第二种方式,为了避免误用第一种方式,一般将构造函数设为protected或private。参考资料:[1]cocos2d-x高级开发教程2.3节[

  2. 利用cocos2dx 3.2开发消灭星星六如何在cocos2dx中显示中文

    由于编码的不同,在cocos2dx中的Label控件中如果放入中文字,往往会出现乱码。为了方便使用,我把这个从文档中获取中文字的方法放在一个头文件里面Chinese.h这里的tex_vec是cocos2dx提供的一个保存文档内容的一个容器。这里给出ChineseWords,xml的格式再看看ChineseWord的实现Chinese.cpp就这样,以后在需要用到中文字的地方,就先include这个头文件然后调用ChineseWord函数,获取一串中文字符串。

  3. 利用cocos2dx 3.2开发消灭星星七关于星星的算法

    在前面,我们已经在GameLayer中利用随机数初始化了一个StarMatrix,如果还不知道怎么创建星星矩阵请回去看看而且我们也讲了整个游戏的触摸事件的派发了。

  4. cocos2dx3.x 新手打包APK注意事项!

    这个在编译的时候就可以发现了比较好弄这只是我遇到的,其他的以后遇到再补充吧。。。以前被这两个问题坑了好久

  5. 利用cocos2dx 3.2开发消灭星星八游戏的结束判断与数据控制

    如果你看完之前的,那么你基本已经拥有一个消灭星星游戏的雏形。开始把剩下的两两互不相连的星星消去。那么如何判断是GameOver还是进入下一关呢。。其实游戏数据贯穿整个游戏,包括星星消除的时候要加到获得分数上,消去剩下两两不相连的星星的时候的加分政策等,因此如果前面没有做这一块的,最好回去搞一搞。

  6. 利用cocos2dx 3.2开发消灭星星九为游戏添加一些特效

    needClear是一个flag,当游戏判断不能再继续后,这个flag变为true,开始消除剩下的星星clearSumTime是一个累加器ONE_CLEAR_TIME就是每颗星星消除的时间2.连击加分信息一般消除一次星星都会有连击信息和加多少分的信息。其实这些combo标签就是一张图片,也是通过控制其属性或者runAction来实现。源码ComboEffect.hComboEffect.cpp4.消除星星粒子效果消除星星时,为了实现星星爆裂散落的效果,使用了cocos2d提供的粒子特效引擎对于粒子特效不了

  7. 02 Cocos2D-x引擎win7环境搭建及创建项目

    官网有搭建的文章,直接转载记录。环境搭建:本文介绍如何搭建Cocos2d-x3.2版本的开发环境。项目创建:一、通过命令创建项目前面搭建好环境后,怎样创建自己的Cocos2d-x项目呢?先来看看Cocos2d-x3.2的目录吧这就是Cocos2d-x3.2的目录。输入cocosnew项目名–p包名–lcpp–d路径回车就创建成功了例如:成功后,找到这个项目打开proj.win32目录下的Hello.slnF5成功了。

  8. 利用cocos2dx 3.2开发消灭星星十为游戏添加音效项目源码分享

    一个游戏,声音也是非常的重要,其实cocos2dx里面的简单音效引擎的使用是非常简单的。我这里只不过是用一个类对所有的音效进行管理罢了。Audio.hAudio.cpp好了,本系列教程到此结束,第一次写教程如有不对请见谅或指教,谢谢大家。最后附上整个项目的源代码点击打开链接

  9. 03 Helloworld

    程序都有一个入口点,在C++就是main函数了,打开main.cpp,代码如下:123456789101112131415161718#include"main.h"#include"AppDelegate.h"#include"cocos2d.h"USING_NS_CC;intAPIENTRY_tWinMain{UNREFERENCED_ParaMETER;UNREFERENCED_ParaMETER;//createtheapplicationinstanceAppDelegateapp;return

  10. MenuItemImage*图标菜单创建注意事项

    学习cocos2dx,看的是cocos2d-x3.x手游开发实例详解,这本书错误一大把,本着探索求知勇于发现错误改正错误的精神,我跟着书上的例子一起调试,当学习到场景切换这个小节的时候,出了个错误,卡了我好几个小时。

返回
顶部