sandra
sandra

Reputation: 33

Corona SDK - How do we connect external functions to main.lua?

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 ---------------------------------------------------------

Upvotes: 2

Views: 5462

Answers (2)

kikito
kikito

Reputation: 52698

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.lua is building a table called create. That table only has one function for creating water; you can add more functions later, for example for creating ground.

-- file 'create.lua'

local function water(game,x,y,xScale,imagePath,soundPath)

  local image = display.newImage( imagePath )
  game:insert( image )

  image.x, image.y, image.xScale = x, y, xScale

  local w,h = display.contentWidth, display.contentHeight

  -- create two empty local variables and assign functions to them
  local loopar, loopar2
  loopar = function() transition.to(image, {time=2300, x=(400), y=(h-140), transition=easing.inOutQuad, onComplete=loopar2}) end
  loopar2 = function() transition.to(image, {time=2200, x=(w-500), y=(h-120), transition=easing.inOutQuad, onComplete=loopar}) end

  -- start the movement
  transition.to(image, {time=2300, x=(w-400), y=(h-140), transition=easing.inOutQuad, onComplete=loopar})

  local sound = media.newEventSound(soundPath)
  image:addEventListener("touch", function() media.playEventSound(sound) end )
end

local create = { water = water }

return create

Inside main.lua, in order to be able to use create.water first you have to get access to that create table; you use require for that. Like this:

-- file 'main.lua'

local create = require 'create'

<put the code for creating the 'game' group here>

create.water(game, 619, 500, 2, "water3.png", "waves.wav")

Notice that create.water receives parameters. You can change the parameters more easily now:

create.water(game, 200, 100, 2, "water3.png", "waves.wav") -- other initial coordinates
create.water(game, 619, 500, 1, "water3.png", "waves.wav") -- scaleX = 1
create.water(game, 619, 500, 1, "water.png", "waves.wav") -- other water img

Regards!

Upvotes: 3

kikito
kikito

Reputation: 52698

I'm pretty sure you can use Lua's require function in Corona.

The simplest way to use it is the following:

1) You create as many local functions as you want inside the file (let's call it extra.lua)

local function f1(...)
  ...
end

local function f2(...)
  ...
end

2) After the functions, you build a table that has all the functions you want to "share". It's common that that table is called like the file, so I'll call mine extras:

local extras = {
  f1 = f1,
  f2 = f2
}

This notation might seem weird. What it's doing is creating a table called extras with a field called f1 inside it, pointing to the local function f1. In other words, calling f1(1,2,3) does the same as calling extras.f1(1,2,3)

3) Return the table at the end of the file:

return extras

4) Now on main.lua you can use the functions defined on extras like this:

local extras = require 'extras'

extras.f1(1,2,3)
extras.f2(4,5,6)

I hope this helps! Good luck!

Upvotes: 5

Related Questions