So I have a simple toolbar module, as below. I use it to create a toolbar object in my menu.lua file. It has a function that takes an item to add to the toolbar, but when I call toolbar:takeItem(crate) as an example, inside the module where crate should be taken on by the “item” paramater, it instead it seems to become bound to self, which is supposed to be the module display group.
– menu.lua
local composer = require( "composer" ) local scene = composer.newScene() local toolbar = require "toolbar" function scene:create( event ) local toolbar = toolbar:new() local crate = display.newImage("crate.png") crate.x, crate.y = 100, 200 --toolbar.takeItem(5) toolbar.takeItem(crate) end function scene:show( event ) local sceneGroup = self.view end function scene:hide( event ) local sceneGroup = self.view end function scene:destroy( event ) local sceneGroup = self.view end
– toolbar.lua
local toolbar = {} function toolbar:new() local self = display.newGroup() self.img = display.newImage("crate.png") self:insert(self.img) self.img.width, self.img.height = 300, 50 self.img.x, self.img.y = display.contentCenterX, 0 self.items = {} self.start = 10 self.xIncrement = 20 self.itemSize = self.xIncrement \* 0.9 function self:takeItem(item) print(self.x) print(item) end return self end return toolbar