How to handle event in sub-folder scripts

Hi,

I try to organize my project into serveral sub-folders, which is following the document from Corona as this link :

http://coronalabs.com/blog/2012/07/10/best-practices-for-organizing-projects/

I can put util.lua in to /scripts/util.lua, and I can call those functions by this code

local util = require( “scripts.util” )

or

local shadow = display.newImageRect( “images/shadow.png”,2,16 )

there is no problem for general purpose, such as load image, return a computed value from sub-folder script,

But if there are some display object in util.lua, how do I handle the event in main.lua but display objects and events from util.lua ?

Thanks

Not entirely sure if this is what you’re asking … but if functions in main.lua (even event handlers) need access to functions in util.lua, then you need to make sure that those functions are global functions, or that main.lua has an object that was defined in util.lua (so you can get to util functions through that object).

First approach:

-- util.lua function more\_event\_processing( e ) -- this code does the extra stuff within util.lua end -- main.lua require( "scripts.util" ) -- all global functions are now visible local function event\_handler( e ) -- do basic event processing, then -- do the stuff that's inside util.lua more\_event\_processing( e ) end Runtime:addEventListener( "eventname", event\_handler )

Second approach (assumes certain OOP-ish things inside util.lua):

-- main.lua local util\_class = require( "scripts.util" ) local util\_obj = util\_class.new() local function event\_handler( e ) -- access functions defined in util.lua util\_obj.myFunc( e ) end

This information is really helpful, I will try it as soon as possible.

Thanks

Not entirely sure if this is what you’re asking … but if functions in main.lua (even event handlers) need access to functions in util.lua, then you need to make sure that those functions are global functions, or that main.lua has an object that was defined in util.lua (so you can get to util functions through that object).

First approach:

-- util.lua function more\_event\_processing( e ) -- this code does the extra stuff within util.lua end -- main.lua require( "scripts.util" ) -- all global functions are now visible local function event\_handler( e ) -- do basic event processing, then -- do the stuff that's inside util.lua more\_event\_processing( e ) end Runtime:addEventListener( "eventname", event\_handler )

Second approach (assumes certain OOP-ish things inside util.lua):

-- main.lua local util\_class = require( "scripts.util" ) local util\_obj = util\_class.new() local function event\_handler( e ) -- access functions defined in util.lua util\_obj.myFunc( e ) end

This information is really helpful, I will try it as soon as possible.

Thanks