corona - how to define a global function that has an event parameter in lua -


assume have following function:

function ontilt( event )     physics.setgravity( (-9.8*event.ygravity), (-9.8*event.xgravity) ) --Το σωστό end 

which used in many different lua files. want define in external file , use require file not repeat in each lua file.

the problem function called when in same file following (without passing argument)

runtime:addeventlistener( "accelerometer", ontilt ) 

can explain me how define in external file , how call then?

you can try minimal external module layout:

-- external module - file named "mymodule.lua" local m = {}  function m.ontilt( event )     physics.setgravity( (-9.8*event.ygravity), (-9.8*event.xgravity) ) end  return m 

where need use function can write (assuming mymodule.lua put in directory on lua search path):

local mymodule = require 'mymodule'      -- ... other code ...  runtime:addeventlistener( "accelerometer", mymodule.ontilt ) 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -