红宝石绿鞋计时器

时间:2017-02-01 06:25:24

标签: ruby timer shoes green-shoes

我为游戏构建了这个弹出倒数计时器。问题是我无法弄清楚如何更新动画,以便显示也发生变化。它到目前为止工作,但你不能看到数字在变化。它融入了00:00。到目前为止,我非常积极地解决这个问题。这是用绿色鞋子完成的。我在这里做错了什么想法?

#Timer button used for creating a countdown timer for the game.
    #Game ends when timer is zero.
    flow width: 80, height: 0.2 do
    button 'Timer', width: 1.0, height: 1.0 do
        window do
        def time_change!
           @clock = '%02d:%02d' % [@second / 60, @second % 60]
           if(@second == 0)
            alert "game is over"
            @clock = '00:00'
                close()
            para @clock, size: 50, stroke: black
           end
         end

         background whitesmoke
         @clock = '00:00'
         para @clock, size: 50, stroke: black


         @second = 10

        animate(1) do
            @second -= 1
            time_change!
            para @clock, size: 50, stroke: black

        end
    end
end

1 个答案:

答案 0 :(得分:1)

您可以替换显示时钟的当前para的文本:

Shoes.app do
  flow do 
    button 'Timer', width: 100, height: 50 do
      window width: 200, height: 100 do  #Open a child window when the button is clicked
        seconds = 3
        tenths = 0
        clock =  '%02d:%02d'

        flow do
          @p = para clock % [seconds, tenths], #Assign the clock para to an instance variable, 
            size: 50,                          #which will make the variable visible outside this block.
            stroke: black
        end

        a = animate(10) do  #Upate the clock every 1/10 of a second.
          tenths -= 1

          if tenths < 0
            tenths  = 9
            seconds -= 1
          end

          @p.text = clock % [seconds, tenths]  #Replace the clock text.

          if seconds == 0 and tenths == 0
            a.stop
            alert("game over")
            close   #=>self.close where self is the window whose block contains this code
          end
        end #animate

      end #window
    end #button
  end #flow
end #app

要验证时钟是否实际显示每个时间增量,您可以通过将animate(10)更改为animate(1)来降低速度。