Timing Events

Can I use a timer to see how long it takes to execute a particular loop and publish that value to the ui? [import]uid: 7719 topic_id: 6015 reply_id: 306015[/import]

I don’t know what you mean by “publish that value to the ui”, but you can certainly get millisecond timer information from system.getTimer(). It doesn’t create a timer, but timers aren’t for timing things; they’re for delaying actions.
[import]uid: 9659 topic_id: 6015 reply_id: 20784[/import]

So if you use timer.performWithDelay, is there no way to stop it? [import]uid: 27080 topic_id: 6015 reply_id: 20812[/import]

Sure - timer.cancel()
[import]uid: 9659 topic_id: 6015 reply_id: 20814[/import]

Here’s my problem:
–what happens when killed (play sound + add to score + remove timer + remove self)
local function kill(event)

media.playEventSound( brickSound )
score.setScore (score.getScore()+107)
event.target:removeEventListener(“tap”,kill)
timer.cancel()

end
–Function to make Bad Guys
local bunch = {}
–# of bad guys
local evil = 3

local function spawnBadguy(event)

local myAnim = movieclip.newAnim{“cube01.png”, “cube02.png”, “cube03.png”, “cube04.png”, “cube05.png”, “cube06.png”}
p0_subGroup1:insert(myAnim)

local x = math.random(50,200)
local y = 200
local i = event.count

bunch[i] = myAnim
bunch[i].x = x
bunch[i].y = y

p0_subGroup2:insert(bunch[i])

local growth = .01

local function grow()

bunch[i].xScale = bunch[i].xScale + growth
bunch[i].yScale = bunch[i].yScale + growth

end

–Make Bad Guys Come at You (time between frames, function,number of scales)
timer.performWithDelay(5,grow,1000)

– Start the animations
bunch[i]:play() – play all frames
bunch[i]:addEventListener(“tap”,kill)

end
–Spawn Bad Guy
timer.performWithDelay(1000,spawnBadguy,evil) --spawn 2 seconds apart
For the life of me I can’t figure out how to cancel that timer that makes the animated objects scale larger!

Any help would be great! [import]uid: 27080 topic_id: 6015 reply_id: 20817[/import]

First of all, you’ll be more likely to get help in general if you use the <code>…</code> tags to delimit your code. I have a real hard time reading code like that.

Secondly, did you carefully read the documentation for timers? It’s not long, and they have a pretty clear example under timer.cancel().

You can’t just say timer.cancel() because you can have 1000 timers going. You need to tell it which timer to cancel. You get a timer object when you create it, or you can use event.source, as the example shows.

Good luck!
[import]uid: 9659 topic_id: 6015 reply_id: 20818[/import]

Sorry for the hard read, I wasn’t sure how to get it to show up like delimited code.

I didn’t quite understand the documentation at first but now I understand that I need to assign a timer a timerID; which was confusing to me because if I say timer = timer.performWithDelay(crap, the timerID is not a local var?

Appreciate you looking at it at least!
[import]uid: 27080 topic_id: 6015 reply_id: 20819[/import]

I agree, the two simple timer examples are not that clear.
Ok for :

local t = {}  
 function t:timer( event )  
 local count = event.count  
 print( "Table listener called " .. count .. " time(s)" )  
 if count \>= 3 then  
 timer.cancel( event.source ) -- after 3rd invocation, cancel timer  
 end  
 end  
   
   
 -- Register to call t's timer method an infinite number of times  
 timer.performWithDelay( 1000, t, 0 )  

But what If I just want something like that :

timer.performWithDelay( 0, randomCell, n )  
-- do some stuff  
-- now it's time to remove my listeners and timers  
timer.cancel (timerid needed) -- what is that "timerid" ??  

Thx

[import]uid: 9328 topic_id: 6015 reply_id: 21558[/import]