【Cocos2d-x Lua笔记六】游戏存储GameState

【Cocos2d-x Lua笔记六】游戏存储GameState,第1张

概述      GameState是Quick中的一个数据存储类,可以进行数据加密,数据校验(当数据被人为改变的时候会被检验出来)。       1.加载        在文件开头加载        local GameState=require(cc.PACKAGE_NAME .. ".cc.utils.GameState")        2.初始化      GameState.init(e

GameState是Quick中的一个数据存储类,可以进行数据加密,数据校验(当数据被人为改变的时候会被检验出来)。

1.加载

在文件开头加载

local GameState=require(cc.PACKAGE_name .. ".cc.utils.GameState")


2.初始化

  GameState.init(eventListener_,statefilename_,secretKey_)


是用于初始化GameState的,在场景的初始化中调用一次就行了。


eventListener_是载入或保存时调用的函数,接下来解析;
statefilename_是保存的文件名,如果留空或非字符串(string)则是默认的state.txt;
secretKey_是 校验文件时所用到的密钥,GameState保存的数据格式为{h = hash,s = s},s是我们要保存的数据(一个table),h则是要校验的一个md5码。如果secretKey_留空或为非字符串(string)则不加校验码, 直接保存数据。

3.eventListener(value)

此函数就是GameState.init中的第一个参数,value为一个table,此函数在载入或保存时都会被调用,相当于一个监听器。不同的情况下value的值会不一样。
注意:eventListener一定要有返回值。
如果在这个函数中载入数据时有异常发生,value值为{name = "load",errorCode = errorCode},name有两种值,"load"和"save",分别对应载入和保存;errorCode分为三种:
GameState.ERROR_INVALID_file_CONTENTS //不合法的文件内容,即取出来的内容不是一个table
GameState.ERROR_HASH_MISS_MATCH //文件被人为更改过
GameState.ERROR_STATE_file_NOT_FOUND //文件不存在
这个时候eventListener可返回nil。

4.加密数据
GAME_STATE_ENCRYPTION_XXTEA="abcd"
GAME_STATE_ENCRYPTION_OPERATION_SAVE="save"
GAME_STATE_ENCRYPTION_OPERATION_LOAD="load"

if param.name==GAME_STATE_ENCRYPTION_OPERATION_SAVE then                local str=Json.encode(param.values)                str=crypto.encryptXXTEA(str,GAME_STATE_ENCRYPTION_XXTEA)                returnValue={data=str}            elseif param.name==GAME_STATE_ENCRYPTION_OPERATION_LOAD then                local str=crypto.decryptXXTEA(param.values.data,GAME_STATE_ENCRYPTION_XXTEA)                returnValue=Json.decode(str)            end  

Json.encode()将表编码为 JsON 字符串

Json.decode()将 JsON 字符串解码为表
Json.decode()Json.decode()crypto.encryptXXTEA(plaintext,key)使用 XXTEA 算法加密内容
crypto.decryptXXTEA(ciphertext,key)使用 XXTEA 算法解密内容

5.保存,取出数据

GameState.load()
载入并返回数据,一般此方法只调用一些就行,在游戏加载前调用并保存到一个全局变量GameData。
GameState.save(newValues)
保存数据,newValues是一个table。GameState.init对应于保存一个文件,此文件的内容就是newValues,所以我们需要更新数据的时候应该改变上面的GameData,然后保存GameData。
GameState.getGameStatePath()
保存的文件的完整路径。

范例:

利用GameState保存声音开关的状态

local GameState=require(cc.PACKAGE_name .. ".cc.utils.GameState")require(cc.PACKAGE_name..".cc.ui.UIPushbutton")--创建游戏主页场景local HomeScene = class("HomeScene",function()    return display.newScene("HomeScene")end)GameData={}HomeScene.CHECKBox_button_IMAGES={    off="Images/menu_sound-sheet1.png",on="Images/menu_sound-sheet0.png",}HomeScene.PUSH_button_IMAGES={    normal="Images/day_start_btn.png",pressed="Images/day_start_btn_pressed.png",Disabled ="Images/day_start_btn_pressed.png",}function HomeScene:ctor()    self:addElement()    self:addMenuElement()endfunction HomeScene:addElement()    self.MainBg=display.newTilessprite("Background/day_buttom.png",cc.rect(display.left,display.bottom,display.wIDth,display.height)):addTo(self)    --align(target,anchorPoint,x,y)    self.MainBottomBg=display.newSprite("Background/day_tree_buttom.png"):align(display.BottOM_CENTER,display.cx,display.bottom):addTo(self)      self.MainTitleBg=display.newSprite("Background/day_Title_buttom.png"):align(display.top_CENTER,display.top-80):addTo(self)    self.MainRabbitBg=display.newSprite("Background/day_rabbit_buttom.png"):align(display.BottOM_CENTER,220,display.bottom+20):scale(0.8):addTo(self)endfunction HomeScene:addMenuElement()    self:initGameState()    --开始按钮    self.startBtn=cc.ui.UIPushbutton.new(HomeScene.PUSH_button_IMAGES,{scale9=true})        :setbuttonLabel("normal",cc.ui.UILabel.new({            UILabelType=1,text="开始",size=62,Font="Font/hero.fnt"        }))        :setbuttonLabel("pressed",Font="Font/hero.fnt",color=cc.c4b(255,64,100)           }))        :setbuttonLabel("Disabled",color=cc.c4b(0,100)           }))        :onbuttonClicked(function(event)            end)        :align(display.CENTER,display.cy)        :addTo(self)    --设置音乐开关    local function updateSoundCheckBox(checkBox)        local state= ""        if checkBox:isbuttonSelected() then            state="on"        else             state="off"            end        GameData.soundState=state        GameState.save(GameData)    end    --声音按钮    self.soundCheckBox=cc.ui.UICheckBoxbutton.new(HomeScene.CHECKBox_button_IMAGES)        :onbuttonStateChanged(function(event)            updateSoundCheckBox(event.target)        end)        :align(display.BottOM_RIGHT,display.wIDth-40,display.bottom+40)        :scale(0.9)        :addTo(self)    if  GameData.soundState =="on" then        self.soundCheckBox:setbuttonSelected(true)    else         self.soundCheckBox:setbuttonSelected(false)    endendfunction HomeScene:initGameState()    GameState.init(function(param)        local returnValue=nil        if param.errorCode then            printError("errorCode",param.errorCode)        else            if param.name==GAME_STATE_ENCRYPTION_OPERATION_SAVE then                local str=Json.encode(param.values)                str=crypto.encryptXXTEA(str,GAME_STATE_ENCRYPTION_XXTEA)                returnValue=Json.decode(str)            end          end        return returnValue    end,GAME_STATE_ENCRYPTION_filename,GAME_STATE_ENCRYPTION)    if io.exists(GameState.getGameStatePath()) then        GameData=GameState.load()        print("savePath:"..GameState.getGameStatePath())    end     local soundState=GameData.soundState    if soundState==nil then        GameData.soundState="on"        GameState.save(GameData)    end endfunction HomeScene:onEnter()endfunction HomeScene:onExit()endreturn HomeScene
总结

以上是内存溢出为你收集整理的【Cocos2d-x Lua笔记六】游戏存储GameState全部内容,希望文章能够帮你解决【Cocos2d-x Lua笔记六】游戏存储GameState所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

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

原文地址:https://54852.com/web/1027356.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存