標籤:style blog http color java os strong io
Cocos2d-x 3.2 Lua樣本 CaptureScreen(截屏)
轉載請註明:IT_xiao小巫
Cocos2d-x截屏功能是從3.2開始提供的,本篇部落格就是介紹Cocos2d-x 3.2中Lua樣本中的截屏功能。效果如下所示:
例子代碼如下:
--[[截屏測試CaptureScreenTest]]---- 擷取螢幕大小local winSize = cc.Director:getInstance():getWinSize()local kTagSprite = 1local childTag = 119-- 建立層local function createLayer() -- 建立層 local layer = cc.Layer:create() local filename = ""-- 檔案名稱 -- 標題 local title = cc.Label:createWithTTF("New Renderer", "fonts/arial.ttf", 36) title:setColor(cc.c3b(255,255,0)) -- 設定顏色為黃色 layer:addChild(title, 1, 10000) -- 第一個參數為node,第二個參數為zorder,第三個參數是tag title:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 30))-- 設定位置top,center -- 子標題 local subTitle = cc.Label:createWithTTF("Capture screen test, press the menu items to capture the screen", "fonts/arial.ttf", 12) subTitle:setColor(cc.c3b(255,255,0)) -- 設定為黃色 layer:addChild(subTitle, 1, 10001) -- 設定tag為10001 subTitle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 60) )-- 設定位置 -- 左邊位置 local left = cc.p(winSize.width / 4, winSize.height / 2) -- 右邊位置 local right = cc.p(winSize.width / 4 * 3, winSize.height / 2) -- 精靈1 local sp1 = cc.Sprite:create("Images/grossini.png") sp1:setPosition(left)-- 設定初始位置在左邊 local move1 = cc.MoveBy:create(1, cc.p(winSize.width/2, 0))--移動動作,持續1秒 -- 動作序列1 local seq1 = cc.RepeatForever:create(cc.Sequence:create(move1, move1:reverse())) layer:addChild(sp1)--添加精靈1 sp1:runAction(seq1)-- 執行動作序列 -- 精靈2 local sp2 = cc.Sprite:create("Images/grossinis_sister1.png") sp2:setPosition(right)-- 設定初始位置在右邊 local move2 = cc.MoveBy:create(1, cc.p(-winSize.width/2, 0))-- 移動動作,持續1秒 -- 動作序列2 local seq2 = cc.RepeatForever:create(cc.Sequence:create(move2, move2:reverse())) layer:addChild(sp2)-- 添加精靈2 sp2:runAction(seq2) -- 執行動作序列2 --截屏回調方法 local function afterCaptured(succeed, outputFile) if succeed then local sp = cc.Sprite:create(outputFile) layer:addChild(sp, 0, childTag) sp:setPosition(winSize.width / 2, winSize.height / 2) sp:setScale(0.25) -- 顯示縮放 fileName = outputFile else cclog("Capture screen failed.") end end -- 點擊標籤回調的方法 local function onCaptured(tag, sender) -- 移除紋理緩衝 cc.Director:getInstance():getTextureCache():removeTextureForKey(fileName) layer:removeChildByTag(childTag) fileName = "CaptureScreenTest.png" -- 截屏 cc.utils:captureScreen(afterCaptured, fileName) end local ttfConfig = {} -- 字型配置表 ttfConfig.fontFilePath = "fonts/arial.ttf" -- 字型路徑 ttfConfig.fontSize = 24 -- 字型大小 -- 建立一個標籤,名為capture all local label1 = cc.Label:createWithTTF(ttfConfig, "capture all", cc.TEXT_ALIGNMENT_CENTER, winSize.width) -- 建立功能表項目標籤 local mi1 = cc.MenuItemLabel:create(label1) -- 註冊點擊回調方法 mi1:registerScriptTapHandler(onCaptured) -- 建立菜單 local menu = cc.Menu:create(mi1) -- 添加菜單到層中 layer:addChild(menu) -- 設定在寬的一半,高的1/4的位置上 menu:setPosition(winSize.width / 2, winSize.height / 4) return layerend---------------------------------- CaptureScreen--------------------------------function CaptureScreenTestMain() -- 建立一個情境 local scene = cc.Scene:create() -- 添加情境到層中 scene:addChild(createLayer()) -- 添加Back功能表項目 scene:addChild(CreateBackMenuItem()) return sceneend
Cocos2d-x 提供以下方法用於截屏: cc.utils:captureScreen(afterCaptured, fileName)
其中afterCaptured是自訂的回調方法,fileName為截屏檔案名稱