How to change Event Handlers without removing the listener

There was a question asked by someone about how to have an event handler set on an object and then set another one on it. Here's the better way (in my opinion)

Firstly we need to confirm what we are after, are we after just having a different handler or are we after capturing other events. The core difference between the two is

#1. Having different Handlers
 obj:addEventListener("touch", onTouch1)
 obj:addEventListener("touch", onTouch2)

we need that first onTouch1 is registered and then onTouch2 is registered.

#2. Having different Event Types is
 obj:addEventListener("touch", onTouch1)
 obj:addEventListener("tap", onTouch1)

Now when we need to manage things like in #1. It is easier to have a boolean that helps us branch off to either of the handlers. Now I am not aware of the reason on why such a thing might be required, but if there is such an requirement then this is how it can be resolved.

 local useFunction1 = true

 local function1(event)
  print ( "Called from Func1" )
 end

 local function2(event)
  print ( "Called from Func2" )
 end

 local function handleIt ( event )
  if useFunction1 == true then
    useFunction1 = false
    function1 ( event )
  else
    function2 ( event )
  end
 end

 obj:addEventListener ( "touch", handleIt )

Now, that is the easiest way to handle it. While thinking hard of a case scenario, I came up with one that would be very useful and would work best with this logic and needed a little change.

Let's say you have a demo screen, one where you would want to demonstrate something, so you have a button that when tapped, takes you to the next set of demos. Now removing the handlers or having a new button on every screen is *bad* programming. So one might have a counter which is 0 and everytime the button is tapped, the counter is incremented and thereby calling a different function based on the counter value.

There is no one correct way to skin a cat, but then skinning the cat is not correct to start with in the first place. So what I mean by this is that there might have been rules for programming and those rules are flaunted, no harm done, but then the software that you are building will have that shaky base as it is not build on a solid foundation of programming know-how. It is rather build on shonky work arounds that *just* work.

You can be the judge and choose whichever method you like to use, after all it is your app and how you code also is a reflection of how you think.

The otherway, that is used just in case you might want to know is

 local function1 ( event )
    obj:removeEventListener ( "someEvent", function1 )
    timer.performWithDelay ( 1, function() obj:addEventListener ( "someEvent", function2 ) end )
 end

 local function2 ( event )
 end

and of course you need to have them forward declared, etc, etc

I think the approach I have outlined above is clean and works better and makes for some very good modular development.

Comments

Popular Posts