Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4613982
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:37:54+00:00 2026-05-22T01:37:54+00:00

Right now we have all our code gathered in main.lua. We don’t want to

  • 0

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 ---------------------------------------------------------
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:37:55+00:00Added an answer on May 22, 2026 at 1:37 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.