Listener in module

Hi again,

so, i’m having some trouble with modular coding approach for my game and thats why i have a little question:

how to properly add an eventListener to object in module? Where do i need to put it for object to listen for touches?
any help is great)

[lua]ocal dog = {}
local dog_mt = { __index = dog } – metatable


– PRIVATE FUNCTIONS

local function getDogYears( realYears ) – local; only visible in this module
return realYears * 7
end


– PUBLIC FUNCTIONS

function dog.new( name, ageInYears ) – constructor

local newDog = {
name = name or “Unnamed”,
age = ageInYears or 2
}

return setmetatable( newDog, dog_mt )
end


function dog:rollOver()
print( self.name … " rolled over." )
end


function dog:sit()
print( self.name … " sits down in place." )
end


function dog:bark()
print( self.name … " says “woof!”" )
end


function dog:printAge()
print( self.name … " is " … getDogYears( self.age ) … " in dog years." )
end


return dog[/lua]
and how can i declare coordinates for this object? [import]uid: 16142 topic_id: 18381 reply_id: 318381[/import]

Since this is an external module, you’ll likely be calling this code from outside of this module.

As you can see at the bottom, your module is returning the ‘dog’ table, so whatever object you assign the ‘new()’ function to will have all the methods of dog.

Example:

local dogClass = require "dog" -- assuming your code is in dog.lualocal myDog = dogClass.new( "Spot", 3 )-- all 'dog' methods are available from 'myDog' now:myDog:printAge() -- terminal output: Spot is 21 in dog years.[/code] [import]uid: 52430 topic_id: 18381 reply_id: 70500[/import]