cocos2dx lua 数据存储
来源:互联网 发布:数控镗床编程 编辑:程序博客网 时间:2024/05/20 04:29
最近项目中使用数据存储,需要存储table表,不能简单的userDefault来存储,也不合适(这里就不记录了,网上多的是)
第一种 :用json文件存储
实际上用io操作,封装成了一个方法
--读取json文件utilJson.LoadFile = function(filename) local file if filename == nil then file = io.stdin else local err file, err = io.open(filename, "rb") if file == nil then error(("Unable to read '%s': %s"):format(filename, err)) end end local data = file:read("*a") if filename ~= nil then file:close() end if data == nil then error("Failed to read " .. filename) end data = json.decode(data) return dataend
-- Function 保存文件utilJson.SaveFile = function(filename, data) --filename 全路径 data为lua中的table print("filename",filename) local file if filename == nil then file = io.stdout else local err file, err = io.open(filename, "wb") if file == nil then error(("Unable to write '%s': %s"):format(filename, err)) end end data = json.encode(data) file:write(data) if filename ~= nil then file:close() endend
第二种 :用plist文件存储
这种我只做了读取,写的方法还有点问题,写入成功了但是值好像没保存下来(以后改正)
--读取plist文件utilJson.getPlistDict = function(fileName) print("是否存在",cc.FileUtils:getInstance():isFileExist(fileName)) if cc.FileUtils:getInstance():isFileExist(fileName) then local plistFile = cc.FileUtils:getInstance():fullPathForFilename(fileName) -- local dict = cc.FileUtils:getInstance():getValueVectorFromFile(plistFile) --<array> local dict = cc.FileUtils:getInstance():getValueMapFromFile(plistFile) --<dict> return dict end end
--写入plist文件utilJson.savePlistDict = function(tbl,fileName) print("是否存在",cc.FileUtils:getInstance():isFileExist(fileName)) if cc.FileUtils:getInstance():isFileExist(fileName) then local dict = {} dict["data"] = tbl for k,v in pairs(tbl[1]) do print(k,v) end local reslut = cc.FileUtils:getInstance():writeValueMapToFile(dict,fileName) print("存成功",reslut) return reslut endend
下面是我的utilJson.lua文件
local utilJson = {}-- json工具local cJson = require("cjson") utilJson.decode = function (fileName) print("参数",fileName)local filePath = fileName local isExist = cc.FileUtils:getInstance():isFileExist(filePath) print(isExist) local t = cc.FileUtils:getInstance():getStringFromFile(filePath) if nil ~= t and "" ~= t then local jsonData = cJson.decode( t ) if jsonData == nil then print("Json error") end return jsonData else print("data was empty") endend--读取json文件utilJson.LoadFile = function(filename) local file if filename == nil then file = io.stdin else local err file, err = io.open(filename, "rb") if file == nil then error(("Unable to read '%s': %s"):format(filename, err)) end end local data = file:read("*a") if filename ~= nil then file:close() end if data == nil then error("Failed to read " .. filename) end data = json.decode(data) return dataend-- Function 保存文件utilJson.SaveFile = function(filename, data) --filename 全路径 data为lua中的table print("filename",filename) local file if filename == nil then file = io.stdout else local err file, err = io.open(filename, "wb") if file == nil then error(("Unable to write '%s': %s"):format(filename, err)) end end data = json.encode(data) file:write(data) if filename ~= nil then file:close() endend -- local tmpTable = shopLayer.getPlistDict("data/buyPersonBall.plist") -- for _,v in pairs(tmpTable) do -- for i,j in pairs(v) do -- print(i,j) -- end -- end -- tmpTable[1]["2"] = 5 -- shopLayer.savePlistDict(tmpTable,"data/buyPersonBall.plist") -- tmpTable = shopLayer.getPlistDict("data/buyPersonBall.plist") -- print("改变后的值",tmpTable[1]["2"]) --读取plist文件utilJson.getPlistDict = function(fileName) print("是否存在",cc.FileUtils:getInstance():isFileExist(fileName)) if cc.FileUtils:getInstance():isFileExist(fileName) then local plistFile = cc.FileUtils:getInstance():fullPathForFilename(fileName) -- local dict = cc.FileUtils:getInstance():getValueVectorFromFile(plistFile) --<array> local dict = cc.FileUtils:getInstance():getValueMapFromFile(plistFile) --<dict> return dict end end --写入plist文件utilJson.savePlistDict = function(tbl,fileName) print("是否存在",cc.FileUtils:getInstance():isFileExist(fileName)) if cc.FileUtils:getInstance():isFileExist(fileName) then local dict = {} dict["data"] = tbl for k,v in pairs(tbl[1]) do print(k,v) end local reslut = cc.FileUtils:getInstance():writeValueMapToFile(dict,fileName) print("存成功",reslut) return reslut endend return utilJson
注意:在ios系统下 数据只能存放在Documents路径下 才能允许读取并写入
获得Documents路径方法
local writablePath = cc.FileUtils:getInstance():getWritablePath()
0 0
- cocos2dx lua 数据存储
- cocos2dx lua 读取json数据
- cocos2dx-数据存储
- cocos2dx xml 数据存储
- cocos2dx:数据存储-UserDefault
- cocos2dX 之数据存储
- cocos2dx-数据存储
- [cocos2dx]数据存储 UserDefault
- cocos2dx:数据存储-静态数据
- cocos2dx数据存储 Userdefault数据存储实例
- quick cocos2dx 游戏数据存储
- cocos2dx UserDefault存储数据问题
- cocos2dx lua Node节点 私有数据存取
- cocos2dx lua http请求获取网络数据:
- cocos2dx lua图片获取像素数据
- cocos2dx lua http请求获取网络数据:
- lua中table存储数据
- quick lua 数据存储 GameState
- redis -windows版本官方下载地址
- android绘制圆角矩形
- java web学习
- 自定义控件原理
- 贪吃蛇游戏源码
- cocos2dx lua 数据存储
- NYOJ-990蚂蚁感冒
- rbac角色赋权
- 使用vue2.0 vue-router vuex 模拟ios7操作
- JS控制图片按比例缩放并不超过一定宽度和高度
- 设置请求不要有缓存
- beyond compare 无限试用
- 在selenium Webdriver中妙用JS
- wpf无边框的两种实现方式