1 文本动画播放
实现文本打字机动画播放。
function TestNode:createLabel()
local label = ccui.Text:create()
label:setString("this is a test. \n a animation test")
label:setFontSize(30)
label:ignoreContentAdaptWithSize(false)
self.label = label
self:addChild(label)
end
function TestNode:playAni()
-- split text to chars
local str = self.label:getString()
self.label:setString("")
local charList = stringToChars(str) -- this func can be found in http://blog.csdn.net/xiang__jiangsu/article/details/64126521
-- create tmplabel to obtain node height
local tmplabel = ccui.Text:create()
tmplabel:setFontSize(self.label:getFontSize())
tmplabel:setString("T")
local nodeHeight = tmplabel:getContentSize().height
-- convert chars to nodes
local origCS = self.label:getContentSize()
local width,height = 0,origCS.height
for k,v in ipairs(charList) do
-- if char is "\n", skip to next line
if v == "\n" then
width = 0
height = height - nodeHeight
else
local curLabel = ccui.Text:create()
curLabel:setFontSize(self.label:getFontSize())
curLabel:setAnchorPoint(cc.p(0,1))
curLabel:setPosition(cc.p(width,height))
curLabel:setOpacity(0)
self.label:addChild(curLabel)
width = width + curLabel:getContentSize().width
end
end
-- play ani
local index = 0
local wordSpeed = 10 -- 10 word one second
local nodeChildren = self.label:getChildren()
for k,v in ipairs(nodeChildren) do
index = index + 1
local delayTime = index/wordSpeed
local delayAct = cc.DelayTime:create(delayTime)
local fadeInAct = cc.FadeIn:create(0.2)
v:runAction(cc.Sequence:create(delayAct,fadeInAct))
end
end