Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Score Counting TextField — Gideros Forum

Score Counting TextField

Just want to share. For improvement, please let me know. Thanks!
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)

Likes: MoKaLux, pie

+1 -1 (+2 / -0 )Share on Facebook

Comments

Sign In or Register to comment.