isShake only on one screen?

Hello, for the beginning I wanna say hello to everyone :slight_smile:

I have problem with the event listener.

local function listener( event )     if event.isShake then         if try ~= nil then             native.showAlert( "Ass", "Shake this ass!", { "OK" } )         end     end end

Runtime:addEventListener( “accelerometer”, listener )

scene:addEventListener( “createScene”, scene )

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

scene:addEventListener( “destroyScene”, scene )

 I have 4 scenes, 3 out of 4 has the try set to nil, only one out of 1 have a try = 1 so the shake event should work only with the 1/4 scenes example.

1,2,3 - empty

4 - listener

When I go to 1,2,3 and shake, it is not working which is good :slight_smile:

When I go for 4 and shake, it shows the showAlert box which is good :slight_smile:

When I go for 4 and shake, press ok and go back to different scene and shake the alert box is shoiwng which is incorrect…

I set up the try to nil on 1,2,3 scenes but it did not help.

Any ideas of this?

Regards,

P.

You should only set up a single listener, and I would recommend it in main.lua.  Instead of a variable “try”, you can use the technique in:

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

to have a non-global global be your try.  That way you’re only creating the listener once.

Rob

Hey!

I have done it by different way to put it only in one scene, so it is still in the different file than main but its called while enter scene then I have add remove event in the exit scene, so this is working fine now!

Example:

exitScene( event ) Runtime:removeEventListener( "accelerometer", listener ) enterScene( event) Runtime:addEventListener( "accelerometer", listener )

You should only set up a single listener, and I would recommend it in main.lua.  Instead of a variable “try”, you can use the technique in:

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

to have a non-global global be your try.  That way you’re only creating the listener once.

Rob

Hey!

I have done it by different way to put it only in one scene, so it is still in the different file than main but its called while enter scene then I have add remove event in the exit scene, so this is working fine now!

Example:

exitScene( event ) Runtime:removeEventListener( "accelerometer", listener ) enterScene( event) Runtime:addEventListener( "accelerometer", listener )