Cocos2d-x 3.2 Lua示例 CaptureScreen(截屏)

https://blog.csdn.net/wwj_748/article/details/38373849

Cocos2d-x截屏功能是從3.2開始提供的,本篇博客就是介紹Cocos2d-x 3.2中Lua示例中的截屏功能。效果如下所示:




例子代碼如下:
[javascript] view plain copy
  1. --[[  
  2. 截屏測試  
  3. CaptureScreenTest  
  4. ]]--  
  5. -- 獲取屏幕大小  
  6. local winSize = cc.Director:getInstance():getWinSize()  
  7. local kTagSprite = 1  
  8. local childTag   = 119  
  9.   
  10.   
  11. -- 創建層  
  12. local function createLayer()  
  13.   -- 創建層  
  14.   local layer = cc.Layer:create()  
  15.   local filename = ""-- 文件名  
  16.   
  17.   -- 標題  
  18.   local title = cc.Label:createWithTTF("New Renderer""fonts/arial.ttf", 36)  
  19.   title:setColor(cc.c3b(255,255,0)) -- 設置顏色爲黃色  
  20.   layer:addChild(title, 1, 10000) -- 第一個參數爲node,第二個參數爲zorder,第三個參數是tag  
  21.   title:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 30))-- 設置位置top,center  
  22.   
  23.   -- 子標題  
  24.   local subTitle = cc.Label:createWithTTF("Capture screen test, press the menu items to capture the screen""fonts/arial.ttf", 12)  
  25.   subTitle:setColor(cc.c3b(255,255,0)) -- 設置爲黃色  
  26.   layer:addChild(subTitle, 1, 10001)  -- 設置tag爲10001  
  27.   subTitle:setPosition( cc.p(VisibleRect:center().x, VisibleRect:top().y - 60) )-- 設置位置  
  28.   
  29.   -- 左邊位置  
  30.   local left  = cc.p(winSize.width / 4, winSize.height / 2)  
  31.   -- 右邊位置  
  32.   local right = cc.p(winSize.width / 4 * 3, winSize.height / 2)  
  33.   
  34.   -- 精靈1  
  35.   local sp1 = cc.Sprite:create("Images/grossini.png")  
  36.   sp1:setPosition(left)-- 設置初始位置在左邊  
  37.   local move1 = cc.MoveBy:create(1, cc.p(winSize.width/2, 0))--移動動作,持續1秒  
  38.   -- 動作序列1  
  39.   local seq1  = cc.RepeatForever:create(cc.Sequence:create(move1, move1:reverse()))  
  40.   layer:addChild(sp1)--添加精靈1  
  41.   sp1:runAction(seq1)-- 執行動作序列  
  42.   -- 精靈2  
  43.   local sp2 = cc.Sprite:create("Images/grossinis_sister1.png")  
  44.   sp2:setPosition(right)-- 設置初始位置在右邊  
  45.   local move2 = cc.MoveBy:create(1, cc.p(-winSize.width/2, 0))-- 移動動作,持續1秒  
  46.   -- 動作序列2  
  47.   local seq2  = cc.RepeatForever:create(cc.Sequence:create(move2, move2:reverse()))  
  48.   layer:addChild(sp2)-- 添加精靈2  
  49.   sp2:runAction(seq2) -- 執行動作序列2  
  50.   
  51.   --截屏回調方法  
  52.   local function afterCaptured(succeed, outputFile)  
  53.     if succeed then  
  54.       local sp = cc.Sprite:create(outputFile)  
  55.       layer:addChild(sp, 0, childTag)  
  56.       sp:setPosition(winSize.width / 2, winSize.height / 2)  
  57.       sp:setScale(0.25) -- 顯示縮放  
  58.       fileName = outputFile  
  59.     else  
  60.       cclog("Capture screen failed.")  
  61.     end  
  62.   end  
  63.   
  64.   -- 點擊標籤回調的方法  
  65.   local function onCaptured(tag, sender)  
  66.     -- 移除紋理緩存  
  67.     cc.Director:getInstance():getTextureCache():removeTextureForKey(fileName)  
  68.     layer:removeChildByTag(childTag)  
  69.     fileName = "CaptureScreenTest.png"  
  70.     -- 截屏  
  71.     cc.utils:captureScreen(afterCaptured, fileName)  
  72.   end  
  73.   
  74.   
  75.   local ttfConfig = {} -- 字體配置表  
  76.   ttfConfig.fontFilePath = "fonts/arial.ttf" -- 字體路徑  
  77.   ttfConfig.fontSize     = 24  -- 字體大小  
  78.   -- 創建一個標籤,名爲capture all   
  79.   local label1 = cc.Label:createWithTTF(ttfConfig, "capture all", cc.TEXT_ALIGNMENT_CENTER, winSize.width)  
  80.   -- 創建菜單項標籤  
  81.   local mi1 = cc.MenuItemLabel:create(label1)  
  82.   -- 註冊點擊回調方法  
  83.   mi1:registerScriptTapHandler(onCaptured)  
  84.   -- 創建菜單  
  85.   local menu = cc.Menu:create(mi1)  
  86.   -- 添加菜單到層中  
  87.   layer:addChild(menu)  
  88.   -- 設置在寬的一半,高的1/4的位置上  
  89.   menu:setPosition(winSize.width / 2, winSize.height / 4)  
  90.   
  91.   return layer  
  92. end  
  93.   
  94. --------------------------------  
  95. -- CaptureScreen  
  96. --------------------------------  
  97. function CaptureScreenTestMain()  
  98.   -- 創建一個場景  
  99.   local scene = cc.Scene:create()  
  100.   -- 添加場景到層中  
  101.   scene:addChild(createLayer())  
  102.   -- 添加Back菜單項  
  103.   scene:addChild(CreateBackMenuItem())  
  104.   return scene  
  105. end  

Cocos2d-x 提供以下方法用於截屏:
 cc.utils:captureScreen(afterCaptured, fileName)
其中afterCaptured是自定義的回調方法,fileName爲截屏文件名


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章