|
| 1 | +require("json") |
| 2 | + |
| 3 | + |
| 4 | +local imgURI = "http://forum.cocos.com/images/logo.png" |
| 5 | +local notExistURI = "http://www.cocos2d-x.org/attachments/1503/no_exist.txt" |
| 6 | + |
| 7 | +local downloader = cc.Downloader.new() |
| 8 | +local writablePath = cc.FileUtils:getInstance():getWritablePath() |
| 9 | +local function DownloaderLayer() |
| 10 | + local layer = cc.Layer:create() |
| 11 | + local winSize = cc.Director:getInstance():getWinSize() |
| 12 | + local margin = 40 |
| 13 | + local space = 35 |
| 14 | + |
| 15 | + local task1 = nil |
| 16 | + local task2 = nil |
| 17 | + |
| 18 | + local function init() |
| 19 | + |
| 20 | + local label = cc.Label:createWithTTF("Downloader Test", s_arialPath, 28) |
| 21 | + label:setAnchorPoint(cc.p(0.5, 0.5)) |
| 22 | + label:setPosition(cc.p(winSize.width / 2, winSize.height - margin)) |
| 23 | + layer:addChild(label, 0) |
| 24 | + |
| 25 | + --Response Code Label |
| 26 | + local labelStatusCode = cc.Label:createWithTTF("status", s_markerFeltFontPath, 20) |
| 27 | + labelStatusCode:setAnchorPoint(cc.p(0.5, 0.5)) |
| 28 | + labelStatusCode:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 6 * space)) |
| 29 | + layer:addChild(labelStatusCode) |
| 30 | + |
| 31 | + local menuRequest = cc.Menu:create() |
| 32 | + menuRequest:setPosition(cc.p(0,0)) |
| 33 | + layer:addChild(menuRequest) |
| 34 | + |
| 35 | + --success |
| 36 | + local function onDownloadFileClicked() |
| 37 | + downloader:createDownloadFileTask(imgURI, writablePath .. "/2dx_icon_512_rounded.png", "task_img"); |
| 38 | + labelStatusCode:setString("waiting...") |
| 39 | + end |
| 40 | + |
| 41 | + local labelGet = cc.Label:createWithTTF("Test Download Image", s_arialPath, 22) |
| 42 | + labelGet:setAnchorPoint(cc.p(0.5, 0.5)) |
| 43 | + local itemGet = cc.MenuItemLabel:create(labelGet) |
| 44 | + itemGet:registerScriptTapHandler(onDownloadFileClicked) |
| 45 | + itemGet:setPosition(cc.p(winSize.width / 2, winSize.height - margin - space)) |
| 46 | + menuRequest:addChild(itemGet) |
| 47 | + |
| 48 | + --failure |
| 49 | + local function onDownloadTxtClicked() |
| 50 | + downloader:createDownloadFileTask(notExistURI, writablePath .. "/no_exists.txt", "task_no_exist"); |
| 51 | + labelStatusCode:setString("waiting...") |
| 52 | + end |
| 53 | + |
| 54 | + local labelPost = cc.Label:createWithTTF("Test Download File Not Exists", s_arialPath, 22) |
| 55 | + labelPost:setAnchorPoint(cc.p(0.5, 0.5)) |
| 56 | + local itemPost = cc.MenuItemLabel:create(labelPost) |
| 57 | + itemPost:registerScriptTapHandler(onDownloadTxtClicked) |
| 58 | + itemPost:setPosition(cc.p(winSize.width / 2, winSize.height - margin - 2 * space)) |
| 59 | + menuRequest:addChild(itemPost) |
| 60 | + |
| 61 | + local function onTaskSuccess(task) |
| 62 | + if task.identifier == "task_img" then |
| 63 | + labelStatusCode:setString("image download success") |
| 64 | + elseif task.identifier == "task_no_exist" then |
| 65 | + labelStatusCode:setString("txt download success") |
| 66 | + end |
| 67 | + end |
| 68 | + local function onProgress(task, bytesReceived, totalBytesReceived, totalBytesExpected) |
| 69 | + if task.identifier == "task_img" then |
| 70 | + labelStatusCode:setString("image download progress " .. tostring(math.floor(totalBytesReceived/totalBytesExpected*100))) |
| 71 | + elseif task.identifier == "task_no_exist" then |
| 72 | + labelStatusCode:setString("txt download progress " .. tostring(math.floor(totalBytesReceived/totalBytesExpected*100))) |
| 73 | + end |
| 74 | + end |
| 75 | + local function onTaskError(task, errorCode, errorCodeInternal, errorStr) |
| 76 | + if task.identifier == "task_img" then |
| 77 | + labelStatusCode:setString("image download error: "..errorStr) |
| 78 | + elseif task.identifier == "task_no_exist" then |
| 79 | + labelStatusCode:setString("txt download error: "..errorStr) |
| 80 | + end |
| 81 | + end |
| 82 | + downloader:setOnFileTaskSuccess(onTaskSuccess) |
| 83 | + downloader:setOnTaskProgress(onProgress) |
| 84 | + downloader:setOnTaskError(onTaskError) |
| 85 | + |
| 86 | + end |
| 87 | + |
| 88 | + local function onNodeEvent(eventName) |
| 89 | + if "enter" == eventName then |
| 90 | + init() |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + layer:registerScriptHandler(onNodeEvent) |
| 95 | + |
| 96 | + return layer |
| 97 | +end |
| 98 | + |
| 99 | +function DownloaderTestMain() |
| 100 | + local scene = cc.Scene:create() |
| 101 | + scene:addChild(DownloaderLayer()) |
| 102 | + scene:addChild(CreateBackMenuItem()) |
| 103 | + return scene |
| 104 | +end |
0 commit comments