1. Computing
Send to a Friend via Email

Click Blocks and Animation

By

5 of 5

The Timer Application Code
Click Blocks and Animation

Note that this application is not accurate. The complete version contains some extra code that does time calculations to make the timer accurate. I've uploaded this version to The Shoebox for public access. This small example of the application will add fractions of seconds as full seconds when pausing or unpausing the timer, as it does not account for small inaccuracies in the interveral at which animate calls the block.

Shoes.app :width => 200, :height => 120 do
  @seconds = 0
  @paused = false

  def display_time
    @display.clear do
      title "%02d:%02d:%02d" % [
        @seconds / (60*60),
        @seconds / 60 % 60,
        @seconds % 60
      ], :stroke => @paused ? gray : black
    end
  end

  @display = stack :margin => 10
  display_time

  button "Pause", :width => '50%' do
    @paused = !@paused
    display_time
  end
  
  button "Reset", :width => '50%' do
    @seconds = 0
    display_time
  end

  animate(1) do
    @seconds += 1 unless @paused
    display_time
  end
end

©2014 About.com. All rights reserved.