Reputation: 8920
I have the following function
DOESN'T WORK:
local function onPlayBtnRelease(level)
storyboard.gotoScene( level, "fade", 500 )
return true
end
I want to pass a parameter so the appropriate level to be loaded, so I have the following button:
planet1 = widget.newButton{
label="1",
labelColor = { default={255}, over={128} },
fontSize=25,
defaultFile="levelplanet.png",
overFile="levelplanet-over.png",
width=62, height=62,
onRelease = onPlayBtnRelease("loadscene1") -- event listener function
}
but the above doesn't work.
THAT IS OK:
If I do
local function onPlayBtnRelease
storyboard.gotoScene( "loadscene1", "fade", 500 )
return true
end
planet1 = widget.newButton{
label="1",
labelColor = { default={255}, over={128} },
fontSize=25,
defaultFile="levelplanet.png",
overFile="levelplanet-over.png",
width=62, height=62,
onRelease = onPlayBtnRelease
}
It compiles. How can I pass a parameter to onPlayBtnRelease
? Any idea why this causes a compilation error?
The error:
Runtime error
bad argument #1 to 'find' (string expected, got table)
stack traceback:
[C]: ?
[C]: in function 'error'
?: in function 'gotoScene'
Upvotes: 1
Views: 3479
Reputation: 833
Try this it will work for certain.
local onPlayBtnRelease = function(event)
storyboard.gotoScene(event.target.myScene, "fade", 500 )
return true
end
planet1 = widget.newButton{
label="1",
labelColor = { default={255}, over={128} },
fontSize=25,
defaultFile="levelplanet.png",
overFile="levelplanet-over.png",
width=62, height=62,
onRelease = onPlayBtnRelease
}
planet1.myScene = "yourSceneName"
Upvotes: 0
Reputation: 10502
Explanation of this line:
onRelease = onPlayBtnRelease("loadscene1") -- event listener function
This line calls onPlayBtnRelease("loadscene1")
and assigns the return value to the field onRelease
. So, what you probably need is to do it like this:
onRelease = function() return onPlayBtnRelease("loadscene1") end
If you need to do this a lot, you can make a closure generator like this
local function newclosure(func, ...)
local args = {...}
return function()
return func(unpack(args));
end
end
This way you only have to write the line like this:
onRelease = newclosure(onPlayBtnRelease, "loadscene1")
Upvotes: 4
Reputation: 373
Maybe it is possible to associate needable data outside of the constructor? For example:
planet1 = widget.newButton { -- then initialize as usuall
}
planet1.next_scene = "loadscene1"
Upvotes: 1
Reputation: 8497
Wondering, if You can build Widget object with some non-standart options without wrapping it into another table like this:
local function onPlayBtnRelease(event)
storyboard.gotoScene(event.target.next_scene, "fade", 500 )
return true
end
planet1 = widget.newButton{
label="1",
labelColor = { default={255}, over={128} },
fontSize=25,
defaultFile="levelplanet.png",
overFile="levelplanet-over.png",
width=62, height=62,
next_scene = "loadscene1"
}
If it won't work, table listeners are the ones You're looking for.
Upvotes: 0
Reputation: 6985
The error is most probably caused by:
local function onPlayBtnRelease
storyboard.gotoScene( "loadscene1", "fade", 500 )
return true
end
because of the missing parameter list in the function definition.
The error could be similar to this, I guess:
G:\root\main\core\Lua/app/lua51/bin/lua.exe: lua_08.lua:5:
'(' expected near 'storyboard'
From what you posted it seems that onPlayBtnRelease
is an event listener function, thus it should be declared as a function taking a single parameter which is the triggered event:
local function onPlayBtnRelease(event)
// ... code ...
return true
end
EDIT
The onRelease
field must be the listener, not the result it returns when it is called. Try simply this:
planet1 = widget.newButton{
label="1",
labelColor = { default={255}, over={128} },
fontSize=25,
defaultFile="levelplanet.png",
overFile="levelplanet-over.png",
width=62, height=62,
onRelease = onPlayBtnRelease
}
Upvotes: 1