怎么用cocos2d-x lua开发游戏

怎么用cocos2d-x lua开发游戏,第1张

protobuf Google的一个很好用的传输数据的封装 说实话Google的东西确实比较好用 所以我们前后端数据交换就用他了 不过Google没有对lua进行支持 还好社区有开源的大侠们贡献 找了所有关于lua protobuf 我只找到 云风的 pbc 修改相关cocos2d-x中的类可以正常使用。protoc-gen-lua 我在使用的时候 总是报截断数据 在修改后cocs2d-x中的类之后没有对protoc-gen-lua 进行测试是否是这个问题导致
1)集成 云风 云大侠的(博客)lua-pbc 标准c写的protobuf 具体看pbc的帮助很轻松集成
2) 生成pb文件(我自己写了个mac中批处理生成所有proto文件为pb文件)把pb 和proto文件都加入到项目资源中
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
#!/bin/sh
#pb = "pb"
for i in proto
do
#echo $i
#echo ${i%}"pb"
#echo ${i%}
#pbn = $i | cut -d
pbname=${i%}"pb"
#echo $pbn
#echo $pbname
protoc --descriptor_set_out $pbname $i
done
echo "finish"
也可以用命令行手动生成
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
protoc --descriptor_set_out aaapb aaaproto
3)本步骤可以忽略了,可以直接用io进行读取(Android是路径问题请看本文最下面解释)在lua中使用如下代码(我用的是cocos2d-x中绑定的CCFileUtils中的获取文件的方式,不过要手动用tolua++进行绑定到lua,可以参考我上个文章中的绑定方式,云大侠中的 用lua io形式获取在相关了解中不能跨平台所有就用这个了)
[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
local protobuf = require "protobuf"
local buffer = CCFileUtils:sharedFileUtils():getFileData("entity/p_resultpb","r",0)
-- print(buffer)
protobufregister(buffer)
4)本步骤可以忽略了,可以直接用io进行读取(Android是路径问题请看本文最下面解释) 上一步完成后我们要对提到的CCFileUtilscpp中的类进行修改 如果不修改读文件pb文件会时好时坏 原因是 读文件的时候结束总是添加多余字节我也不清楚这个问题 进行修改cocos2d-x中CCFileUtilscpp的下面方法中的读取数据后处理并在tolua++ 中添加下面方法绑定到lua层
修改CCFileUtilscp getFileData(const char pszFileName, const char pszMode,unsignedlong pSize)方法(在最后添加\0,保证字节不多余)如下代码
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
unsigned char CCFileUtils::getFileData(const char pszFileName, const char pszMode, unsigned long pSize)
{
unsigned char pBuffer = NULL;
CCAssert(pszFileName != NULL && pSize != NULL && pszMode != NULL, "Invalid parameters");
pSize = 0;
do
{
// read the file from hardware
std::string fullPath = fullPathForFilename(pszFileName);
FILE fp = fopen(fullPathc_str(), pszMode);
CC_BREAK_IF(!fp);

fseek(fp,0,SEEK_END);
pSize = ftell(fp);

fseek(fp,0,SEEK_SET);
pBuffer = new unsigned char[pSize];
pSize = fread(pBuffer,sizeof(unsigned char), pSize,fp);
fclose(fp);

} while (0);

if (pSize >0 && pBuffer[pSize] != '\0')
pBuffer[pSize] = '\0';

if (! pBuffer)
{
std::string msg = "Get data from file(";
msgappend(pszFileName)append(") failed!");

CCLOG("%s", msgc_str());
}
return pBuffer;
}
5)经过上一步骤lua层基本搞定可以创建本地的数据并encode成传输数据到服务器端了 如下代码
[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
local major = {
majorId = "795f94a9-3466-41b4-bf16-043ba8081fab"
}
local buffer = protobufencode("comsjwebprotoMajor", major)
6)我们客户端数据传输到服务器端 服务器端会返回数据给我们 同样我们接收的数据肯定也是protobuf数据了 用 protobufdecode进行解数据
[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
local t = protobufdecode("comsjwebprotoResult", request:getResponseString())--toluacast(eventdataCString))--toluacast(eventdataCString,"CCString"):getCString())
cclog(t)

