Game Development Reference
In-Depth Information
stage:addEventListener(Event.APPLICATION_START,
function(event)
print("Started")
end)
You can also monitor the time when the application is suspended or resumes, so you can save the
state of your app and then load it again when the app is resumed.
Timer
You can set the timer in Gideros Studio by using the
Timer.new
function and then adding an event
listener to listen for when the timer fires. You can also listen for the timer-complete event, which
occurs after all of the timer loops have fired. The default value for loops is 0, which is the same as
indicating infinite loops for the timer.
local theTimer = Timer.new(1000, 5)
theTimer:addEventListener(Event.TIMER,
function(event)
print("Timer fired")
end)
theTimer:addEventListener(Event.TIMER_COMPLETE,
function(event)
print("All timer loops completed")
end)
This code creates a new timer that fires at a set frequency and runs a specified number of times. In
this case, the frequency is 1,000 ms and the loop runs five times. The timer then has to be started
using the
start
function.
theTimer:start()
Now you can see the results in the output window, showing that the timer fires every 1 second
(1,000 ms) and completes after firing five times.
The timer has a
currentCount
property (a read-only property) that holds the number of times the
timer has fired.
repeatCount
holds the number of times the timer should repeat, and the timer stops
once the
currentCount
value reaches the
repeatCount
value. We can use the
reset
function to set
the
currentCount
to 0. You can set
repeatCount
to use the
setRepeatCount
function, and you can
alter the delay using the
setDelay
function.
The timer can be finely controlled using the
start
,
pause
, and
stop
functions. Further, all of the active
timers can be paused, stopped, or resumed using the
pauseAll
,
resumeAll
, and
stopAll
functions,
respectively. The timer can also be queried using the
isRunning
function, which returns whether the
timer is running or not.
If you need to trigger a function after a delay, you can use the
Timer.delayedCall
function. This
function takes three parameters: a delay time (after which the function is invoked), a function, and a
custom parameter that you can pass to the function.