cocos2d-lua屏幕截屏的方法,超好用,亲测可行

前端之家收集整理的这篇文章主要介绍了cocos2d-lua屏幕截屏的方法,超好用,亲测可行前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2d-x lua系统自带截屏功能,使用方便。代码如下:

  1. local fileName = "printScreen.png"
  2. -- 移除纹理缓存
  3. cc.Director:getInstance():getTextureCache():removeTextureForKey(fileName)
  4. self:removeChildByTag(1000)
  5. -- 截屏
  6. cc.utils:captureScreen(function(succeed,outputFile)
  7. if succeed then
  8. local winSize = cc.Director:getInstance():getWinSize()
  9. local sp = cc.Sprite:create(outputFile)
  10. self:addChild(sp,1000)
  11. sp:setPosition(winSize.width / 2,winSize.height / 2)
  12. sp:setScale(0.5) -- 显示缩放
  13. print(outputFile)
  14. else
  15. cc.showTextTips("截屏失败")
  16. end
  17. end,fileName)


第二种方法,一帧之内进行截图并把纹理保存到本地的方法

关键代码

  1. function WeixinShareTips.createWeixinImageFile(bShareCurrentScene,func_next)
  2.  
  3. local imgSize = cc.size(640,960)
  4. local backGround = {
  5. path = "weixin/background_twoDimension.jpg",pos = cc.p(imgSize.width / 2,imgSize.height / 2),scale = 1
  6. }
  7. local logo = {
  8. path = cc.logos[cc.getSDKPlat()],pos = cc.p(503,848),scale = 0.51
  9. }
  10. local twoDimension = {
  11. path = nil,pos = cc.p(532,106),scale = 1
  12. }
  13.  
  14. if bShareCurrentScene then
  15. backGround = nil
  16. logo.pos = cc.p(117,550)
  17. twoDimension = nil
  18. imgSize = cc.size(960,640)
  19. logo.scale = 0.3
  20. elseif cc.getSDKPlat() == "ios_yd" then
  21. twoDimension.path = "weixin/twoDimension_bierangwomaoxian.jpg"
  22. elseif cc.getSDKPlat() == "ios_yd2" then
  23. twoDimension.path = "weixin/twoDimension_maoxianqishituan.jpg"
  24. elseif cc.getSDKPlat() == "ios_yd3" then
  25. twoDimension.path = "weixin/twoDimension_menghuanqishituan.jpg"
  26. else
  27. backGround.path = "weixin/background_noDimension.jpg"
  28. twoDimension = nil
  29. end
  30.  
  31. local bigImagePath = WeixinShareTips.createWeixinImageFileWithlogoAndTwoDimension("bigWeixinShare.jpg",backGround,logo,twoDimension,imgSize)
  32. local saveRet = bigImagePath ~= nil
  33.  
  34. local function delayDoSomething(call_todo)
  35. cc.Director : getInstance() : getRunningScene() : runAction(cc.Sequence:create(
  36. cc.DelayTime:create(0.1),cc.CallFunc:create(function ()
  37. call_todo()
  38. end)))
  39. end
  40.  
  41. delayDoSomething(function()
  42. local imgScale = 1/8
  43. imgSize = cc.size(imgSize.width * imgScale,imgSize.height * imgScale)
  44. backGround = {
  45. path = bigImagePath,scale = imgScale
  46. }
  47.  
  48. local smallImagePath = WeixinShareTips.createWeixinImageFileWithlogoAndTwoDimension("smallWeixinShare.jpg",nil,imgSize)
  49. delayDoSomething(function()
  50. saveRet = saveRet and (smallImagePath ~= nil)
  51. func_next(saveRet,smallImagePath,bigImagePath)
  52. end)
  53. end)
  54. end
  55.  
  56. function WeixinShareTips.createWeixinImageFileWithlogoAndTwoDimension(toFileName,imgSize)
  57.  
  58. local function createRenderNodeWithPathPos(pathPos)
  59. local sprite = nil
  60. if pathPos then
  61. sprite = cc.Sprite:create(pathPos.path)
  62. sprite : setPosition(pathPos.pos)
  63. sprite : setScale(pathPos.scale)
  64. end
  65. return sprite
  66. end
  67.  
  68. local function createRenderTextureWithNodes(logoRenderNode,twoDimensionNode,backGroundNode)
  69. -- body
  70. local renderTexture = cc.RenderTexture:create(imgSize.width,imgSize.height)
  71.  
  72. renderTexture : beginWithClear(0,0)
  73.  
  74. if backGroundNode and (cc.Director:getInstance():getRunningScene() ~= backGroundNode) then
  75. backGroundNode : getTexture() : setTexParameters(cc.GL_LINEAR,cc.GL_LINEAR,cc.GL_CLAMP_TO_EDGE,cc.GL_CLAMP_TO_EDGE)
  76. end
  77.  
  78. if backGroundNode then
  79. backGroundNode : visit()
  80. end
  81.  
  82. if logoRenderNode then
  83. logoRenderNode : visit()
  84. end
  85.  
  86. if twoDimensionNode then
  87. twoDimensionNode : visit()
  88. end
  89.  
  90. renderTexture : endToLua()
  91. return renderTexture
  92. end
  93.  
  94. local function createImageFileWithRenderTexture(renderTexture)
  95. local saveRet = renderTexture : saveToFile(toFileName,cc.IMAGE_FORMAT_JPEG,false)
  96. cc.Director : getInstance() : getTextureCache() : removeTextureForKey(
  97. cc.FileUtils:getInstance():getWritablePath() .. toFileName)
  98. if saveRet then
  99. return cc.FileUtils:getInstance():getWritablePath() .. toFileName
  100. else
  101. cc.showTextTips("保存图片失败")
  102. return nil
  103. end
  104. end
  105.  
  106. local logoNode = createRenderNodeWithPathPos(logo)
  107. local twoDimensionNode = createRenderNodeWithPathPos(twoDimension)
  108. local backGroundNode = createRenderNodeWithPathPos(backGround)
  109. if not backGroundNode then
  110. backGroundNode = cc.Director:getInstance():getRunningScene()
  111. end
  112. local renderTexture = createRenderTextureWithNodes(logoNode,backGroundNode)
  113. return createImageFileWithRenderTexture(renderTexture)
  114. end

