Composer and external modules

I want to learn composer.

Does Composer support these functions?

package.seeall (deprecated)module() (deprecated)

If you are making your own external modules, you can still use the package.seeall and module() setup but it’s not recommended.  When you require a file (even with Composer) it won’t care which way the module is built.  Deprecated means it may be removed in future releases so unless you have a strong reason to use the older module style, I would recommend using the newer method.

local MODULE = {} – Modules are just tables that get required.

MODULE.someNumber = 10

MODULE.someString = “Hello”

function MODULE.someFunction( param, list )  – a function that doesn’t care about it’s object

   …

end

function MODULE:someObjectFunction( param, list)  – a function with the reference to the object (self) being passed in)

    – self is the instance of this object

end

return MODULE

Most people use a simple “M” to represent MODULE since thats too much typing, i.e.:

local M = {}

function M.someFunction() … end

return M

That’s all you need to define a module.

[lua]I am using storyboard at the moment.
I try to upgrade some deprecated modules

I create a module
–islaMo.lua

local islaTa = {}

function islaTa.newIsland(params)

local isla = display.newImageRect( “_Assets/Images/wisla.png”, 98,68 )
isla.x=params.x
isla.y=params.y

end
return islaTa

—**************************************************–
this is my options.lua scene
local island =require(“islaMo”)

function scene:createScene( event )
local group = self.view

local islandOne =island.newIsland{
x = 250,
y = 25
}

group:insert(islandOne)

end

When I put “group:insert(islandOne)”, it doesnt work.
If I dont put “group:insert(islandOne)” it works, but the image is in any scene.
[/lua]

Perhaps something like this:

--islaMo.lua local islaTa = {} function islaTa.newIsland(params)     local isla = display.newImageRect( "\_Assets/Images/wisla.png", 98,68 )     isla.x=params.x     isla.y=params.y     return isla end return islaTa

 

The last return returns the islaTa table for the require to work.  However when you want to run newIsland() you need it to return the display object you’re creating.

 

Rob

thanks,

It works now.

another question

if I want to remove from memory the module , I have to use

package.loaded[“islaTa”] = nil   ?

I believe that is correct.

I added physics to the module, but I have a problem When I create an island in the scene levelOne.

When I Leave the scene, the physics of  "islandOne " is still there .

the physics is not removed


module islaMo

[lua]

local islaTa = {}

function islaTa.newIsland(params)

  

    local isla = display.newImageRect( “_Assets/Images/wisla.png”, 98,68 )

isla.x=params.x

        isla.y=params.y

        -

        

        physics.addBody(isla,“static”,{ density=3.0, friction=0.5, bounce=0.3,radius=20 } )

        return isla

end

return islaT

–**scene – levelOne

local island =require(“islaMo”)

function scene:createScene( event )

        local group = self.view

 local islandOne =island.newIsland{

            x = 250, 

            y = 225

        }

      

 group:insert(islandOne)

end

[/lua]

If you are making your own external modules, you can still use the package.seeall and module() setup but it’s not recommended.  When you require a file (even with Composer) it won’t care which way the module is built.  Deprecated means it may be removed in future releases so unless you have a strong reason to use the older module style, I would recommend using the newer method.

local MODULE = {} – Modules are just tables that get required.

MODULE.someNumber = 10

MODULE.someString = “Hello”

function MODULE.someFunction( param, list )  – a function that doesn’t care about it’s object

   …

end

function MODULE:someObjectFunction( param, list)  – a function with the reference to the object (self) being passed in)

    – self is the instance of this object

end

return MODULE

Most people use a simple “M” to represent MODULE since thats too much typing, i.e.:

local M = {}

function M.someFunction() … end

return M

That’s all you need to define a module.

[lua]I am using storyboard at the moment.
I try to upgrade some deprecated modules

I create a module
–islaMo.lua

local islaTa = {}

function islaTa.newIsland(params)

local isla = display.newImageRect( “_Assets/Images/wisla.png”, 98,68 )
isla.x=params.x
isla.y=params.y

end
return islaTa

—**************************************************–
this is my options.lua scene
local island =require(“islaMo”)

function scene:createScene( event )
local group = self.view

local islandOne =island.newIsland{
x = 250,
y = 25
}

group:insert(islandOne)

end

When I put “group:insert(islandOne)”, it doesnt work.
If I dont put “group:insert(islandOne)” it works, but the image is in any scene.
[/lua]

Perhaps something like this:

--islaMo.lua local islaTa = {} function islaTa.newIsland(params)     local isla = display.newImageRect( "\_Assets/Images/wisla.png", 98,68 )     isla.x=params.x     isla.y=params.y     return isla end return islaTa

 

The last return returns the islaTa table for the require to work.  However when you want to run newIsland() you need it to return the display object you’re creating.

 

Rob

thanks,

It works now.

another question

if I want to remove from memory the module , I have to use

package.loaded[“islaTa”] = nil   ?

I believe that is correct.

I added physics to the module, but I have a problem When I create an island in the scene levelOne.

When I Leave the scene, the physics of  "islandOne " is still there .

the physics is not removed


module islaMo

[lua]

local islaTa = {}

function islaTa.newIsland(params)

  

    local isla = display.newImageRect( “_Assets/Images/wisla.png”, 98,68 )

isla.x=params.x

        isla.y=params.y

        -

        

        physics.addBody(isla,“static”,{ density=3.0, friction=0.5, bounce=0.3,radius=20 } )

        return isla

end

return islaT

–**scene – levelOne

local island =require(“islaMo”)

function scene:createScene( event )

        local group = self.view

 local islandOne =island.newIsland{

            x = 250, 

            y = 225

        }

      

 group:insert(islandOne)

end

[/lua]