print(tmajorgender)
print(tmajormajorId)
print(tuserusername)
7)上一步中的数据是服务器端过来的数据,不过在>创建列数据,填入c列数据,设置c列类型为txt,双击x轴坐标,在scale标签中设置步长为1,在tick label标签中设置type为“text from dataset”,dataset中填入c列地址。见下两图:

>我用VBA在EXCEL2010下做了段程序,可自动生成柏拉图。
以下是程序源代码
'数据区域的左上角位置常量
Const UpperLeftPos = "B4"
Const UpperPos = "B"
Const LeftPos = 4
Sub 绘制标准柏拉图()
Dim Range1 As Range
'选择数据区域,Range("B2")这个格子里放的是有效数据的行数,即分类的总个数
Set Range1 = Range(UpperLeftPos & ":" & Chr(Asc(UpperPos) + 1) & (LeftPos + Range("B2")) & "," & Chr(Asc(UpperPos) + 2) & LeftPos & ":" & Chr(Asc(UpperPos) + 2) & (LeftPos + Range("B2") + 1))
'插入柱形图
ActiveSheetShapesAddChartSelect
ActiveChartChartType = xlColumnClustered
ActiveChartSetSourceData Source:=Range1
'把百分比数据系列的图形格式变为“带数据值的折线图”,并画在由次要横坐标和次要纵坐标里
ActiveChartSeriesCollection(2)Select
ActiveChartSeriesCollection(2)AxisGroup = 2
ActiveChartSeriesCollection(2)ChartType = xlLineMarkers
ActiveChartSetElement (msoElementSecondaryCategoryAxisWithoutLabels)
ActiveChartSetElement (msoElementSecondaryCategoryAxisShow)
'把次要横坐标的值显示到刻度上,而不是刻度中间,并隐藏次要横坐标的显示
ActiveChartAxes(xlCategory, xlSecondary)Select
SelectionMajorTickMark = xlNone
SelectionTickLabelPosition = xlNone
ActiveChartAxes(xlCategory, xlSecondary)AxisBetweenCategories = False
'调整柱形图的显示,去掉相邻柱子的间距,并给每个柱子加外框
ActiveChartSeriesCollection(1)Select
ActiveChartChartGroups(1)GapWidth = 0
With SelectionFormatLine
Visible = msoTrue
ForeColorObjectThemeColor = msoThemeColorText1
ForeColorTintAndShade = 0
ForeColorBrightness = 0
End With
With SelectionFormatLine
Visible = msoTrue
Weight = 1
End With
'修改主要纵坐标,满值为累加和
ActiveChartAxes(xlValue)Select
ActiveChartAxes(xlValue)CrossesAt = 0
SelectionMajorTickMark = xlInside
ActiveChartAxes(xlValue)MinimumScale = 0
ActiveChartAxes(xlValue)MaximumScale = Range(Chr(Asc(UpperPos) + 3) & (LeftPos + Range("B2") + 1))
'修改次要纵坐标,满值为100%
ActiveChartAxes(xlValue, xlSecondary)Select
ActiveChartAxes(xlValue, xlSecondary)MaximumScale = 1
ActiveChartAxes(xlValue, xlSecondary)MinimumScale = 0
SelectionMajorTickMark = xlInside
ActiveChartAxes(xlValue, xlSecondary)CrossesAt = 1
SelectionTickLabelsNumberFormat = "0%"
'让百分比折线上的点显示出具体的数值
ActiveChartSeriesCollection(2)Select
ActiveChartSeriesCollection(2)ApplyDataLabels
ActiveChartSeriesCollection(2)DataLabelsSelect
SelectionPosition = xlLabelPositionRight
'隐去横向的主要网格线
ActiveChartAxes(xlValue)Select
ActiveChartAxes(xlValue)MajorGridlinesSelect
SelectionFormatLineVisible = msoFalse
'为图表添加标题,标题的内容放在Range("B1")格子里
ActiveChartSetElement (msoElementChartTitleAboveChart)
ActiveChartChartTitleSelect
ActiveChartChartTitleText = Range("B1")
End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/12694124.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-27
下一篇 2023-05-27

发表评论

登录后才能评论

评论列表(0条)

保存