Tutorial: Counting Time + Practical Usage

I wrote an article that explains how to count time in your game (without using Corona’s timer library), and how you might use it in your games/apps:

Read it here:
http://coronasdk.com/2010/creating-a-time-counter-and-practical-usage/

I didn’t run any tests or benchmarks, so if you HAVE, could you please answer the following question:

Does what I describe in the article provide any performance benefits over using Corona’s timer library?

This is my train of thought… Since os.time() is always running no matter what, it would make sense that it could potentially provide a performance boost over Corona’s timer library whenever using it in place of performWithDelay makes sense, that is. [import]uid: 7849 topic_id: 2576 reply_id: 302576[/import]

Nice tutorial Jonathan. Thanks for posting it.

-Tom [import]uid: 7559 topic_id: 2576 reply_id: 7616[/import]

look great
thats one peice of the puzzle for me thanks
like your site looking forward to many more tutorials [import]uid: 7911 topic_id: 2576 reply_id: 7621[/import]

@Tom: No problem! Hopefully others will find it useful as well.

@jstrahan: Thanks a lot, and more tutorials are definitely coming. [import]uid: 7849 topic_id: 2576 reply_id: 7626[/import]

I will be testing the performance on this.

However so far, we can confirm that, system.getTimer() gives you real timing such that not affected by CPU lag, or FPS slowdown.

timer.perform… gets affected by CPU lag which is not very precise when a sudden burst of activities occur. [import]uid: 6066 topic_id: 2576 reply_id: 25001[/import]

That link is broken for me, anyone have an updated URL? [import]uid: 6084 topic_id: 2576 reply_id: 25103[/import]

This should be the updated link:

http://ideveloper.kodingen.com/2010/creating-a-time-counter-and-practical-usage/ [import]uid: 14598 topic_id: 2576 reply_id: 26980[/import]

in build 591

timer.pause(sometimer) -doesnt work!!!

ARRRGH
[import]uid: 45315 topic_id: 2576 reply_id: 58503[/import]

It wasn’t implemented in build .591;

Supported on operating systems and platforms for build numbers shown:
Mac OS X: Build 2011.596
Windows: Build 2011.596
iOS: Build 2011.596
Android: Build 2011.596

http://developer.anscamobile.com/reference/index/timerpause

I see you posted there also. D’oh! [import]uid: 52491 topic_id: 2576 reply_id: 58563[/import]

Hello!
I am trying to create a health bar style timer that slowly decreases as you play the game. The game is a match memory card style game and each time you get two card matched, the health or time increases. If you don’t match all the cards and your health runs out, the game is over.
How do I implement this to function with a green health bar graphic? And where the heck to I put it?

here is my Main Lua

–Set Global width and height variables
_W = display.contentWidth;
_H = display.contentHeight;

–Hide status bar
display.setStatusBar(display.HiddenStatusBar);

display.setStatusBar(display.HiddenStatusBar)

local background = display.newImage (“background.png”)


– background music –

backgroundMusic = audio.loadStream(“billyJean.caf”)

backgroundMusicChannel = audio.play( backgroundMusic, { channel=0, loops=1, fadein=20000 } )

– Michaels Head Normal –

local MichaelNormal = display.newImage (“MichaelNormal.png”)
MichaelNormal.x = 130
MichaelNormal.y = 85

–Declare a totalButtons variable to track number of buttons on screen
local totalButtons = 0

–Declare variable to track button select
local secondSelect = 0
local checkForMatch = false

–Declare button, buttonCover, and buttonImages table
local button = {}
local buttonCover = {}
local buttonImages = {1,1, 2,2, 3,3, 4,4, 5,5, 6,6,}
–Notify player if match is found or not
local matchText = display.newText("", 0, 228, native.systemFont, 18)
matchText:setReferencePoint(display.CenterReferencePoint)
matchText:setTextColor(51, 255, 102)
matchText.x = _W/2

–Set starting point for button grid
x = -27
y = -80
–Set up game function
function game(object, event)
if(event.phase == “began”) then
if(checkForMatch == false and secondSelect == 0) then
–Flip over first button
buttonCover[object.number].isVisible = false;
lastButton = object
checkForMatch = true

elseif(checkForMatch == true) then
if(secondSelect == 0) then
–Flip over second button
buttonCover[object.number].isVisible = false;
secondSelect = 1;
–If buttons do not match, flip buttons over

if(lastButton.myName ~= object.myName) then
matchText.text = “Match Not Found!”;
timer.performWithDelay(800, function()
matchText.text = " ";
checkForMatch = false;
secondSelect = 0;
buttonCover[lastButton.number].isVisible = true;
buttonCover[object.number].isVisible = true;
local MichaelNormal = display.newImage (“MichaelSick.png”)
MichaelNormal.x = 130
MichaelNormal.y = 85
media.playEventSound(“imsorry.caf”)
end, 1)

–If buttons DO match, remove buttons
elseif(lastButton.myName == object.myName) then
matchText.text = “Match Found!”;
timer.performWithDelay(1250, function()
matchText.text = " ";
checkForMatch = false;
secondSelect = 0;
lastButton:removeSelf();
object:removeSelf();
buttonCover[lastButton.number]:removeSelf();
buttonCover[object.number]:removeSelf();
local MichaelNormal = display.newImage (“MichaelHealth.png”)
MichaelNormal.x = 130
MichaelNormal.y = 85
media.playEventSound(“good.caf”)
end, 1)
end
end
end
end
end

–Place buttons on screen
for count = 1,4 do
x = x + 75
y = 214

for insideCount = 1,3 do
y = y + 75

–Assign each image a random location on grid
temp = math.random(1,#buttonImages)
button[count] = display.newImage(buttonImages[temp] … “.png”);

–Position the button
button[count].x = x;
button[count].y = y;

–Give each a button a name
button[count].myName = buttonImages[temp]
button[count].number = totalButtons

–Remove button from buttonImages table
table.remove(buttonImages, temp)

–Set a cover to hide the button image
buttonCover[totalButtons] = display.newImage(“button.png”);
buttonCover[totalButtons].x = x; buttonCover[totalButtons].y = y;
totalButtons = totalButtons + 1

–Attach listener event to each button
button[count].touch = game
button[count]:addEventListener( “touch”, button[count], playfalse1 )

end
end
[import]uid: 94909 topic_id: 2576 reply_id: 59261[/import]

How to use timer as a score to implement in a game? [import]uid: 82446 topic_id: 2576 reply_id: 60803[/import]

Hey Rahul, you can actually check this out; http://techority.com/2011/03/25/adding-a-score-to-your-app-that-goes-up-based-on-a-timer/

I believe that should do exactly what you want :slight_smile:

Peach [import]uid: 52491 topic_id: 2576 reply_id: 60920[/import]

Thanks for the reply but i have already come up with this.But I am not sure that whether this would store the score and retain it even after the application quits ?Will this do that I am asking? [import]uid: 82446 topic_id: 2576 reply_id: 61037[/import]

No, if you want to save data you need to do that too. Nothing is ever going to be saved unless you explicitly tell it to save. [import]uid: 52491 topic_id: 2576 reply_id: 61144[/import]

After doing that how can we save the data explicitly?is there any way to do that? [import]uid: 82446 topic_id: 2576 reply_id: 61534[/import]

There are tutorials around about how to do this, or see here for something really neat that Jon Beebe put together that greatly simplifies saving/loading; http://jonbeebe.tumblr.com/post/2320935925/beebegames-class-for-corona-sdk [import]uid: 52491 topic_id: 2576 reply_id: 61602[/import]