Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Timer event not working after first time — Gideros Forum

Timer event not working after first time

somezombiegmsomezombiegm Member
edited March 2014 in General questions
Hey, I'm currently working with timers and just found out that the "Event.TIMER" event doesn't seem to work like I thought it would.

I start a timer when the user finishes touching the screen. The event is called the number of times that I told it to. But when it finishes and the user touches the screen again, the event is called only once. That's my problem.

Full code:
test2 = gideros.class(Sprite)
 
function test2:init()
	-- Add background
	self.background = Bitmap.new(Texture.new("gfx/background.png"))
	self.background:setPosition(0,80)
	-- bgSprite: For the input listener
	self.bgSprite = Sprite.new()
	self.bgSprite:addChild(self.background)
	self:addChild(self.bgSprite)
 
	-- Init Timer
	self.cooldownTimer = Timer.new(200, 10)
	self.cooldownTimer:addEventListener(Event.TIMER, self.onTimerElapsed, self)
	self.cooldownTimer:addEventListener(Event.TIMER_COMPLETE, self.onTimerEnd, self)
 
	-- Release function
	function release(frame, event)
		self:startSubstraction()
	end	
 
	-- Input listener
	self.bgSprite:addEventListener(Event.MOUSE_UP, release, self.bgSprite)
 
end
 
function test2:startSubstraction()
	self.percentage = 100
	print("start",self.percentage)
	self.cooldownTimer:start()
end
 
function test2:onTimerElapsed()
	self.percentage = self.percentage - 10
	print("elapsed",self.percentage)	
end
 
function test2:onTimerEnd()
	self.percentage = 0
	print("end",self.percentage)
end
And here is where my problem lies:
function test2:onTimerElapsed()
	self.percentage = self.percentage - 10
	print("elapsed",self.percentage)	
end
This method works fine the first time (it's called 10 times). But for the next ones it's called only once. What I want is for it to be called 10 times every time the user finishes touching the screen. Please help.

I'm working with ar2rs Game Template and this test2.lua is one of it's scenes

Comments

  • I haven't used timers but what I normally do (and would do in your case) is remove the event when it's completed and then I'd add it again each time you want it to start, so you'd have the "self.cooldownTimer = Timer.new(200, 10)" each time, stating that you want it to be called 10 times.

    That's just a quick guess, though, witouth trying the code (and not working with timers, as I mentioned).
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Yes the problem is that internal counter of Timer is already at what you have specified, so you can also call reset method to reset it and start all over again :)
  • Ok, thanks guys. I added the reset method each time before the timer starts:
    function test2:startSubstraction()
    	self.percentage = 100
    	print("start",self.percentage)
    	self.cooldownTimer:reset()
    	self.cooldownTimer:start()
    end
    Now the onTimerElapsed method is called 10 times, as expected
  • You would think this would be automatic?
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • @SinisterSoft Of course, that's what I thought. I think that's the way it should work. But anyway, I'm ok reseting it every time.
  • somezombiegmsomezombiegm Member
    edited April 2014
    I don't know if I should ask this question on a new topic, but here it goes: Can a timer be restarted in the middle of it? I'm trying to restart a timer on an update method but I can't get it to work. I this example it would be something like:
    if self.cooldownTimer:isRunning() then
        self.cooldownTimer:stop()
        self.cooldownTimer:reset()
        self.cooldownTimer:start()
    end
    What it ends up doing is just stoping the timer, and starting it again but it doesn't actually restart. I mean, it doesn't restart from zero, it keeps going from the point where it was :(. Help!
  • ar2rsawseenar2rsawseen Maintainer
    @somezombiegm sorry, please define restart, because in my understanding restart is starting again :)
  • @ar2rsawseen Ok, I mean starting again, but from the beginning. For example, if a timer is going to be executed 10 times, and I want to "restart" it in the middle of it (let's say after 5), what I want to happen is for it to start again and be executed 10 times, not the 5 that it had left.
  • OZAppsOZApps Guru
    edited April 2014
    timer:reset()
    timer:setRepeatCount(10)
    and you can check with the
    timer:getCurrentCount()
    at what stage the timer is at.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • ar2rsawseenar2rsawseen Maintainer
    Ah, I see
    What @OZApps says is true,
    but if reset does not reset the internal counter in the middle (but only when the timer is done, I think it is a bug)

    Here is a simple test I ran and it looks like its working correctly for me:
    cooldownTimer = Timer.new(1000, 10)
    cooldownTimer:addEventListener(Event.TIMER, function(e)
    	print("TIMER", cooldownTimer:getCurrentCount())
    end)
    cooldownTimer:start()
     
    stage:addEventListener(Event.MOUSE_DOWN, function()
    	if cooldownTimer:isRunning() then
    		cooldownTimer:stop()
    		cooldownTimer:reset()
    		cooldownTimer:start()
    	end
    end)
    reseting repeat count to 1 on every touch
  • somezombiegmsomezombiegm Member
    edited April 2014
    if cooldownTimer:isRunning() then
    	cooldownTimer:stop()
    	cooldownTimer:reset()
    	cooldownTimer:start()
    end
    Now works, maybe there was something wrong with something else on my code :\">. Thanks for the example and info on timer:setRepeatCount(10) and timer:getCurrentCount() though.
  • @somezombiegame, are you saving the timer object to self? Because now when you have just one timer, it seems to work for you.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
Sign In or Register to comment.