cocos2dx lua 数据存储
2017-03-01 09:25
232 查看
最近项目中使用数据存储,需要存储table表,不能简单的userDefault来存储,也不合适(这里就不记录了,网上多的是)
第一种 :用json文件存储
实际上用io操作,封装成了一个方法
第二种 :用plist文件存储
这种我只做了读取,写的方法还有点问题,写入成功了但是值好像没保存下来(以后改正)
下面是我的utilJson.lua文件
注意:在ios系统下 数据只能存放在Documents路径下 才能允许读取并写入
获得Documents路径方法local writablePath = cc.FileUtils:getInstance():getWritablePath()
第一种 :用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 data end
-- 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() end end
第二种 :用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 end end
下面是我的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")
end
end
--读取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 data end
-- 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() end end
-- 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
end
end
return utilJson
注意:在ios系统下 数据只能存放在Documents路径下 才能允许读取并写入
获得Documents路径方法local writablePath = cc.FileUtils:getInstance():getWritablePath()
相关文章推荐
- cocos2dx lua数据存储
- 【iOS-cocos2d-X 游戏开发之六】使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据编码!
- 【COCOS2DX-LUA 脚本开发之十一】C/C++与Lua之间进行数据函数交互 推荐
- cocos2dx 3.x 中 Lua socket 和 node.js 利用scoket互相通信读写二进制数据
- Cocos2dx移植到Android报错问题,原因getIntegerForKey数据存储问题
- cocos2dx lua Node节点 私有数据存取
- 【iOS-cocos2d-X 游戏开发之六】使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据编码!
- Cocos2dx-C++和Lua数据通讯
- Cocos2d-X 学习笔记 16 使用Base64算法对Cocos2dX自带CCUserDefault游戏存储数据编码加密
- cocos2dx基础篇(20)——数据存储CCUserDefault
- quick-lua中使用本地数据存储
- 和屌丝一起学cocos2dx-数据存储
- 【COCOS2DX-LUA 脚本开发之十一】C/C++与Lua之间进行数据函数交互以及解决“PANIC: unprotected error in call to Lua API (attempt t
- 【COCOS2DX-LUA 脚本开发之十】使用Lua CJSON库进行encode与decode操作完成对Json数据转化
- 【cocos2dx】在Lua中使用CJSON库进行Json数据转化
- 【COCOS2DX-LUA 脚本开发之十】使用Lua CJSON库进行encode与decode操作完成对Json数据转化
- cocos2dx xml 数据存储
- cocos2dx:数据存储-静态数据
- 【COCOS2DX-LUA 脚本开发之十一】C/C++与Lua之间进行数据函数交互以及解决“PANIC: unprotected error in call to Lua API (attempt t
- cocos2d-x lua table数据存储