It looks like you're new here. If you want to get involved, click one of these buttons!
Score = Core.class(Sprite) function Score:init(score) self.score = score or 0 self.scoreFont = TTFont.new('fonts/serati_regular.ttf', 60) self.countTime = 500 self.scoreText = TextField.new(self.scoreFont, tostring(self.score)) self.scoreText:setLayout({wid = 80, hei = 40, flags=FontBase.TLF_CENTER}) self.scoreText:setTextColor(0xffffff) self.scoreText:setPosition(240, 120) self:addChild(self.scoreText) -- timer self.t = Timer.new(1000, 0) self.t:addEventListener(Event.TIMER, self.updateText, self) end function Score:updateText() local displayScore = tonumber(self.scoreText:getText()) if displayScore < self.score then self.scoreText:setText(tostring( displayScore + 1) ) end end function Score:addScore(add) self.score = self.score + add local displayScore = tonumber(self.scoreText:getText()) -- set timer propertie -- bigger add score means faster step local tRepeat = self.score - displayScore local tDelay = self.countTime / tRepeat self.t:stop() self.t:reset() self.t:setRepeatCount(tRepeat) self.t:setDelay(tDelay) self.t:start() return score end -- test local score = Score.new() stage:addChild(score) score:addScore(10) Timer.delayedCall(1000, function() score:addScore(200) end) |
Comments
Likes: errorpi
https://wiki.gideros.rocks/index.php/Ftf_libs#Score_Counting_TextField_.40errorpi
Please tell me if you disagree. Should I add a license (MIT)? It seems it isn't necessary to stop the timer since it will stop once repeat count is reached.
PS: I have tweaked it a little bit
PS2: your class is very cool indeed!