Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Multiple functions - more concise way to type this? — Gideros Forum

Multiple functions - more concise way to type this?

NinjadoodleNinjadoodle Member
edited March 2016 in General questions
Hi guys

I have five monsters, and I want them all to blink at random intervals. I've setup 5 function with timers and this works fine.

I'm however wondering, whether there is a more consise way to set this up?

Thank you heaps in advance!
        local monster1BlinkTimer = Timer.new(math.random(1000, 5000))
	local function monster1Blink()
		self.monster1Eye.mc:gotoAndPlay(1)
		monster1BlinkTimer:reset()
		monster1BlinkTimer:start()
	end
	monster1BlinkTimer:addEventListener(Event.TIMER, monster1Blink)
	monster1BlinkTimer:start()
 
	local monster2BlinkTimer = Timer.new(math.random(1000, 5000))
	local function monster2Blink()
		self.monster2Eye.mc:gotoAndPlay(1)
		monster2BlinkTimer:reset()
		monster2BlinkTimer:start()
	end
	monster2BlinkTimer:addEventListener(Event.TIMER, monster2Blink)
	monster2BlinkTimer:start()
 
	local monster3BlinkTimer = Timer.new(math.random(1000, 5000))
	local function monster3Blink()
		self.monster3Eye.mc:gotoAndPlay(1)
		monster3BlinkTimer:reset()
		monster3BlinkTimer:start()
	end
	monster3BlinkTimer:addEventListener(Event.TIMER, monster3Blink)
	monster3BlinkTimer:start()
 
	local monster4BlinkTimer = Timer.new(math.random(1000, 5000))
	local function monster4Blink()
		self.monster4Eye.mc:gotoAndPlay(1)
		monster4BlinkTimer:reset()
		monster4BlinkTimer:start()
	end
	monster4BlinkTimer:addEventListener(Event.TIMER, monster4Blink)
	monster4BlinkTimer:start()
 
	local monster5BlinkTimer = Timer.new(math.random(1000, 5000))
	local function monster5Blink()
		self.monster5Eye.mc:gotoAndPlay(1)
		monster5BlinkTimer:reset()
		monster5BlinkTimer:start()
	end
	monster5BlinkTimer:addEventListener(Event.TIMER, monster5Blink)
	monster5BlinkTimer:start()

Comments

  • antixantix Member
    edited March 2016 Accepted Answer
    I don't think it's a good idea to have loads of timers going off everywhere. Having a manager that updates your monsters each frame and handles stuff like blinking and such would be a better solution. See my post in your other thread :)
  • Hi @antix

    Thank you for the reply!

    I've managed to consolidate the code by doing this ...
                    for i=1, #monsters do
     
    		monsters[i].counter = 1
     
    		monsters[i].blinkTimer = Timer.new(math.random(1000, 5000))
    		monsters[i].blink = function()
    			monsters[i].eye.mc:gotoAndPlay(1)
    			monsters[i].blinkTimer:reset()
    			monsters[i].blinkTimer:start()
    		end
    		monsters[i].blinkTimer:addEventListener(Event.TIMER, monsters[i].blink)
    		monsters[i].blinkTimer:start()
     
    	end
    This seems to work very well. These timers are only for a single level and they are going to be cleared as soon as the level is complete.

    The scope of this level doesn't really get much bigger than 10 monster with random blinks and a few event listeners.

    Would this still cause problems in your opinion?
  • antixantix Member
    It shouldn't cause problems at all. It's only when your projects start getting larger will you run into issues. I've been there, done that so I try to make everything as efficient as I can nowdays.
Sign In or Register to comment.