Cocos2d-x 3.2 Lua演示樣例FontTest(字體測試)
? 本篇博客介紹Cocos2d-x 3.2中Lua測試項目中的FontTest樣例,主要使用了字體文件來創建我們想要的字體樣式:
第一個參數為文本。第二參數為ttf字體文件,第三個參數為字體大小,第四個參數為塊大小。第五個參數為文本橫向對齊方式,第六個慘為文本縱向對齊方式。
樣例效果圖:
演示樣例代碼:(注:單獨文件不可執行,詳細可參考lua-tests)
--4個標簽的Tag
local kTagLabel1 = 0
local kTagLabel2 = 1
local kTagLabel3 = 2
local kTagLabel4 = 3-- 字體列表
local 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) -- 獲得表元素個數
cclog("font count = "..fontCount)local vAlignIdx = 1
local verticalAlignment = {cc.VERTICAL_TEXT_ALIGNMENT_TOP, -- 對齊頂部cc.VERTICAL_TEXT_ALIGNMENT_CENTER, -- 居中對齊cc.VERTICAL_TEXT_ALIGNMENT_BOTTOM, -- 底部對齊
}local vAlignCount = table.getn(verticalAlignment)--[[
===============
顯示字體
===============
]]--
local function showFont(ret, pFont)cclog("vAlignIdx="..vAlignIdx)local s = cc.Director:getInstance():getWinSize()-- 窗體大小local blockSize = cc.size(s.width/3, 200)-- 塊大小local fontSize = 26--字體大小-- 依據Tag移除子節點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)-- 第一個參數為文本,第二個參數為字體樣式,第三個參數為字體大小,第四個參數為塊大小,第五個參數為文本橫向對齊。第六個參數為文本縱向對齊local top = cc.Label:createWithTTF(pFont, pFont, 24)local left = cc.Label:createWithTTF("alignment left", pFont, fontSize,blockSize, cc.TEXT_ALIGNMENT_LEFT, verticalAlignment[vAlignIdx])local center = cc.Label:createWithTTF("alignment center", pFont, fontSize,blockSize, cc.TEXT_ALIGNMENT_CENTER, verticalAlignment[vAlignIdx])local right = cc.Label:createWithTTF("alignment right", pFont, fontSize,blockSize, cc.TEXT_ALIGNMENT_RIGHT, verticalAlignment[vAlignIdx])-- 創建顏色層,第一個參數是顏色,第二個參數為寬度,第三個參數為高度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)-- 忽略錨點對位置的影響 ,假設為true。錨點為(0,0)leftColor:ignoreAnchorPointForPosition(false)centerColor:ignoreAnchorPointForPosition(false)rightColor:ignoreAnchorPointForPosition(false)top:setAnchorPoint(cc.p(0.5, 1)) -- 設置錨點(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)) -- 設置顯示位置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 = true
local originCreateLayer = createTestLayer
local function createTestLayer()if isFirst == false thenif Helper.index == 1 thenvAlignIdx = vAlignIdx % vAlignCount + 1endelseisFirst = falseendlocal ret = originCreateLayer("")showFont(ret, fontList[Helper.index])return ret
endfunction FontTestMain()cclog("FontTestMain")Helper.index = 1vAlignIdx = 1local scene = cc.Scene:create()-- 創建方法表Helper.createFunctionTable = {createTestLayer,createTestLayer,createTestLayer,createTestLayer,createTestLayer,createTestLayer}scene:addChild(createTestLayer()) -- 加入測試層scene:addChild(CreateBackMenuItem())--加入后退buttonreturn scene
end