local updata = class("updata",function()
    return cc.Scene:create()
end)
local title
function updata.create()
    local scene = updata.new()
    scene:addChild(scene:createLayer())
    return scene
end

function updata:ctor()
end

function updata:createLayer()
 local menuLayer = cc.Layer:create()

    title = cc.MenuItemFont:create("title")
    title:setPosition(480/2,280)
    menuLayer:addChild(title)

    function menuCall(tag)
            updata:update()
    end


    local menuLabel2 = cc.Label:createWithTTF("Update", "font.ttf", 24)
    local menuItem2 = cc.MenuItemLabel:create(menuLabel2)
    menuItem2:registerScriptTapHandler(menuCall)
    menuItem2:setPosition(480 /2,320/2)

    local MainMenu = cc.Menu:create()
    MainMenu:setPosition(0,0)
    MainMenu:addChild(menuItem2)
    menuLayer:addChild(MainMenu)

    return menuLayer
end

function updata:getAssetsManager()

    local function onError(errorCode)  --处理更新错误
        if errorCode == cc.ASSETSMANAGER_NO_NEW_VERSION then
            title:setString("no new version")
        elseif errorCode == cc.ASSETSMANAGER_NETWORK then
            title:setString("contect server error")
        end
    end

    local function onProgress( percent )  --下载中回调
        local progress = string.format("downloading %d%%",percent)
        title:setString(progress)
    end

    local function onSuccess() --下载完成回调
        title:setString("downloading ok")
        --加载下载好的 testScene
        local scene = require("testScene")
        local gameScene = scene.create()
        cc.Director:getInstance():replaceScene(gameScene)
    end

    --文件适配
    local path
    if cc.Application:getInstance():getTargetPlatform()==cc.PLATFORM_OS_WINDOWS then
     path="..//..//"
    end

    if cc.Application:getInstance():getTargetPlatform()==cc.PLATFORM_OS_ANDROID then
    path=""
    end
    local assetsManager = cc.AssetsManager:new("http://171.90.197.122/data.zip",--更新文件
        "http://171.90.197.122/version",--版本检查
        path)
    assetsManager:deleteVersion()
    assetsManager:retain()
    assetsManager:setDelegate(onError, cc.ASSETSMANAGER_PROTOCOL_ERROR )
    assetsManager:setDelegate(onProgress, cc.ASSETSMANAGER_PROTOCOL_PROGRESS)
    assetsManager:setDelegate(onSuccess, cc.ASSETSMANAGER_PROTOCOL_SUCCESS )
    assetsManager:setConnectionTimeout(3)


    return assetsManager
end

function updata:update()
    updata:getAssetsManager():update() --执行更新

end

return updata