[Quick-x lua]CCLabel類數字變化動做

以前寫了個C++版本的,如今改爲lua的,node

 

二者原理是同樣,可是動做的執行方式有些微區別c++

(由於lua沒法繼承CCActionInterval類,單純使用lua的話沒法調用action的update方法)git

 

下載地址:https://github.com/chenquanjun/Quick-x-CCLabelChangegithub

c++版本的也放到這個號了ide

--演示ui

--使用方法lua

 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

 

--源代碼spa

  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 --兩種狀況下執行此方法 一、動做執行完畢 二、同類動做,舊動做在執行中,新動做須要執行,此時把舊動做移除
 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
相關文章
相關標籤/搜索