Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How do I add a timer into my own class? — Gideros Forum

How do I add a timer into my own class?

Tom2012Tom2012 Guru
edited September 2012 in General questions
Playing around with making classes and I can't find how to make this work:

Monster = Core.class();

function Monster:init()

self.health = 100
self.speed = 3

self.timer = Timer.new(1000,5);

local function doOnTimer(event)
print("Timer event!");
end

timer:addEventListener(Event.TIMER, doOnTimer);
self.timer:start()


end


Any help much appreciated

Thanks

Tom

Comments

  • it will work just there should be

    self.timer:addEventListener(Event.TIMER, doOnTimer);

    Likes: Tom2012

    +1 -1 (+1 / -0 )Share on Facebook
  • atilimatilim Maintainer
    An addition to @hgvyas123, also I've modified your code a little:
    Monster = Core.class()
     
    function Monster:init()
    	self.health = 100
    	self.speed = 3
     
    	self.timer = Timer.new(1000, 5)
    	self.timer:addEventListener(Event.TIMER, self.onTimer, self)
    	self.timer:start()
    end
     
    function Monster:onTimer()
    	print("Timer event!")
    end
     
    Monster.new()

    Likes: Tom2012

    +1 -1 (+1 / -0 )Share on Facebook
  • Perfect, thanks. :-)
Sign In or Register to comment.