Lingo Tutorial: Timer

READING: Manual pages of the commands described here

File "onstage_timer.dir"

In the games you create you may want to have a timer on the screen to measure how much time the player has taken since her last move. How do you implement such a timer?

When clicking some button you record the ticks (60th of a second) in some variable gameTime.

on mouseDown me
   sprite(me.spriteNum).member = member("red")
end 

on mouseUp me
   global gameTime
   gameTime = the ticks
   sprite(me.spriteNum).member = member("green")
end

Notice the me.spriteNum property that gives you the channel number of the sprite you just clicked on!

Then, in the frame that you want the onscreen timer to appear, you display in a field the number of seconds passed since the variable was initiated:

on exitFrame me
  showTime
  go _movie.frame
end

on showTime
  global gameTime
  currentTime = string((the ticks - gameTime)/60)
  member("myTimer").text = currentTime
end

Function string() creates a string out of a number (the reverse of parseFloat() in Javascript).

Delaying some action

A related issue that comes up often: you would like to have the ability to delay the rest of your code execution by saying something like "wait for a few of seconds before proceeding". But the speeding Director will not let you do that easily. Here is how you implement a delay function:

 
on delay sec
  startTimer
  repeat while _movie.timer < 60*sec
    nothing
  end repeat
end

 

 

Maintained By: Takis Metaxas
Last Modified: April 16, 2009