Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Countdown timer — Gideros Forum

Countdown timer

tytadastytadas Member
edited August 2016 in General questions
http://giderosmobile.com/tools/countdown

This is the code to create countdown timer. I'm doing everything like in the example, but this line local cd = Countdown.new({ and showing me error : "attempt to index global 'Countdown' (a nil value)"
Yes and I have created Countdown.lua file.

I believe I'm not the only with this error, maybe Countdown method doesn't work anymore and theres a different function to call countdown timer?

Comments

  • Have you downloaded and run the example from the github repo? I downloaded it all and it works. If you are just doing this from your own code then maybe you aren't creating the Countdown instance?

    May I also ask what you are trying to achieve with the countdown class? Do you just need a simple second countdown or is it something more complicated?
  • tytadastytadas Member
    edited August 2016
    I just need 10 seconds countdown on the screen, and after 10 seconds , it should display "Happy Birthday" :D

    Likes: talis

    +1 -1 (+1 / -0 )Share on Facebook
  • tytadastytadas Member
    edited August 2016
    I tried to create my own code because I have started reading a PIL book, but nop, not enough experience yet. I'll try to download now
  • tytadastytadas Member
    edited August 2016
    Somehow It still doesnt work for me.. I'll try to recheck it
  • antixantix Member
    edited August 2016 Accepted Answer
    @tytadas using that class for a simple 10 second countdown might be a bit like trying to kill an ant with a sledge hammer ;)

    Maybe you might try this small class instead..
    --[[
     
    name:   CountDown
    vers:   1.0.0
    desc:   A simple counter that counts down to 0 and runs a function on completion.
    auth:   by Cliff Earl, Antix Software
    date:   August 2016
    legal:  Distribute freely but please leave my credits intact.
     
    --]]
     
    CountDown = Core.class(Sprite)
     
    function CountDown:init(options) -- Setup counter
     
      local props = { -- Default options
        x           = 16,
        y           = 16,
        scale       = 1,
        alpha       = 1,
        color       = 0x000000,
        font        = nil,
        duration    = 10,
        onComplete  = nil,
      }
     
      if options then
        for key, value in pairs(options) do
          props[key]= value
        end
      end
     
      self. countingDown = false
      self.counter = 0
     
      local text = TextField.new(props.font, props.duration) -- Create text
      text:setTextColor(props.color)
      self:addChild(text)
     
      self:setPosition(props.x, props.y) -- Position, etc
      self:setScale(props.scale)
      self:setAlpha(props.alpha)
     
      self.props = props
      self.text = text
    end
     
    function CountDown:start() -- Start the counter
      local props = self.props
      self.counter = props.duration + 1
      self:show(true)
      self.countingDown = true
    end
     
    function CountDown:setDuration(duration) -- Set duration of counter
      local props = self.props
      props.duration = duration
    end
     
    function CountDown:show(state) -- Show or hide counter
      self:setVisible(state)
    end
     
    function CountDown:updateText() -- Update the counter text
      local text = self.text
      local counter= math.floor(self.counter)
      text:setText(counter)
    end
     
    function CountDown:update(dt) -- Call this every frame
     
      if not self.countingDown then return end -- If not running then just exit
     
      local counter = self.counter - dt -- Decrement counter
     
      self:updateText() -- Update the text
     
      if counter <= 1 then -- Has counter run down?
     
        self.countingDown = false -- stop any further counting down
        self:show(false) -- Hide ourselves
        if self.props.onComplete then
          self.props.onComplete() -- Call function when the count down reaches 0
        end
     
      end
      self.counter = counter
     
    end
    Here's an example of how it can be used..
    local counter = CountDown.new( {
        x           = 64,
        y           = 64,
        scale       = 5,
        alpha       = 1,
        color       = 0x000000,
        font        = nil,
        duration    = 15,
        onComplete  = function() -- This function gets run when the timer gets to zero
          print("complete")
        end,
      } )
     
      stage:addChild(counter)
      counter:start()
     
    local function update(e) -- Enter Frame Event
      local dt = e.deltaTime
     
      counter:update(dt) -- Update counter
     
    end
     
    stage:addEventListener(Event.ENTER_FRAME, update)
    zip
    zip
    CountDown.zip
    3K

    Likes: tytadas

    +1 -1 (+1 / -0 )Share on Facebook
  • @antix Wow man, you Da real MVP! Thank you for this sample! I will leave the credits! Really thanks! Works as well as Perwoll!

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • You're welcome :)

    Likes: tytadas

    +1 -1 (+1 / -0 )Share on Facebook
  • this deltaTime property of the event is undocumented, isn't it? nice one.

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • I think there's some mention of it in the reference, that's where I came across it. It's a pretty handy var :)
  • @antix Nice one!. I might use this, too.

    Likes: antix, tytadas

    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.