Having trouble setting up an external module

Hi all.

So right now I’m trying to create an external module .lua file, only I’ve hit a wall with some of the options on the new object.

local soundBtn = {} local soundBtn\_mt = {\_\_index = soundBtn} function soundBtn.new ( text, ID, soundCue, yPos ) local newsoundBtn = { buttonImg = display.newImage ("button.png", display.contentWidth/2, (display.contentHeight\*0.15)\*yPos), buttonTxt = display.newText (text, display.contentWidth/2, (display.contentHeight\*0.15)\*yPos, "SHOWG.TTF", 90), } return setmetatable (newsoundBtn, soundBtn\_mt) end function soundBtn:tap( event ) audio.loadSound(soundCue) audio.play( soundCue) end return soundBtn

Here’s what I have so far. 

My problem is that I can’t find a way to set things such as the text’s colour, or attach this ‘soundBtn:tap’ function as a listener to the ‘buttonImg’. How do I go about adjusting these kind of things within this module?

Another smaller problem I’m having is about using this entire object as one entity. For example, in my main.lua, I have this:

local soundBtn1 = soundBtn.new("test", 1, "audio.mp3", 4)

but then I also want to add this object to a scroll widget, but since it isn’t a display object it can’t be added, so I have to do something like this:

menuScroll:insert(soundBtn1.buttonImg) menuScroll:insert(soundBtn1.buttonTxt)

Is there some way I can add this soundBtn object and have it’s children (buttonImg buttonTxt) follow it?

Hope I make sense.

Thanks for reading.

I’d forget all about using metatables.  There is a much simpler way to solve this.  
 
Also, you’re not doing the sound right as far as I can tell
 
Note: I’m not saying metatables are bad, but I feel they add nothing to the solution in this case.  Also,  I’d love to hear from anyone why one should use metatables instead of this solution:

local soundBtn = {} local sounds = {} local function sharedListener( self, event ) print( "My ID: ", self.ID ) audio.play( self.soundHandle ) end function soundBtn.new ( x, y, width, height, text, ID, soundCue) -- Use a group as the button 'container' local button = display.newGroup() -- Do not use newImage except in special cases. Use newImageRect -- Place the parts of the button in the group. button.img = display.newImageRect( button, "button.png", width, height ) button.label = display.newText( button, text, 0, 0, "SHOWG.TTF", 90) -- Question: Are you sure "SHOWG.TTF" is upper case? -- Move the group to move the parts button.x = x button.y = y -- Use the image to catch touches -- Also use a single function over and over to do the work. Saves time and memory. button.img.tap = sharedListner button.img:addEventListener("tap") -- Asign the ID to the image button.img.ID = ID -- Only load the sound once. sounds[soundCue] = sounds[soundCue] or audio.loadSound( soundCue ) button.img.soundHandle = sounds[soundCue] return button end return soundBtn

now later:

local soundButton = require("soundButton") local button = soundButton.new( 100, 100, 300, 60, "Bob's You're Uncle", 1, "bob.wav")

I’d forget all about using metatables.  There is a much simpler way to solve this.  
 
Also, you’re not doing the sound right as far as I can tell
 
Note: I’m not saying metatables are bad, but I feel they add nothing to the solution in this case.  Also,  I’d love to hear from anyone why one should use metatables instead of this solution:

local soundBtn = {} local sounds = {} local function sharedListener( self, event ) print( "My ID: ", self.ID ) audio.play( self.soundHandle ) end function soundBtn.new ( x, y, width, height, text, ID, soundCue) -- Use a group as the button 'container' local button = display.newGroup() -- Do not use newImage except in special cases. Use newImageRect -- Place the parts of the button in the group. button.img = display.newImageRect( button, "button.png", width, height ) button.label = display.newText( button, text, 0, 0, "SHOWG.TTF", 90) -- Question: Are you sure "SHOWG.TTF" is upper case? -- Move the group to move the parts button.x = x button.y = y -- Use the image to catch touches -- Also use a single function over and over to do the work. Saves time and memory. button.img.tap = sharedListner button.img:addEventListener("tap") -- Asign the ID to the image button.img.ID = ID -- Only load the sound once. sounds[soundCue] = sounds[soundCue] or audio.loadSound( soundCue ) button.img.soundHandle = sounds[soundCue] return button end return soundBtn

now later:

local soundButton = require("soundButton") local button = soundButton.new( 100, 100, 300, 60, "Bob's You're Uncle", 1, "bob.wav")