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”:
--------------- 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 ---------------------------------------------------------
On this other answer I’ll try to implement what I think you want. Keep in mind that I have never used CoronaSDK, so it might need some debugging.
First, here’s a file called create.lua . You should place it on the same directory as main.lua.
The main objective of
create.luais building a table calledcreate. That table only has one function for creating water; you can add more functions later, for example for creating ground.Inside main.lua, in order to be able to use create.water first you have to get access to that
createtable; you userequirefor that. Like this:Notice that create.water receives parameters. You can change the parameters more easily now:
Regards!