Cocos2d-x 3.2 Lua sample FontTest (font test), cocos2d-xfonttest

Source: Internet
Author: User
Tags addchild

Cocos2d-x 3.2 Lua sample FontTest (font test), cocos2d-xfonttest

Cocos2d-x 3.2 Lua sample FontTest (FontTest)


This blog introduces the FontTest example in the Lua test project in Cocos2d-x 3.2, mainly using a font file to create the font style we want:


The first parameter is text, the second parameter is the ttf font file, the third parameter is the font size, the fourth parameter is the block size, and the fifth parameter is the text horizontal alignment, the sixth is vertical text alignment.


Example:








Sample Code: (Note: a separate file cannot be run. For details, refer to lua-tests)

-- Taglocal kTagLabel1 = 0 local kTagLabel2 = 1 local kTagLabel3 = 2 local kTagLabel4 = 3 -- fontList = {"fonts/A Damn Mess. ttf "," fonts/Abberancy. ttf "," fonts/Abduction. ttf "," fonts/Paint Boy. ttf "," fonts/Schwarzwald Regular. ttf "," fonts/Scissor Cuts. ttf "} local fontCount = table. getn (fontList) -- get the number of table elements cclog ("font count = ".. fontCount) local vAlignIdx = 1 local verticalAlignment = {cc. VERTICAL_TEXT_ALIGNMENT_TOP, -- align cc at the top. VERTICAL_TEXT_ALIGNMENT_CENTER, -- center alignment cc. VERTICAL_TEXT_ALIGNMENT_BOTTOM, -- bottom aligncount} local vAlignCount = table. getn (verticalignment) -- [[===================== display font =========================]] -- local function showFont (ret, pFont) cclog ("vAlignIdx = ".. vAlignIdx) local s = cc. director: getInstance (): getWinSize () -- window size local blockSize = cc. size (s. width/3,200) -- block size local fontSize = 26 -- font size -- remove the sub-node ret: removeChildByTag (kTagLabel1, true) ret: removeChildByTag (kTagLabel2, true) ret: removeChildByTag (kTagLabel3, true) ret: removeChildByTag (kTagLabel4, true) -- static Label * createWithTTF (const std: string & text, const std: string & fontFile, float fontSize, const Size & dimensions = Size: ZERO, -- TextHAlignment hAlignment = TextHAlignment: LEFT, TextVAlignment vAlignment = TextVAlignment: TOP) -- the first parameter is text, the second parameter is the font style, the third parameter is the font size, the fourth parameter is the block size, and the fifth parameter is the horizontal alignment of the text, the sixth parameter is vertical text alignment. local top = cc. label: createWithTTF (pFont, pFont, 24) local left = cc. label: createWithTTF ("alignment left", pFont, fontSize, blockSize, cc. TEXT_ALIGNMENT_LEFT, verticalignment [vAlignIdx]) local center = cc. label: createWithTTF ("alignment center", pFont, fontSize, blockSize, cc. TEXT_ALIGNMENT_CENTER, verticalignment [vAlignIdx]) local right = cc. label: createWithTTF ("alignment right", pFont, fontSize, blockSize, cc. TEXT_ALIGNMENT_RIGHT, verticalignment [vAlignIdx]) -- creates a color layer. The first parameter is color, the second parameter is width, and the third parameter is height local leftColor = cc. layerColor: create (cc. c4b (100,100,100,255), blockSize. width, blockSize. height) local centerColor = cc. layerColor: create (cc. c4b (200,100,100,255), blockSize. width, blockSize. height) local rightColor = cc. layerColor: create (cc. c4b (100,100,200,255), blockSize. width, blockSize. height) -- ignore the influence of the anchor on the position. If it is true, the anchorpointforposition (0, 0) leftColor: ignoreAnchorPointForPosition (false) centerColor: ignoreAnchorPointForPosition (false) top: setAnchorPoint (cc. p (0.5, 1) -- set the anchor (0.5, 1) left: setAnchorPoint (cc. p (0, 0.5) leftColor: setAnchorPoint (cc. p (0, 0.5) center: setAnchorPoint (cc. p (0, 0.5) centerColor: setAnchorPoint (cc. p (0, 0.5) right: setAnchorPoint (cc. p (0, 0.5) rightColor: setAnchorPoint (cc. p (0, 0.5) top: setPosition (cc. p (s. width/2, s. height-20) -- set the display position left: setPosition (cc. p (0, s. height/2) leftColor: setPosition (left: getPosition () center: setPosition (cc. p (blockSize. width, s. height/2) centerColor: setPosition (center: getPosition () right: setPosition (cc. p (blockSize. width * 2, s. height/2) rightColor: setPosition (right: getPosition () ret: addChild (leftColor,-1) ret: addChild (left, 0, kTagLabel1) ret: addChild (rightColor, -1) ret: addChild (right, 0, kTagLabel2) ret: addChild (centerColor,-1) ret: addChild (center, 0, kTagLabel3) ret: addChild (top, 0, kTagLabel4) endlocal isFirst = truelocal originCreateLayer = createTestLayerlocal function createTestLayer () if isFirst = false then if Helper. index = 1 then vAlignIdx = vAlignIdx % vAlignCount + 1 end else isFirst = false end local ret = originCreateLayer ("") showFont (ret, fontList [Helper. index]) return retendfunction FontTestMain () cclog ("FontTestMain") Helper. index = 1 vAlignIdx = 1 local scene = cc. scene: create () -- create a method table Helper. createFunctionTable = {createTestLayer, createTestLayer} scene: addChild (createTestLayer () -- add Test layer scene: addChild (CreateBackMenuItem ()) -- add the return sceneend button



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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.