Title: Tutorial Eight: Timers Post by: Access_Denied on June 14, 2007, 03:27:10 AM In
this exciting lesson, we'll be coving timers. Timers in Lua measure
milliseconds and are extremely easy to use. So let's get to it.
--Timers --August 21,2006 orange = Color.new(255,128,0) Now, we need a timer. Creating a timer is very easy. counter = Timer.new() Easy right? We just made a timer named "counter". Now, on to our main loop: oldpad = Controls.read() while true do screen:clear() pad = Controls.read() time = counter:time() Now, that fourth line defines the counter't time as "time". So if we want to print the time to the screen, we use the variable "time". This must be put after the main loop. Now, we need to put our timer to use. So let's begin. if pad:cross() and oldpad:cross() ~= pad:cross() then counter:start() end In case you couldn't figure it out, pressing X in our program will start the timer. if pad:circle() and oldpad:circle() ~= pad:circle() then counter:stop() end That will stop the timer. if pad:triangle() and oldpad:triangle() ~= pad:triangle() then counter:reset(0) end And that will reset the counter to 0. Now, we want to be able to see our timer, so add this: screen:print(100,100,time,orange) This prints our timer's time to the screen. Now finish up. screen.waitVblankStart() screen.flip() oldpad = pad end See ya in the next tut! |