本文主要是介绍quick-cocos2d-x 关卡数据的解析与更新,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
--解析场景表的数值
local levels_str=getFile("/Users/student/Desktop/bbbb/res/scene.txt")
self.levels_table = unserialize(levels_str)
for k,v in pairs(self.levels_table) do
if k == "scene_1" then
for i,j in pairs(v) do
print(i,j)
end
end
end
--解析关卡表的数值
local chapter_str=getFile("/Users/student/Desktop/bbbb/res/chapter.txt")
self.chapter_table = unserialize(chapter_str)
for k,v in pairs(self.chapter_table) do
if k == "Level_1" then
for i,j in pairs(v) do
print(i,j)
end
end
end
--更新关卡
function MainScene:UpdatechapterData(Level_1,LevelNumber,Unlock,starNumber,HighestScore)chapter={}
chapter.Level_1=Level_1
chapter.LevelNumber=LevelNumber
chapter.Unlock=Unlock
chapter.starNumber=starNumber
chapter.HighestScore=HighestScore
local table1=serialize(chapter)
write_content("/Users/student/Desktop/bbbb/res/level.txt",table1)
end
--更新场景
function MainScene:UpdateSceneData(scene_1,sceneNumber,chapterNumber,unLock,needstarNumber)
scene={}
scene.scene_1=scene_1
scene.sceneNumber=sceneNumber
scene.chapterNumber=chapterNumber
scene.unLock=unLock
scene.needstarNumber=needstarNumber
local table2=serialize(scene)
write_content("/Users/student/Desktop/bbbb/res/level.txt",table2)
end
必须添加的函数:
function serialize(obj)
local lua = ""
local t = type(obj)
if t == "number" then
lua = lua .. obj
elseif t == "boolean" then
lua = lua .. tostring(obj)
elseif t == "string" then
lua = lua .. string.format("%q", obj)
elseif t == "table" then
lua = lua .. "{\n"
for k, v in pairs(obj) do
lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n"
end
local metatable = getmetatable(obj)
if metatable ~= nil and type(metatable.__index) == "table" then
for k, v in pairs(metatable.__index) do
lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. ",\n"
end
end
lua = lua .. "}"
elseif t == "nil" then
return nil
else
error("can not serialize a " .. t .. " type.")
end
return lua
end
function unserialize(lua)
local t = type(lua)
if t == "nil" or lua == "" then
return nil
elseif t == "number" or t == "string" or t == "boolean" then
lua = tostring(lua)
else
error("can not unserialize a " .. t .. " type.")
end
lua = "return " .. lua
local func = loadstring(lua)
if func == nil then
return nil
end
return func()
end
function write_content( fileName,content )
local f = assert(io.open(fileName, 'w'))
f:write(content)
f:close()
end
function getFile(file_name)
print(file_name)
local f = assert(io.open(file_name, 'r'))
local string = f:read("*all")
f:close()
return string
end
这篇关于quick-cocos2d-x 关卡数据的解析与更新的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!