Cocos2d-x 3.2 Lua example CaptureScreen (screenshot), cocos2dlua
Cocos2d-x 3.2 Lua example CaptureScreen (screenshot)
Reprinted Please note: IT_xiao xiaowu
The Cocos2d-x screenshot feature is available from 3.2 and this blog introduces the screenshot feature in the Lua sample in Cocos2d-x 3.2. The effect is as follows:
The sample code is as follows:
-- [[Screenshot test CaptureScreenTest] ---- obtain the screen size. local winSize = cc. director: getInstance (): getWinSize () local kTagSprite = 1 local childTag = 119 -- create layer local function createLayer () -- create layer local layer = cc. layer: create () local filename = "" -- file name -- title local title = cc. label: createWithTTF ("New Renderer", "fonts/arial. ttf ", 36) title: setColor (cc. c3b (255,255, 0) -- set the color to yellow layer: addChild (title, 1, 10000) -- the first parameter is node, the second parameter is zorder, the third parameter is tag title: setPosition (cc. p (VisibleRect: center (). x, VisibleRect: top (). y-30) -- set the location top, center -- subTitle 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) -- set it to yellow layer: addChild (subTitle, 1, 10001) -- set the tag to 10001 subTitle: setPosition (cc. p (VisibleRect: center (). x, VisibleRect: top (). y-60) -- set the position -- local left = cc on the left. p (winSize. width/4, winSize. height/2) -- the right position is local right = cc. p (winSize. width/4*3, winSize. height/2) -- genie 1 local sp1 = cc. sprite: create ("Images/grossini.png") sp1: setPosition (left) -- set the initial position to local move1 = cc on the left. moveBy: create (1, cc. p (winSize. width/2, 0) -- move action, lasting 1 second -- action sequence 1 local seq1 = cc. repeatForever: create (cc. sequence: create (move1, move1: reverse () layer: addChild (sp1) -- add genie 1 sp1: runAction (seq1) -- execution action sequence -- genie 2 local sp2 = cc. sprite: create ("Images/grossinis_sister1.png") sp2: setPosition (right) -- set the initial position to local move2 = cc on the right. moveBy: create (1, cc. p (-winSize. width/2, 0) -- move action, lasting 1 second -- action sequence 2 local seq2 = cc. repeatForever: create (cc. sequence: create (move2, move2: reverse () layer: addChild (sp2) -- add genie 2 sp2: runAction (seq2) -- execute action sequence 2 -- screenshot callback method 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) -- display zoom fileName = outputFile else cclog ("Capture screen failed. ") end -- click the label callback method local function onCaptured (tag, sender) -- remove the texture cache cc. director: getInstance (): getTextureCache (): removeTextureForKey (fileName) layer: removeChildByTag (childTag) fileName = "CaptureScreenTest.png" -- screenshot cc. utils: captureScreen (afterCaptured, fileName) end local ttfConfig ={} -- ttfConfig. fontFilePath = "fonts/arial. ttf "-- font path ttfConfig. fontSize = 24 -- font size -- create a tag named capture all local label1 = cc. label: createWithTTF (ttfConfig, "capture all", cc. TEXT_ALIGNMENT_CENTER, winSize. width) -- create the menu item label local mi1 = cc. menuItemLabel: create (label1) -- Registration click callback method mi1: registerScriptTapHandler (onCaptured) -- create menu local menu = cc. menu: create (mi1) -- add the menu to the layer: addChild (menu) -- set it to the half of the width, and the position of the High 1/4 Menu: setPosition (winSize. width/2, winSize. height/4) return layerend ---------------------------------- CaptureScreen -------------------------------- function CaptureScreenTestMain () -- create a scenario local scene = cc. scene: create () -- add scenario to layer scene: addChild (createLayer () -- add Back menu item scene: addChild (CreateBackMenuItem () return sceneend
The Cocos2d-x provides the following methods for screenshots: cc. utils: captureScreen (afterCaptured, fileName)
AfterCaptured is a custom callback method, and fileName is the screenshot file name.
If you think the blog is useful to you, please go to the following link to cast a sacred vote for Xiao Wu.
Http://vote.blog.csdn.net/Article/Details? Articleid = 38272837
When creating a cocos2D-x project in VS2010, Select Lua support: Support Lua (Hook), an error is reported after generation,
Some library files are not properly loaded. Find the corresponding library file and paste it to the corresponding location. Do not forget to check whether the file has permissions.
How to combine cocos2d-x with Lua to use specific points
The cocos2d-x comes with the lua engine. Look at this. Although it is English, it is easy to understand.
Cocos2d-x NEW Lua Engine README
Main features
Support autorelease CCObject object.
Call Lua function from C ++ (local, global, closure), avoid memory leaks.
Add CCNode: setPosition (x, y), CCNode: getPosition () huge performance boost.
Remove needless class and functions from tolua ++. pkg files, improved performance.
CCMenuItem events handler.
CCNode onEnter/onExit events handler.
CCLayer touch & multi-touches events handler.
Check the two connections.
Www.cocos2d-x.org/..uments
Scripting and Translating between Programming ages