Cocos2d-x 3.2 Lua sample FontTest (FontTest)
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:
Vcq9o6y12sH5uPay0s6qzsSxvtfdz/assets/dfT0Ke5 + 828o7o8L3A + PHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140816/2014081608452773.jpg" width = "480" height = "320" alt = "\"/>
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