onKeyEvent Error

scene1 —> scene2 ---->scene3

Back key is used to get back to scene2 from scene3 and back key is used once again to go from scene2 to scene1. Sometimes, back key which is used at scene3 to go back to scene2  does not work properly and its skip scene2 and directly goes to scene1.

After this, when i go from scene1 to scene2 and then  it doesn’t go to scene3. and game just stop there.

How to solve the above problem?

Perhaps your device is generating two events?  Most likely though you may not be trappnig for the fact that each key press generates a “DOWN” and an “UP” event.

Can you post your key event code please?

Rob

local function onKeyEvent( event )

   local phase = event.phase

   local keyName = event.keyName

   

  – if ( “back” == keyName and phase == “up” and platformName==“Android” and backcounter==0) then 

   if (“back” == keyName and phase == “up” and platformName==“Android”)  then

     – if ( composer.getSceneName( “current” ) == “area1levels” ) then 

if(gameData[11]==1) then 

audio.resume(back)

end

         composer.gotoScene(“area1levels”)  

Runtime:removeEventListener(“key”,onKeyEvent)  

return true 

end  

return false

end 

See attached file also

Hello @puneetpositive,

Have you carefully checked the conditions that you’re using for your conditional checks using print() statements? There is likely one sub-case that is failing in your multiple-condition checks, and typically a little debugging will pinpoint what it is. For example, are you sure that “platformName” is always what you expect it should be, as in, does the Composer scene recognize that variable and is it in scope?

Brent

I suspect the problem is that you’re event handler’s are not being removed correctly and realistically there can only be one keyEvent handler at a time.  Sure you probably could make more work, but it takes some thinking about how things are handled.

The best practice is to only have one onKeyEvent handler in your main.lua, enable it and never disable it.  That function has to be smart and know what scene you’re in and where it needs to go to.

Rob

Perhaps your device is generating two events?  Most likely though you may not be trappnig for the fact that each key press generates a “DOWN” and an “UP” event.

Can you post your key event code please?

Rob

local function onKeyEvent( event )

   local phase = event.phase

   local keyName = event.keyName

   

  – if ( “back” == keyName and phase == “up” and platformName==“Android” and backcounter==0) then 

   if (“back” == keyName and phase == “up” and platformName==“Android”)  then

     – if ( composer.getSceneName( “current” ) == “area1levels” ) then 

if(gameData[11]==1) then 

audio.resume(back)

end

         composer.gotoScene(“area1levels”)  

Runtime:removeEventListener(“key”,onKeyEvent)  

return true 

end  

return false

end 

See attached file also

Hello @puneetpositive,

Have you carefully checked the conditions that you’re using for your conditional checks using print() statements? There is likely one sub-case that is failing in your multiple-condition checks, and typically a little debugging will pinpoint what it is. For example, are you sure that “platformName” is always what you expect it should be, as in, does the Composer scene recognize that variable and is it in scope?

Brent

I suspect the problem is that you’re event handler’s are not being removed correctly and realistically there can only be one keyEvent handler at a time.  Sure you probably could make more work, but it takes some thinking about how things are handled.

The best practice is to only have one onKeyEvent handler in your main.lua, enable it and never disable it.  That function has to be smart and know what scene you’re in and where it needs to go to.

Rob