Problem referencing a function

Hi guys,

I made a quite simple code to explain a problem that I have referencing a function.
As you see, at the two.lua file, I want to call a function of the main.lua file.

Could anyone tell me the right way to do this?

Thanks in advance and sorry for my english!


–main.lua

local ui = require(“ui”)
local two = require(“two”)
local backgroundWidth = 320
local backgroundHeight = 480
local localGroup = display.newGroup()
local background = display.newRect(0,0,backgroundWidth, backgroundHeight)
background:setFillColor(255,255,255,50)

local function firstFunct()
print(“First function”)
end

localGroup:insert(background)
local one = two.new()

–two.lua

module(…, package.seeall)

local buttonPressed = function (event)
if event.phase == “release” then
– Here I want to call the function firstFunction()
end
end

local buttonPress = ui.newButton{
default = “buttonBlue.png”,
over = “buttonBlueOver.png”,
onEvent = buttonPressed,
text = “Press me”,
font = native.systemFont,
size = 14,
emboss = true
}

function new()

buttonPress.x=165
buttonPress.y=200

end [import]uid: 44013 topic_id: 7876 reply_id: 307876[/import]

because main doesnt have a reference to itself you can pass to “two” (as far as i know), the simples solution seems to pass a reference to the function into “two”

main.lua
[lua]two.firstFunct = firstFunct[/lua]

two.lua
[lua]local buttonPressed = function (event)
if event.phase == “release” then
firstFunct()
end

end[/lua]

it works, however it doesn’t seem like the neatest solution. i’ll see what else i can come with [import]uid: 6645 topic_id: 7876 reply_id: 28014[/import]

ok coming from a Flash background myself, one solution would be to dispatch a custom event

main.lua
[lua]local ui = require(“ui”)
local two = require(“two”)

local backgroundWidth = 320
local backgroundHeight = 480
local localGroup = display.newGroup()
local background = display.newRect(0,0,backgroundWidth, backgroundHeight)
background:setFillColor(255,255,255,50)

local function firstFunct()
print(“First function”)
end

localGroup:insert(background)
local one = two.new()

function buttonPressedInTwo(event)
print(“button pressed in two”)
local theButton = event.target – we sent this in our event
event.target.alpha=0.2

– call the function as desired
firstFunct()
end

Runtime:addEventListener(“two.blueButtonPressed”, buttonPressedInTwo)[/lua]

two.lua
[lua]module(…, package.seeall)

local buttonPress – forward reference

local buttonPressed = function (event)
if event.phase == “release” then

local event = { name=“two.blueButtonPressed”, target=buttonPress }
– note: the event name can be whatever you want
– i have assumed you may want to reference the button too
– so we send this as .target
Runtime:dispatchEvent(event)

end
end

buttonPress = ui.newButton{
default = “buttonBlue.png”,
over = “buttonBlueOver.png”,
onEvent = buttonPressed,
text = “Press me”,
font = native.systemFont,
size = 14,
emboss = true
}

function new()

buttonPress.x=165
buttonPress.y=200

end[/lua] [import]uid: 6645 topic_id: 7876 reply_id: 28015[/import]

Hello!

Thanks a lot, jmp909!! I appreciate your help!
The first code works but the second one crash when you write: event.target.alpha=0.2 (line 20)
I don’t know what this line is for.

Regards. [import]uid: 44013 topic_id: 7876 reply_id: 28117[/import]

i was just showing an example of passing the button itself in the event back up to the listener. i’ll have a look into why it didnt work

[import]uid: 6645 topic_id: 7876 reply_id: 28119[/import]

Ok. Thank you! [import]uid: 44013 topic_id: 7876 reply_id: 28151[/import]