Displaying a count down (blocking)

This lesson demonstrates how to create a blocking count down in LiveCode.

The example used here is the endless song 99 Bottles of Beer on the Wall. The example displays the song interactively, one verse at a time.

The mouseUp message

The mouseUp message is sent when the user clicks an object, in this case, a button. It's possible to include this code in a different handler; putting it in a button's mouseUp handler is just convenient for this example. When the mouseUp message is sent to the button, it triggers the mouseUp handler.

99 Bottles of Beer on the Wall

This handler displays the endless verses of the song "99 Bottles of Beer on the Wall". In case you're not familiar with the song, it goes like this:

99 bottles of beer on the wall, 99 bottles of beer

You take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer

You take one down and pass it around, 97 bottles of beer on the wall.

...and so on. The song continues, decreasing the number of bottles by one with each verse, until there's just one bottle left:

One more bottle of beer on the wall, one more bottle of beer

You take one down and pass it around, no more bottles of beer on the wall.

No more bottles of beer on the wall, no more bottles of beer

You go to the store and buy some more, 99 bottles of beer on the wall.

99 bottles of beer on the wall...

...and the song starts over from the beginning.

The mouseUp handler

The example displays the song interactively, one verse at a time. The handler assumes there is a field named "Song" to display successive verses. When the user clicks, the handler starts a repeat loop to display each verse of the song from the beginning. The tBottlesLeft variable keeps track of which verse is next: it starts out at 99, and each time through the loop, it's decreased by one. After a one-second delay imposed by the wait command, the handler returns to the top of the loop and displays the next verse. You can change the delay time, depending on how long you think it will take to read one verse and how long you want the song to take.  

put 99 into tBottlesLeft

switch tBottlesLeft

case zero

## last verse, zero bottles left

put bottlePhrase(tBottlesLeft) && "on the wall," \

&& bottlePhrase(tBottlesLeft) & return \

& "Go to the store and buy some more," \

&& bottlePhrase(99) \

&& "on the wall." into field "Song"

break

default

## all other verses

put bottlePhrase(tBottlesLeft) && "on the wall," \

&& bottlePhrase(tBottlesLeft) & return \

& "Take one down and pass it around," \

&& bottlePhrase(tBottlesLeft - 1) \

&& "on the wall." into field "Song"

end switch

subtract 1 from tBottlesLeft ## for the next verse

When the song finishes, and the number of bottles left is zero, the handler sets it back to 99 to start the song over from the beginning:

if tBottlesLeft < zero then

put 99 into tBottlesLeft

end if

wait for 1 second

Ending the loop

The loop continues for all the verses, then starts over again, until the user clicks the button and stops the song. The repeat loop ends when the mouseClick function returns true, which happens if the mouse has been clicked.

repeat until the mouseClick

...

end repeat

 

## when the repeat loop ends with a click, the song is over:

put empty into field "Song"

The mouseUp handler code

on mouseUp

local tBottlesLeft

 

put 99 into tBottlesLeft

repeat until the mouseClick

    ## stop song when the user clicks

    ## show a verse, using the bottlePhrase function

      ## do one verse on each iteration of the loop:

switch tBottlesLeft

case zero

## last verse, zero bottles left

put bottlePhrase(tBottlesLeft) && "on the wall," \

&& bottlePhrase(tBottlesLeft) & return \

& "Go to the store and buy some more," \

&& bottlePhrase(99) \

&& "on the wall." into field "Song"

break

default

## all other verses

put bottlePhrase(tBottlesLeft) && "on the wall," \

&& bottlePhrase(tBottlesLeft) & return \

& "Take one down and pass it around," \

&& bottlePhrase(tBottlesLeft - 1) \

&& "on the wall." into field "Song"

end switch

subtract 1 from tBottlesLeft ## for the next verse

   

if tBottlesLeft < zero then

put 99 into tBottlesLeft

end if

wait for 1 second

end repeat

 

## when the repeat loop ends with a click, the song is over:

put empty into field "Song"

end mouseUp

The bottlePhrase function code

function bottlePhrase pNumber

## generate the "base phrase" used to make a verse

switch pNumber

case zero

return "No more bottles of beer"

break

case one

return "One last bottle of beer"

break

default

return pNumber && "bottles of beer"

end switch

end bottlePhrase

Inspiration

This example was inspired by a discussion on the LiveCoce mailing list with David Vaughan. The topic was in turn inspired by the "99 Bottles of Beer" site at /http://99-bottles-of-beer.ls-la.net, featuring 99 Bottles of Beer code in 454 programming languages (and counting).

0 Comments

Add your comment

E-Mail me when someone replies to this comment