cocos2d-X json

c++

    rapidjson::Document doc;
    ssize_t size = 0;
    unsigned char *pBytes = NULL;
    filename = "C:/Users/Administrator/Desktop/4b18fd8d-6454-4eab-b2b2-dcea0e785928.85e63.json";
    pBytes = cocos2d::CCFileUtils::getInstance()->getFileData(filename, "rb", &size);

    std::string load_str((const char*)pBytes, size);

    CC_SAFE_DELETE_ARRAY(pBytes);

    doc.Parse<0>(load_str.c_str());

    auto r = doc.MemberCount();
    auto rr = doc.FindMember("_atlasText");
    auto rrr = rr[0].value.GetString();

    string str = rrr;
    auto file = fopen("kb_zjh.atlas", "wb+");
    if (file)
    {
        fwrite(str.c_str(), str.length(), 1, file);
        fclose(file);
    }

lua:

local MainEngine = class("MainEngine")
local solution = require("PriFrame/Solution")
local def = require("PriFrame/def")
local json = require("json")


-- //穿透解决方案
function MainEngine:Penetration(node, bool)
    node:setTouchEnabled(true)
    node:registerScriptTouchHandler( function()
        return bool
    end )
end

function MainEngine:NetworkBox(str)
    local node = cc.Layer:create()
    node:setName("NetworkBox")
    node:setTouchEnabled(true)
    node:registerScriptTouchHandler( function()
        return true
    end )
    local box = cc.Sprite:create("client/res/box/box_4.png")
    box:setContentSize(cc.size(800, 500))
    box:setPosition(display.center)
    node:addChild(box)

    local file = "client/res/UI/btn/close_4.png"
    local close = ccui.Button:create(file, file, file)
    close:setName("close")
    --    close:setScale9Enabled(true)
    --    close:setContentSize(cc.size(50,50))
    close:setPosition(box:getContentSize().width - close:getContentSize().width / 2,
    box:getContentSize().height - close:getContentSize().height / 2)
    box:addChild(close)

    local msg = cc.Label:createWithSystemFont(str, "", 35)
    msg:setPosition(box:getContentSize().width / 2, box:getContentSize().height / 2)
    msg:setColor(cc.c3b(255, 255, 255))
    --    msg:setDimensions(750, 200)
    box:addChild(msg)

    close:addClickEventListener( function(ref)
        local layer = ref:getParent():getParent()
        layer:runAction(cc.ScaleTo:create(0.2, 0.01));
        layer:getParent():runAction(cc.Sequence:create(cc.DelayTime:create(0.2),
        cc.CallFunc:create( function()
            layer:getParent():removeChild(layer);
        end )))
    end )
    local sscaleto = cc.ScaleTo:create(0.1, 1.2);
    local time = cc.DelayTime:create(0.01);
    local escaleto = cc.ScaleTo:create(0.1, 1.0);
    local sequence = cc.Sequence:create(sscaleto, time, escaleto);
    node:runAction(sequence);
    return node;
end
function MainEngine:MessageBox(str)
    return self:NetworkBox(str)
end
function MainEngine:LoadCSB(file)
    local node = cc.CSLoader:createNode(file);
    node:setPosition(display.center)
    local Action = cc.CSLoader:createTimeline(file);
    Action:setTimeSpeed(1.0);
    node:runAction(Action);
    Action:gotoFrameAndPlay(0, true);
    return node, Action;
end

-- ccexp.AudioEngine:playEffect(file)
function MainEngine:PlayeMusic(file, isLoop)
    ccexp.AudioEngine:play2d(file, isLoop)
end

function MainEngine:ResetNetworkBox(str)
    -- local node = display.newLayer(cc.c4b(0, 0, 0, 200))
    local node = cc.Layer:create()
    node:setName("NetworkBox")
    node:setTouchEnabled(true)
    node:registerScriptTouchHandler( function()
        return true
    end )
    local box = cc.Sprite:create("client/res/box/box_4.png")
    box:setContentSize(cc.size(800, 500))
    box:setPosition(display.center)
    node:addChild(box)

    local nodes, action = self:LoadCSB("Network_ProgressBar/Network_ProgressBar.csb")
    nodes:setName("network")
    nodes:setPosition(cc.p(box:getContentSize().width / 2,
    box:getContentSize().height / 2 + 100))
    box:addChild(nodes)

    --    local file = "client/res/UI/btn/close_4.png"
    --    local close = ccui.Button:create(file,file,file)
    --    close:setName("close")
    ----    close:setScale9Enabled(true)
    ----    close:setContentSize(cc.size(50,50))
    --    close:setPosition(box:getContentSize().width - close:getContentSize().width / 2,
    --    box:getContentSize().height - close:getContentSize().height / 2)
    --    box:addChild(close)

    local msg = cc.Label:createWithSystemFont(str, "", 35)
    msg:setPosition(box:getContentSize().width / 2, box:getContentSize().height / 2)
    msg:setColor(cc.c3b(255, 255, 255))
    --    msg:setDimensions(750, 200)
    box:addChild(msg)


    local AgreeFile = "UI/Agree.png"
    local CancelFile = "UI/Cancel.png"
    local callback = function(ref)
        local state = ref:getTag()
        if (state == 1) then
            local layer = ref:getParent():getParent()
            layer:runAction(cc.ScaleTo:create(0.2, 0.01));
            layer:getParent():runAction(cc.Sequence:create(cc.DelayTime:create(0.2),
            cc.CallFunc:create( function()
                layer:getParent():removeChild(layer);
            end ), cc.DelayTime:create(1.5), cc.CallFunc:create( function()
                local kernal = require("PriFrame/Kernal")
                kernal:create()
                kernal:init()
            end )))

            return;
        else
            solution:stop(def.KernalThreadID)
            cc.Director:getInstance():endToLua()
        end
    end
    local Agree = ccui.Button:create(AgreeFile, AgreeFile, AgreeFile)
    local Cancel = ccui.Button:create(CancelFile, CancelFile, CancelFile)
    Agree:setPressedActionEnabled(true)
    Cancel:setPressedActionEnabled(true)
    Agree:setTag(1)
    Cancel:setTag(0)
    Cancel:addClickEventListener(callback)
    Agree:addClickEventListener(callback)
    Agree:setPosition(cc.p(Agree:getContentSize().width + 50, Agree:getContentSize().height))
    Cancel:setPosition(cc.p(box:getContentSize().width - Cancel:getContentSize().width - 50,
    Cancel:getContentSize().height))
    box:addChild(Agree)
    box:addChild(Cancel)


    local sscaleto = cc.ScaleTo:create(0.1, 1.2);
    local time = cc.DelayTime:create(0.01);
    local escaleto = cc.ScaleTo:create(0.1, 1.0);
    local sequence = cc.Sequence:create(sscaleto, time, escaleto);
    node:runAction(sequence);
    return node;
end

function MainEngine:showMSG(string)
    local s = cc.Sprite:create("Show/showMSG.png")
    s:setPosition(display.center)
    s:setScale(0.8)
    cc.Director:getInstance():getRunningScene():addChild(s)
    local str = cc.Label:createWithSystemFont(string, "", 30)
    str:setPosition(cc.p(s:getContentSize().width / 2, s:getContentSize().height / 2))
    str:setColor(cc.c3b(39, 21, 79))
    s:addChild(str)
    local action = cc.Spawn:create(cc.FadeOut:create(1.5),
    cc.MoveTo:create(1.5, cc.p(s:getPositionX(), display.height + 500)))
    action = cc.Sequence:create(cc.DelayTime:create(1), action, cc.CallFunc:create( function()
        s:removeFromParent()
    end ))
    s:runAction(action)
end

function MainEngine:createRunTime(str)
    local label = nil
    if (str ~= nil) then
        label = cc.Label:createWithSystemFont(str, "", 30)
    else
        label = cc.Label:createWithSystemFont("资源准备中.....", "", 30)
    end

    local scene = cc.Director:getInstance():getRunningScene()
    local layer = cc.Layer:create()
    local node, action = MainEngine:LoadCSB("Network_ProgressBar/Network_ProgressBar.csb")
    node:setName("runtime")
    scene:addChild(node)
    label:setColor(cc.c3b(255, 0, 0))
    label:setPosition(cc.p(0, -100))
    node:addChild(label)
    node:addChild(layer)
    self:Penetration(layer, true)
end

function MainEngine:deleteRunTime()
    local scene = cc.Director:getInstance():getRunningScene()
    local node = nil
    for i = 0, 5 do
        node = scene:getChildByName("runtime")
        if (node ~= nil) then
            node:removeFromParent()
        end
    end
end

function MainEngine:createPNGEx(filename, w, h, r, g, b, a)
    local path = ""
    --[[



        ]]
    --[[if (device.platform == "android") then
        path = "/storage/emulated/0/Android/data/com.YZFHKM.GAME.APP.Android.DATA/tmp"
    else
        path = device.writablePath .. "root/save"
    end]]
    path = device.writablePath .. "root/save"
    solution:createpng(path .. "/" .. filename, w, h, r, g, b, a)
end

function MainEngine:isFileExist(fileName)

    local path = cc.FileUtils:getInstance():fullPathForFilename(fileName)

    if (path == "" or path == nil or path == " ") then
        return false
    end

    if (io.exists(path) == false) then
        return false
    else
        return true
    end
    return nil

end

function MainEngine:isHeaderExist(fileName)

    local path = device.writablePath .. "root/user/header/" .. fileName

    if (io.exists(path) == false) then
        return false
    else
        return true
    end

end


function MainEngine:createpng(fileName, w, h)
    if (self:isFileExist(fileName) == false) then
        self:createPNGEx(fileName, w, h,
        0, 255, 0, 255)
        return true
    end
    return false
end

function MainEngine:createJson(fileName)

    local raw_Data = nil
    local json_Data = nil

    raw_Data = cc.FileUtils:getInstance():getDataFromFile(fileName)

    if (raw_Data ~= nil) then

        json_Data = json.decode(raw_Data, 0);

        if (json_Data ~= nil) then
            return json_Data, raw_Data;
        end

    end

    return nil
end

function MainEngine:saveJson(fileName, string)

    cc.FileUtils:getInstance():writeStringToFile(json.encode(string), device.writablePath .. "root/save/" .. fileName)

end

function MainEngine:createTmpFile(fileName,string)
    cc.FileUtils:getInstance():writeStringToFile(string, device.writablePath .. "root/save/" .. fileName)
end




-- function MainEngine:createButton(file)
--    local sprite = cc.Sprite:create(file)
--    local Layer = cc.Layer:create()
--    local w = sprite:getContentSize().width
--    local h = sprite:getContentSize().height
--    Layer:setContentSize(cc.size(w,h))
--    Layer:setAnchorPoint(cc.p(0,0))
--    sprite:addChild(Layer)
--    return sprite
-- end


return MainEngine;
原文地址:https://www.cnblogs.com/YZFHKMS-X/p/14318757.html