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

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

 

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

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

 

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

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

--演示

--使用方法

 1     do
 2         local label = CCLabelTTF:create("0", "Arial", 60)
 3         label:setPosition(display.cx - 100, display.cy)
 4         self:addChild(label)
 5         local action = CCLabelChange:create(label, 60, 1, 100)
 6 
 7         action:playAction()       
 8     end
 9 
10     do
11         local label = CCLabelTTF:create("0", "Arial", 60)
12         label:setPosition(display.cx + 100, display.cy)
13         label:setColor(ccc3(255, 0, 0))
14         self:addChild(label)
15         local action = CCLabelChange:create(label, 60, 100, 1)
16 
17         action:playAction()       
18     end
19 
20     do
21         local label = CCLabelTTF:create("0", "Arial", 60)
22         label:setPosition(display.cx - 100, display.cy - 100)
23         label:setColor(ccc3(0, 0, 255))
24         self:addChild(label)
25         local action = CCLabelChange:create(label, 60, -100, 100)
26 
27         action:playAction()       
28     end
29 
30     do
31         local label = CCLabelTTF:create("0", "Arial", 60)
32         label:setPosition(display.cx + 100, display.cy - 100)
33         label:setColor(ccc3(0, 255, 0))
34         self:addChild(label)
35         local action = CCLabelChange:create(label, 20, 1, 1000000)
36 
37         action:playAction()       
38     end
View Code

 

--源代码

  1 CCLabelChange = class("CCLabelChange", function()
  2     local node = display.newNode()
  3     node:setNodeEventEnabled(true)
  4     return node
  5 end)            
  6 --index
  7 CCLabelChange.__index              = CCLabelChange
  8 CCLabelChange._duration         = -1
  9 CCLabelChange._fromNum          = -1
 10 CCLabelChange._toNum            = -1
 11 CCLabelChange._target           = nil
 12 CCLabelChange._isPause          = false
 13 
 14 --初步使用思路
 15 --创建对象时候自动将本对象addChild(因为基于schedule执行)
 16 --动作执行完毕后从父类移除,并通过某种方式标记执行完毕(例如在使用对象加字段,nil表示完毕)
 17 
 18 --因为不是继承CCActionInterval,所以需要传入target对象
 19 function CCLabelChange:create(target, duration, fromNum, toNum)
 20     local ret = CCLabelChange.new()
 21     ret:init(target, duration, fromNum, toNum)
 22     return ret
 23 end
 24 
 25 function CCLabelChange:init(target, duration, fromNum, toNum)
 26     self._duration = duration
 27     self._fromNum = fromNum
 28     self._toNum = toNum
 29     self._target = target
 30 
 31     target:addChild(self) --基于此执行
 32 end
 33 
 34 --两种情况下执行此方法 1、动作执行完毕 2、同类动作,旧动作在执行中,新动作需要执行,此时把旧动作移除
 35 function CCLabelChange:selfKill()
 36     self._target._labelChange:unscheduleUpdate() --停止scheduler
 37     self:removeFromParentAndCleanup(true) --从父类移除
 38     self._target._labelChange = nil --把引用删除
 39     self._target = nil
 40 end
 41 
 42 function CCLabelChange:pauseAction()
 43     self._isPause = true
 44 end
 45 
 46 function CCLabelChange:resumeAction()
 47     self._isPause = false
 48 end
 49 
 50 function CCLabelChange:playAction()
 51     local oldAction = self._target._labelChange
 52 
 53     if oldAction then
 54         --旧动作存在
 55         oldAction:selfKill()
 56     end
 57 
 58     self._target._labelChange = self --引用变成自己
 59 
 60     local curTime = 0
 61     local duration = self._duration
 62 
 63     local function int(x) 
 64         return x>=0 and math.floor(x) or math.ceil(x)
 65     end
 66 
 67     local function updateLabelNum(dt)
 68                 if self._isPause then
 69                     return
 70                 end
 71 
 72                 curTime = curTime + dt
 73 
 74                 --这个类似动作里面的update的time参数
 75                 local time = curTime / duration
 76 
 77                 if self._target then 
 78                     if time < 1 then --执行时间内
 79                         local tempNum = int((self._toNum - self._fromNum) *time) --取整
 80                         local num = self._fromNum + tempNum
 81 
 82                         self._target:setString(num)
 83                     else
 84                         self._target:setString(self._toNum)
 85                         self:selfKill()
 86                     end
 87 
 88                 else
 89                     error("target not exist")
 90                 end
 91 
 92     end
 93 
 94     self:unscheduleUpdate()
 95     self:scheduleUpdate(updateLabelNum)
 96 end
 97 
 98 function CCLabelChange:onEnter()
 99     -- print("enter")
100 end
101 
102 function CCLabelChange:onExit()
103     print("exit")
104     self:unscheduleUpdate()
105 end
CCLabelChange

 

转载于:https://www.cnblogs.com/creeper/p/3685072.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值