由于在开发过程中,会增加游戏中的暂停功能和下次继续等操作,这就需要保存当前状态下游戏中的各种参数
以便在下次开始游戏时直接提取保存的参数接着上次的玩
这就需要我们将各种参数保存在文件中再在下次加载游戏时提取出来
于是就设计到了文件的操作,下面就是封装在file.lua中的save方法和load方法
function save(fileName,dataName)
local gameFile=gearboxfile.open(fileName,"write");
local data=serialize(dataName);
gearboxfile.append(gameFile,data);
gearboxfile.close(gameFile);
end
function load(fileName)
local gameFile=gearboxfile.open(fileName,"read");
local data;
if gameFile~=-1 then
data=unserialize(gearboxfile.getItem(gameFile,0));
else
data=nil;
end
gearboxfile.close(gameFile);
return data;
end
aLabel={
a=1;
b=1;
}
bLable={
a={};
b={};
};
cNumber=1;
--------上以为要保存的数据,开始放表保存-----
saveData={
aData=aLabel;
bData=bLabel;
cData=cNumber;
};
save(gameSave,saveData);
--------保存完毕,下面是读取-------------
saveData=load(gameSave);
aLabel=saveData.aData;
bLabel=saveData.bData;
cNumber=saveData.cData;
--------完毕-------------
最后,在一些其他功能,如排行榜、上次游戏等等操作中,都会涉及这些文件操作,所以需要熟练掌握。