用法代码如下:(WeixinShareTips.lua)
  1. local WeixinShareTips = class("WeixinShareTips",cc.Node)
  2. local EventTips = import(".EventTips")
  3. local NoticeLayer = import(".NoticeLayer")
  4. local EvaluateLayer = import(".EvaluateLayer")
  5. local GuideLayer = import(".GuideLayer")
  6.  
  7. WeixinShareTips.RESOURCE_FILENAME = "WeixinShareTips.csb"
  8.  
  9. --一些变量
  10. local lsl = LuaSerialize:getInstance()
  11. local this = nil
  12.  
  13. function WeixinShareTips : ctor()
  14. self.root = cc.CSLoader:createNode(WeixinShareTips.RESOURCE_FILENAME)
  15. self:addChild(self.root)
  16. self:enableNodeEvents()
  17. this = self
  18. end
  19.  
  20. function WeixinShareTips : getResourceNode()
  21. return self.root
  22. end
  23.  
  24. function WeixinShareTips.getShareRewardFromServer()
  25. print("SHARE_SUCCESS")
  26. --请求数据
  27. lsl:clear()
  28. lsl:writeShort(111)
  29. lsl:writeBytes(cc.userSid,8)
  30. local data = lsl:getBytes()
  31. cc.sendReq(cc.gameServer,data,function (msg,sc,rc)
  32. if sc == 0 and rc == 0 then
  33. cc.showTextTips("分享成功,请在邮件中查收奖品")
  34. elseif rc == 109 then
  35. print("今日已领取奖励")
  36. else
  37. cc.showTextTips("sc:" .. sc .. " rc:" .. rc)
  38. end
  39. end)
  40. end
  41.  
  42. function WeixinShareTips.startShareWithNodeAndshareCurrentSece(dstNode,isShareCurrentScene)
  43. local WeixinTips = WeixinShareTips : create()
  44. dstNode : addChild(WeixinTips)
  45. WeixinShareTips.isShareCurrentScene = isShareCurrentScene
  46. end
  47.  
  48. function WeixinShareTips : onEnter()
  49. --按钮事件
  50. --mask
  51. self : getResourceNode() : getChildByName("pnl_mask") : addClickEventListener(
  52. function ()
  53. cc.gameSound:playClickSound()
  54. self : removeFromParent()
  55. end)
  56.  
  57.  
  58. self : getResourceNode() : getChildByName("btn_shareAll") : addClickEventListener(
  59. function ()
  60. cc.gameSound:playClickSound()
  61. self : removeFromParent()
  62. WeixinShareTips.createWeixinImageFile(WeixinShareTips.isShareCurrentScene,function(success,bigImagePath)
  63. if success then
  64. WeixinShareTips.sendWeixinImage(cc.const.TOALLFRIENDS,bigImagePath)
  65. end
  66. end)
  67. end)
  68.  
  69. self : getResourceNode() : getChildByName("btn_shareFriend") : addClickEventListener(
  70. function ()
  71. cc.gameSound:playClickSound()
  72. self : removeFromParent()
  73. WeixinShareTips.createWeixinImageFile(WeixinShareTips.isShareCurrentScene,bigImagePath)
  74. if success then
  75. WeixinShareTips.sendWeixinImage(cc.const.TOFRIEND,bigImagePath)
  76. end
  77. end)
  78. end)
  79.  
  80. self : initUI()
  81. end
  82.  
  83.  
  84. --重置UI
  85. function WeixinShareTips : initUI()
  86. local pnl_smallItem = self.root : getChildByName("pnl_smallItem")
  87. local pnl_hero = self.root : getChildByName("pnl_hero")
  88. local itemInterval = 82
  89. local heroInterval = 110
  90.  
  91. local rewards = cc.parseCombainCfg(cc.csvData["global"]["wxShareRewardShow"])
  92. local node = self : createRewardNode(pnl_smallItem,pnl_hero,itemInterval,heroInterval,rewards)
  93. self.root : getChildByName("pnl_posTop") : addChild(node)
  94.  
  95. rewards = cc.parseCombainCfg(cc.csvData["global"]["wxShareFirstRewardShow"])
  96. node = self : createRewardNode(pnl_smallItem,rewards)
  97. self.root : getChildByName("pnl_posDown") : addChild(node)
  98. end
  99.  
  100. --创建奖励,pnl_smallItem: 小项的panle,pnl_hero:英雄背景,itemInterval:列间距,heroInterval英雄间距,rewards:奖励
  101. function WeixinShareTips : createRewardNode(pnl_smallItem,rewards)
  102.  
  103. --创建角色等待动画
  104. local function createARoleWithShadow(name,qlty,scale)
  105. local node = ccui.Widget : create()
  106. local anim = cc.createRoleAnim(name,qlty == "6")
  107. anim:setName("anim")
  108. anim:getAnimation():play("wait",-1,1)
  109. anim:setScale(scale)
  110. node:addChild(anim)
  111. anim:setPosition(45,13)
  112. node : setContentSize(cc.size(100,100))
  113. return node
  114. end
  115.  
  116. local function func_showHero(sender)
  117. -- body
  118. local heroCode = tostring(sender : getTag())
  119. local hero = cc.csvData["heros"][heroCode]
  120. EventTips.getInstance():showTips(this)
  121. hero.new = false
  122.  
  123. hero.hit = 0 --命中
  124. hero.crit = 0 --暴击
  125. hero.def = 0 --防御
  126. hero.dodge = 0 --闪避
  127. hero.ethos = 0 --王者
  128. hero.speed = 0 --速度
  129.  
  130. EventTips.getInstance():showHero(hero,true)
  131. end
  132.  
  133. local function setHeroPanelInfoWithCode(panel,heroCode)
  134. local qlty = cc.csvData["heros"][heroCode]["qlty"]
  135. local anim = createARoleWithShadow(cc.csvData["heros"][heroCode]["appearance"],0.35)
  136. anim : setPosition(cc.p(47,45))
  137.  
  138. panel : setVisible(true)
  139. panel : getChildByName("img_anim") : removeChildByTag(1024)
  140. panel : getChildByName("img_anim") : addChild(anim)
  141. anim : setTag(1024)
  142. panel : getChildByName("img_anim") : setTag(heroCode)
  143. panel : getChildByName("img_anim") : loadTexture("icon/ui_ctn_layer_Box0" .. qlty ..".png")
  144. panel : getChildByName("img_anim") : addClickEventListener(func_showHero)
  145. end
  146.  
  147. local function func_showItemTips(sender,touchType)
  148. local code = tostring(sender : getTag())
  149.  
  150. if touchType == cc.const.TOUCH_EVENT_BEGAN then
  151. local img_BoxSize = sender : getContentSize()
  152. --began
  153. cc.gameSureTips:showItemTips(
  154. sender,code,cc.p(img_BoxSize.width / 2,img_BoxSize.height + 6)
  155. )
  156. elseif touchType == 1 then
  157. --moved
  158. else
  159. --ended or canceled
  160. local tips = sender : getChildByName("showItemTips")
  161. if tips then
  162. tips.close()
  163. end
  164. end
  165. end
  166.  
  167. local function setItemInfoWithReward(pnl_item,reward,img_BoxName)
  168. local img_Box = pnl_item : getChildByName("img_Box")
  169. img_Box : setName(img_BoxName)
  170. img_Box : loadTexture(cc.getResIconName(reward.aType)) --icon
  171. img_Box : setTouchEnabled(true)
  172.  
  173. --数值类型
  174. if reward.code == "0" then
  175. reward.code = tostring(900000 + tonumber(reward.aType))
  176. end
  177.  
  178. img_Box:loadTexture("icon/ui_icon_Box"..cc.csvData["items"][reward.code]["qlty"]..".png")
  179. img_Box:getChildByName("img_icon")
  180. :loadTexture("icon/"..cc.csvData["items"][reward.code]["icon"]..".png")
  181. img_Box:getChildByName("txt_count"):setString(reward.count)
  182.  
  183. img_Box : addTouchEventListener(func_showItemTips)
  184. img_Box : setTag(reward.code)
  185. img_Box : setSwallowTouches(false)
  186. end
  187.  
  188. local startPos = cc.p(0,0) --起点位置
  189. local node = ccui.Widget : create()
  190.  
  191. for i = 1,#rewards,1 do
  192. local isHero = (rewards[i][1] == "100") --是否为英雄
  193.  
  194. if isHero then
  195. --创建角色
  196. local pnl_isHero = pnl_hero : clone()
  197. node : addChild(pnl_isHero)
  198. pnl_isHero : setPosition(startPos)
  199. local heroCode = rewards[i][2]
  200. setHeroPanelInfoWithCode(pnl_isHero,heroCode)
  201. startPos.x = startPos.x + heroInterval
  202. else
  203. local pnl_item = pnl_smallItem : clone()
  204. pnl_item : setVisible(true)
  205. node : addChild(pnl_item)
  206.  
  207. local reward = {
  208. aType = rewards[i][1],code = rewards[i][2],count = rewards[i][3]
  209. }
  210. setItemInfoWithReward(pnl_item,"img_Box_" .. i)
  211. pnl_item : setPosition(startPos)
  212. startPos.x = startPos.x + itemInterval
  213. end
  214.  
  215. end
  216.  
  217. return node
  218. end
  219.  
  220.  
  221. function WeixinShareTips : onExit()
  222. self : removeAllChildren()
  223. end
  224.  
  225. function WeixinShareTips.addShareButtonTo(dstNode)
  226.  
  227. local function func_btnShare()
  228. WeixinShareTips.startShareWithNodeAndshareCurrentSece(dstNode,true)
  229. end
  230.  
  231. local shareOpen = not(cc.weixinID == nil or cc.weixOpen == nil or cc.weixOpen == "0")
  232. if not shareOpen then
  233. return nil
  234. else
  235. dstNode : removeChildByName("weixinShareButton")
  236.  
  237. local btn = ccui.Button : create()
  238. dstNode : addChild(btn)
  239.  
  240. btn : setName("weixinShareButton")
  241. btn : loadTextures("weixin/ui_btn_share_normal.png","weixin/ui_btn_share_selected.png","")
  242. btn : setLocalZOrder(12345)
  243. btn : setPosition(cc.p(898,54))
  244.  
  245. btn : setTouchEnabled(true)
  246. btn : addClickEventListener(func_btnShare)
  247.  
  248. return btn
  249. end
  250. end
  251.  
  252.  
  253. --mode:0发给朋友,1发到朋友圈,smallPng:小图片,bigPng:大图
  254. function WeixinShareTips.sendWeixinImage(mode,smallPng,bigPng)
  255. -- body
  256. -- local mode = "1"
  257. -- local smallPng = "test.png"
  258. -- local bigPng = "testBig.png"
  259. local info = "8;" .. mode .. ";" ..smallPng .. ";" ..bigPng .. ";" .. cc.weixinID
  260. cc.dataEye(info)
  261. end
  262.  
  263. --mode:0发给朋友,Png:图片
  264. function WeixinShareTips.sendWeixinURL(mode,png,title,desc,url)
  265. -- body
  266. -- local mode = "1"
  267. -- local png = "test.png"
  268. -- local title = "TITLE-------TITLE"
  269. -- local desc = "DESC--------DESC"
  270. -- local url = "http://www.baidu.com"
  271. -- local mode = "1"
  272. local info = "9;" .. png .. ";" ..title .. ";" ..desc .. ";" .. url .. ";" .. mode .. ";" ..cc.weixinID
  273. cc.dataEye(info)
  274. end
  275.  
  276. function WeixinShareTips.createWeixinImageFile(bShareCurrentScene,backGroundNode)
  277. return createImageFileWithRenderTexture(renderTexture)
  278. end
  279.  
  280. return WeixinShareTips

猜你在找的Cocos2d-x相关文章