Hi guys,
I am building an application, and I am modularising it, to make it easy to maintain. Everything works fine, but I need some help or pointers in terms of accessing variables in different modules..
For example, I have an image on the screen.. there is a button on the screen.. when the button is pressed the image should dissapear.. pretty easy?
The image is created in the main.lua file, the button is created in a gamefunctions.lua, which has the ontouch event... which is where the issue comes in.. how do I access the image from main in gamefunctions.lua
Thanks in advance
Code posted
main.lua
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | local gameFunctions = require "gameFunctions" local btnPressOK = true local button -- Create New image image1 = display.newImage("reload.png") -- Create button which hides and shows image button = gameFunctions.createButtons() <code> gameFunctions.lua <code> module(..., package.seeall) local ui = require("ui") function createButton(originX, originY, sizeX, sizeY, imageBtnLocation,imageBtnPressedLocation) local MenuBtn = ui.newButton{ defaultSrc = imageBtnLocation, defaultX = sizeX, defaultY = sizeX, overSrc = imageBtnPressedLocation, overX = sizeX, overY = sizeX, text = "", font = "Helvetica", textColor = { 255, 255, 255, 255 }, size = 16, emboss = false } MenuBtn.x = originX; MenuBtn.y = originY return MenuBtn end function onbtnTouch() print("button Pressed") -- when the button is clicked I want image1 to be hidden -- how do I access that variable from main --main.image1.isVisible = false end function createButtons() local btn = createButton(450, 300,48, 48, "next.png", "next_p.png") btn:addEventListener("tap", onbtnTouch ) return btn end <code> |
@vik,
you might want to have a read of the following http://howto.oz-apps.com/2011/09/referencing-multiple-objects-of-same.html
and
http://howto.oz-apps.com/2011/09/scopes-for-functions.html
and
http://howto.oz-apps.com/2011/09/function-scope-answers.html
these should give you a better way to structure your code and make it modular.
cheers,
?:)
hey mate,
thanks for that.. Learnt some new things but it didn't really explain how to access a variable in a different module...
in my original post the code looks like it is in the same file... I just reposted
its in the 'onbtnTouch' function....
main.lua
1 2 3 4 5 6 7 8 9 | local gameFunctions = require "gameFunctions" local btnPressOK = true local button -- Create New image image1 = display.newImage("reload.png") -- Create button which hides and shows image button = gameFunctions.createButtons() |
vik,
That's because you are looking for something specific, not getting the broader/bigger picture.
1. change the gamefunction.lua as following
1 2 3 4 5 6 7 8 9 10 11 | function createButtons() local function createButton(......) end local function onBtnTouch(event) end local btn = createButton(....) btn:addEventListener("tap",onBtnTouch) return btn end |
Thanks mate... I do appreciate the assistance
viks,
did it work for you?
where are you from? you seem to be a new name on the forums.
cheers
?:)
Hi mate,
Yes it did... but I managed to get a work around using closures had to get something specific...
from melbourne in Australia... and yourself? I am guessing from the company name .. from australia
Vik
I'm from North QLD.
cheers,
?:)