Hi,
Right now we have all our code gathered in main.lua. We don't want to work with object oriented code, but still find an easy way of splitting the different objects up into separated files.
In our main.lua file we have objects like water, boat, boy, island and cloud - all together creating one massive bit of code. We want to have "BEGIN WATER 3" in it's own lua file and be able to execute that code in main.lua with a simple function instead. How do we do that?
Here an example from our main.lua file, displaying "water3":
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | --------------- BEGIN WATER 3 --------------------------------------------------------- local watere = display.newImage( "water3.png", true ) game:insert( watere ) watere.y = 619 watere.x = 500 watere.xScale = 2 --water sound local wavesound5 = media.newEventSound("waves.wav") local function playWave5 (event) media.playEventSound(wavesound5) end local w,h = display.contentWidth, display.contentHeight local function callbackFunc() print( "Transition 1 completed" ) end local function mainwater(watere) end function loopar() local myTween = transition.to(watere, {time=2300, x=(400), y=(h-140), transition=easing.inOutQuad, onComplete=loopar2}) end function loopar2() local myTween = transition.to(watere, {time=2200, x=(w-500), y=(h-120), transition=easing.inOutQuad, onComplete=loopar}) end local listener2 = function() print( "Transition 2 completed" ) end local myTween = transition.to(watere, {time=2300, x=(w-400), y=(h-140), transition=easing.inOutQuad, onComplete=loopar}) watere:addEventListener("touch", playWave5) ---------------- END WATER 3 --------------------------------------------------------- |
One piece of advice first: read the forum rules. You should use the "lua" tag whenever possible. Anyway:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | In file "water3.lua": local watere = display.newImage( "water3.png", true ) game:insert( watere ) watere.y = 619 watere.x = 500 watere.xScale = 2 --water sound local wavesound5 = media.newEventSound("waves.wav") local function playWave5 (event) media.playEventSound(wavesound5) end local w,h = display.contentWidth, display.contentHeight local function callbackFunc() print( "Transition 1 completed" ) end local function mainwater(watere) end function loopar() local myTween = transition.to(watere, {time=2300, x=(400), y=(h-140), transition=easing.inOutQuad, onComplete=loopar2}) end function loopar2() local myTween = transition.to(watere, {time=2200, x=(w-500), y=(h-120), transition=easing.inOutQuad, onComplete=loopar}) end local listener2 = function() print( "Transition 2 completed" ) end local myTween = transition.to(watere, {time=2300, x=(w-400), y=(h-140), transition=easing.inOutQuad, onComplete=loopar}) watere:addEventListener("touch", playWave5) |
Thank you for answering!
Our main purpose is to organize the main.lua file. Right now we have objects like water, boat, boy, island and cloud gathered in main.lua. We want to cut each object out and organize them into separate lua files. Because of the structure in lua, some objects appear infront or behind of other objects (made by purpose). So, when cutting objects out into separate files, they need to keep their same position in the "layer hierarchy".
Then we need to access each object in main.lua, with (if possible) some simple function.
And if I wanted to access the functions and local variables?
Check out the following resources, they will help you;
http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/
http://blog.anscamobile.com/2011/09/a-better-approach-to-external-modules/
http://blog.anscamobile.com/2011/07/using-external-modules-in-corona/
Peach :)