Error with removeEventListener() function in CoronaSDK 2.0

Hi,
Does anyone know if the implementation of the removeEventListener() has changed in SDK2.0 as code that ran fine in Beta 7 now errors as follows:-

Runtime error
ERROR: nil key supplied for property lookup.
stack traceback:
[C]: ?
[C]: ?
?: in function ‘removeEventListener’
Example Code:

btnLetterAudio:removeEventListener( “tap”, whichOption)
[import]uid: 5872 topic_id: 2300 reply_id: 302300[/import]

Just test this myself. You get this error when the handler function that you provided (whichOption) doesn’t exist in this scope. [import]uid: 5712 topic_id: 2300 reply_id: 6964[/import]

If “whichOption” is not within scope, it should fail. Was this working before? I’m not sure if anything has changed in this area but the developer could have found a bug when looking at something else and fixed it.

Is everything working as expected or is there a new bug here?

If there is still a problem can someone post some sample code showing the problem?

Thanks,
-Tom [import]uid: 7559 topic_id: 2300 reply_id: 7039[/import]

Tom, here you go:

[lua]function onTouch(event)
–xxxxxx
end

testObj = display.newCircle(160, 240, 50 )

testObj:addEventListener( “touch”, onTouch )
testObj:removeEventListener( “touch”, onTouchnotThere )[/lua] [import]uid: 5712 topic_id: 2300 reply_id: 7074[/import]

Mike, do you think this is a bug or are you saying that this didn’t generate an error before? I would expect it to generate an error on line 8.

Thanks,
-Tom [import]uid: 7559 topic_id: 2300 reply_id: 7086[/import]

I am unable to test this or my own code under Beta 7 as it has expired (which what I have been using while transitioning to 2.0) but my code used to run error free before

But under Beta 8 the code mike posted errors on line 8 as you say. This was not the case in the past as my code was fine.

Here is s slightly different block of code but I have used the same principal for each screen, call a disable screen function/method that takes down the screen and disposes of the assets before drawing the next one.

This now errors:-

function Menu(pageFadeOn)

local function disableMenuPageButtons(event)
print ("*** You have entered disableMenuPageButtons()")
bntStart:removeEventListener( “tap”, Start)

for i=grpBackground.numChildren,1,-1 do local child = grpBackground[i] child.parent:remove( child )end
print ("* grpBackground count = “…#grpBackground…” if 0 all objects removed from display group")

print ("*** nilScreenItems()")
bntStart = null
menuBackgroundImg = null
grpBackground = null
end

local function Start ( event )
print("*** You have selected Start() button")
media.playEventSound(buttonMenuExitSID)
timer.performWithDelay( globalDelay, disableMenuPageButtons)
clueSelected = 0
currentPageNo = 1
PageSetup(globalPageFade)
end

bntStart:addEventListener( “tap”, Start)

end

I have discovered that the reason is the location of the function Start if it is not local all works ok.

So that leaves we with two question
1-If a function is not prefixed local is it global (visible outside Menu() or global in as much as local to the code inside Menu()

2-When should a function be local as I though good practice was always local unless there was a need to be global as then when a block of code is not executed say a function it’s sub functions are removed from memory. Where as if they are functions within function but defined global they are all in memory for the duration of execution of main() so to speak.

Thanks for your help
[import]uid: 5872 topic_id: 2300 reply_id: 7121[/import]

@Tom:
No, I was posting the code so you can see what error he is getting. [import]uid: 5712 topic_id: 2300 reply_id: 7126[/import]

I re-posted your code using using the code and /code codes so it easier to read. I can’t verify that your code worked in Beta 7 but the current problem is you are trying to remove the “Start” listener but the Start function hasn’t been defined yet (it come before the Start function). Because of how Lua processes variables you sometimes need to add a “forward reference” so the variable is define before it’s used. I added a “local Start” line and now your code should work. (If you add a forward reference, be sure to remove the “local” before the function or it will generate an error.)

Defining functions and variables as “local” is good practice because it allows Lua to reclaim the memory used if the references to the functions or variables go away. If your functions are only used within another function, it makes sense to make the function local to that function (as you did). If you need variables and functions visible to the functions and code within the file, you can makes them local at the top of your file.

I hope that helps.

-Tom

function Menu(pageFadeOn)  
  
local Start -- \*\*\* Added forward reference \*\*\*  
  
local function disableMenuPageButtons(event)  
print ("\*\*\* You have entered disableMenuPageButtons()")  
bntStart:removeEventListener( "tap", Start)  
  
for i=grpBackground.numChildren,1,-1 do local child = grpBackground[i] child.parent:remove( child )end  
print ("\* grpBackground count = "..#grpBackground.." if 0 all objects removed from display group")  
  
print ("\*\*\* nilScreenItems()")  
bntStart = null  
menuBackgroundImg = null  
grpBackground = null  
end  
  
function Start ( event ) -- \*\*\* Removed "local" \*\*\*  
print("\*\*\* You have selected Start() button")  
media.playEventSound(buttonMenuExitSID)  
timer.performWithDelay( globalDelay, disableMenuPageButtons)  
clueSelected = 0  
currentPageNo = 1  
PageSetup(globalPageFade)  
end  
  
bntStart:addEventListener( "tap", Start)  
  
end  

[import]uid: 7559 topic_id: 2300 reply_id: 7132[/import]

@Mike, thanks. [import]uid: 7559 topic_id: 2300 reply_id: 7133[/import]

Thanks this post just helped me out when I was having the same issue. I just changed the attached function from local to global and all of a sudden it began to work. [import]uid: 49863 topic_id: 2300 reply_id: 121048[/import]

Thanks this post just helped me out when I was having the same issue. I just changed the attached function from local to global and all of a sudden it began to work. [import]uid: 49863 topic_id: 2300 reply_id: 121048[/import]