Gideros Countdown class displays (and updates) time left provided by user. Time can be provided in any time units, and they will be recalculated to specified time units. It is possible to specify which units or text will appear in countdown and which text must be hidden if certain unit reaches 0 by using labels for each time unit.

Package contains Countdown.lua and example Gideros project.

 

Contents

Download

Example codes

--you need to create text field 
--where to output specific units
local seconds = TextField.new(nil, "")
seconds:setPosition(20, 120)

--you can provide some text with time unit
--which will be hidden when unit reaches zero
local minutes = TextField.new(nil, "somet text here {i} and there")
minutes:setPosition(20, 100)

--you can use font with your countdown
local tahoma = TTFont.new("tahoma.ttf", 15)
local hours = TextField.new(tahoma, "{h} hours left")
hours:setPosition(20, 80)

--you can also use TextWrap class
local days = TextWrap.new("{d} days left", 320, "center")
days:setPosition(0, 60)

--you don't need to use all possible time units
--if you don't use one, it will be 
--automatically recalculated to lower ones
--local weeks = TextField.new(nil, "{w} weeks left")
--weeks:setPosition(20, 40)

--add other possible time units are months and years
local months = TextField.new(nil, "{m} months left")
months:setPosition(20, 20)

local years = TextField.new(nil, "{y} years left")
years:setPosition(20, 0)


--textfield, or sprite or anything 
--that is hidden and can be made visible using
--setVisible(true) method when countdown ends
local ended = TextField.new(nil, "Countdown Ended")
ended:setPosition(20, 120)


--create coutndown
local cd = Countdown.new({

	--time to specific timestamp
	--time = 1639324800
	
	--or provide time left
	year = 1,
	month = 0,
	week = 0,
	day = 0,
	hour = 0,
	min = 0,
	sec = 10,
	
	--textfields where to output countdown
	label_sec = seconds,
	label_min = minutes,
	label_hour = hours,
	label_day = days,
	--label_week = weeks,
	label_month = months,
	label_year = years,
	
	--TextField to show when countdown ended
	label_end = ended,
	--hide ended units
	hide_zeros = true,
	--use leading zeros for hours, minutes and seconds
	leading_zeros = true,
	--callback function on countdown end
	onend = function() print("Ended") end,
	--callback function on each coutndown step
	--provides seconds left till end of countdown
	onstep = function(seconds) print(seconds) end
})

cd:setPosition(0,10)
stage:addChild(cd)

Examples video

Method list

Constructor

Back to method list

Method name Countdown.new(config)
Description Creates class instance with provided configuration
Input parameters

table config - lua table with configuration:

  • int time - timestamp to the target date, provide target timestamp, of units left in parameters below(default: 0)
  • int year - how many years have left till countdown ends, if timestamp not provided (default: 0)
  • int month - how many months have left till countdown ends, if timestamp not provided (default: 0)
  • int week - how many weeks have left till countdown ends, if timestamp not provided (default: 0)
  • int day - how many days have left till countdown ends, if timestamp not provided (default: 0)
  • int hour - how many hours have left till countdown ends, if timestamp not provided (default: 0)
  • int min - how many minutes have left till countdown ends, if timestamp not provided (default: 0)
  • int sec - how many seconds have left till countdown ends, if timestamp not provided (default: 0)
  • TextField label_year - TextField where to display years, if not provided, years will be recalculated to lesser units. If additional text should appear with year units, provide it as TextField's text in this format 'Some text {y} more text' (default: none)
  • TextField label_month - TextField where to display months, if not provided, months will be recalculated to lesser units. If additional text should appear with months units, provide it as TextField's text in this format 'Some text {m} more text' (default: none)
  • TextField label_week - TextField where to display weeks, if not provided, weeks will be recalculated to lesser units. If additional text should appear with weeks units, provide it as TextField's text in this format 'Some text {w} more text' (default: none)
  • TextField label_day - TextField where to display days, if not provided, days will be recalculated to lesser units. If additional text should appear with days units, provide it as TextField's text in this format 'Some text {d} more text' (default: none)
  • TextField label_hour - TextField where to display hours, if not provided, hours will be recalculated to lesser units. If additional text should appear with hour units, provide it as TextField's text in this format 'Some text {h} more text' (default: none)
  • TextField label_min - TextField where to display minutes, if not provided, minutes will be recalculated to lesser units. If additional text should appear with minute units, provide it as TextField's text in this format 'Some text {i} more text' (default: none)
  • TextField label_sec - TextField where to display seconds, if not provided, seconds will be recalculated to lesser units. If additional text should appear with second units, provide it as TextField's text in this format 'Some text {s} more text' (default: none)
  • TextField label_end - TextField to make visible after countdown ended (default: none)
  • bool hide_zeros - hide highest units that have reached zero, for example hides '0' years in '0 years 2 days 3 hours 4 secons' (default: 'true')
  • bool leading_zeros - display leading zeros for hours, minutes and seconds, when their value < 10 (default: 'true')
  • function onstep - callback function which is called every second and provides how many seconds is left (till countdown end) as function parameter
  • function onend - callback function which is called when countdown is ended

 

Start countdown

Back to method list

Method name start()
Description Start countdown if countdown was stopped

Stop countdown

Back to method list

Method name stop()
Description Stop countdown

Get seconds left

Back to method list

Method name getSeconds()
Description Get time left in seconds till countdown ends

Latest changes

None for now