[Quick-x lua]CCLabel类数字变化动作

时间:2022-07-31 05:32:05

之前写了个C++版本的,现在改成lua的,

两者原理是一样,但是动作的执行方式有些微区别

(因为lua无法继承CCActionInterval类,单纯使用lua的话无法调用action的update方法)

下载地址:https://github.com/chenquanjun/Quick-x-CCLabelChange

c++版本的也放到这个号了

--演示

[Quick-x lua]CCLabel类数字变化动作

--使用方法

     do
local label = CCLabelTTF:create("", "Arial", )
label:setPosition(display.cx - , display.cy)
self:addChild(label)
local action = CCLabelChange:create(label, , , ) action:playAction()
end do
local label = CCLabelTTF:create("", "Arial", )
label:setPosition(display.cx + , display.cy)
label:setColor(ccc3(, , ))
self:addChild(label)
local action = CCLabelChange:create(label, , , ) action:playAction()
end do
local label = CCLabelTTF:create("", "Arial", )
label:setPosition(display.cx - , display.cy - )
label:setColor(ccc3(, , ))
self:addChild(label)
local action = CCLabelChange:create(label, , -, ) action:playAction()
end do
local label = CCLabelTTF:create("", "Arial", )
label:setPosition(display.cx + , display.cy - )
label:setColor(ccc3(, , ))
self:addChild(label)
local action = CCLabelChange:create(label, , , ) action:playAction()
end

--源代码

 CCLabelChange = class("CCLabelChange", function()
local node = display.newNode()
node:setNodeEventEnabled(true)
return node
end)
--index
CCLabelChange.__index = CCLabelChange
CCLabelChange._duration = -
CCLabelChange._fromNum = -
CCLabelChange._toNum = -
CCLabelChange._target = nil
CCLabelChange._isPause = false --初步使用思路
--创建对象时候自动将本对象addChild(因为基于schedule执行)
--动作执行完毕后从父类移除,并通过某种方式标记执行完毕(例如在使用对象加字段,nil表示完毕) --因为不是继承CCActionInterval,所以需要传入target对象
function CCLabelChange:create(target, duration, fromNum, toNum)
local ret = CCLabelChange.new()
ret:init(target, duration, fromNum, toNum)
return ret
end function CCLabelChange:init(target, duration, fromNum, toNum)
self._duration = duration
self._fromNum = fromNum
self._toNum = toNum
self._target = target target:addChild(self) --基于此执行
end --两种情况下执行此方法 1、动作执行完毕 2、同类动作,旧动作在执行中,新动作需要执行,此时把旧动作移除
function CCLabelChange:selfKill()
self._target._labelChange:unscheduleUpdate() --停止scheduler
self:removeFromParentAndCleanup(true) --从父类移除
self._target._labelChange = nil --把引用删除
self._target = nil
end function CCLabelChange:pauseAction()
self._isPause = true
end function CCLabelChange:resumeAction()
self._isPause = false
end function CCLabelChange:playAction()
local oldAction = self._target._labelChange if oldAction then
--旧动作存在
oldAction:selfKill()
end self._target._labelChange = self --引用变成自己 local curTime =
local duration = self._duration local function int(x)
return x>= and math.floor(x) or math.ceil(x)
end local function updateLabelNum(dt)
if self._isPause then
return
end curTime = curTime + dt --这个类似动作里面的update的time参数
local time = curTime / duration if self._target then
if time < then --执行时间内
local tempNum = int((self._toNum - self._fromNum) *time) --取整
local num = self._fromNum + tempNum self._target:setString(num)
else
self._target:setString(self._toNum)
self:selfKill()
end else
error("target not exist")
end end self:unscheduleUpdate()
self:scheduleUpdate(updateLabelNum)
end function CCLabelChange:onEnter()
-- print("enter")
end function CCLabelChange:onExit()
print("exit")
self:unscheduleUpdate()
end

CCLabelChange