How to add a delay to Runtime:addEventListener

So here is my code, plain and simple:

  
local playerAccelerate = function( event )  
 if gameIsActive then  
 player.x = player.x + event.xGravity \* 20  
 end   
end  
  

In my main game loop, I call this:

Runtime:addEventListener("accelerometer", playerAccelerate)  

Everything works perfect, exactly how I want; however, I want the Runtime:addEventListener to start after a 3 - 4 second delay. So basically detect the accelerometer after 3 or 4 seconds have passed.

I have tried for about 2 hours to do this may different ways, but I just cant get it to work. I tried timer.performWithDelay, if statements, many different things, and it just wont work!

To give you an idea of exactly what im trying to do, my player moves onto the screen from off the screen using transition.to, and I dont want the accelerometer to start working until the player moves to the center of the screen (the transition takes 3 seconds).

I am still in the process of learning the programming fundamentals of Lua so sorry for the noob questions. Thanks a lot to everyone who has helped me so far, and anyone who helps me here!

-Kyle

[import]uid: 9968 topic_id: 4605 reply_id: 304605[/import]

did you try it like this

[blockcode]
local playerAccelerate = function( event )
if gameIsActive then
player.x = player.x + event.xGravity * 20
end
end

function startAcc()
Runtime:addEventListener(“accelerometer”, playerAccelerate)
end

timer.performWithDelay( 3000, startAcc )
[/blockcode] [import]uid: 7911 topic_id: 4605 reply_id: 14525[/import]

That is one of the options I thought I tried but I guess I did it wrong because this works perfect! Thanks a lot for your help! [import]uid: 9968 topic_id: 4605 reply_id: 14529[/import]

your welcome [import]uid: 7911 topic_id: 4605 reply_id: 14532[/import]