![]() Don't worry about the initial x position of 0 for each object - in this enhanced version, we'll actually use modifications within the existing updateTime() function to set both the frame and position of every digit. The first two objects represent the minutes digits and the second two objects are for the seconds. Local secondsSingle = display.newSprite( sheet, digitSequence ) Local secondsDouble = display.newSprite( sheet, digitSequence ) Local minutesDouble = display.newSprite( sheet, digitSequence ) Local minutesSingle = display.newSprite( sheet, digitSequence ) Next, define a basic sprite sequence and create/position the colon object on screen:.Essentially, this new code defines the frames of the image sheet (11 in total) and creates an image sheet ( sheet). This tutorial won't explain the above configuration in detail - if you need further understanding on the topic, please see the Image Sheets guide. Local sheet = graphics.newImageSheet( "timer-digits.png", sheetOptions ) Local secondsLeft = 10 * 60 - 10 minutes * 60 seconds By passing in minutes first, then seconds, we get the traditional MM:SS type display. Finally, string.format() takes one variable per placeholder, filling in the values from left to right. The leading 0 says that if the number is too small to fit in the two character spaces, prefix it with enough zeros to match the requested format - in other words, if we have a seconds value of 7, it will be formatted and displayed as 07. ![]() The % sign defines the beginning of a placeholder and the 02d says that we want a whole number ( d for decimal) and use two ( 2) character spaces to display it. The string format for our purposes is "%02d:%02d" which might look cryptic, but it's actually pretty basic. Using it, we can specify placeholders that have a certain format and then pass in values to fill those placeholders. We could use os.date() for this, but string.format() will do the job more easily. To make the time display in a typical time format ( MM:SS), we need to format a string. This is easy to accomplish using the modulus operator ( %) - essentially secondsLeft % 60 will give us the number of seconds without the minutes. For the seconds variable, we do need the fractional part because that will be the visual "seconds" in the display. Thus, we use math.floor() to generate a whole number and then we store the value as the minutes variable. This will present us with a fractional value, but we don't need the fractional part for the "minutes" aspect of the display. To compute the minutes, we need to reverse the time calculation that we used to convert the time into seconds, so we simply divide the number of remaining seconds by 60. If we didn't do this, the timer wouldn't count down! Optionally, we could add a conditional test after this line to see if secondsLeft equals 0 and, if so, trigger some event indicating that the clock has reached zero. The first step in this function is critical - subtract 1 from secondsLeft. Let's explore this function in greater detail: Local timeDisplay = string.format( "%02d:%02d", minutes, seconds ) Local minutes = math.floor( secondsLeft / 60 ) ![]() Time is tracked in seconds convert it to minutes and seconds That's a good start, but it doesn't actually do anything! Let's fix that by first writing a function which updates the visual time display: local function updateTime( event ) If you run the code above, you'll see a large display of 10:00 on the screen.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |