I’m trying to create a level selection screen. That includes a button for each level. I made a loop to dump out the buttons. Each button calls the same event handler which “will” in turn use a string representation of the function name to call to set up the level.
Example it would call:
Level1()
Level2()
.
.
.
Level40()
How do I get lua to take that string “Level1()” and call said function? I tried lua.eval() but Corona doesn’t seem to support it. [import]uid: 8434 topic_id: 4906 reply_id: 304906[/import]
You can try this:
[lua]function tapBtn( event )
print( "Executing Function: " … event.target.func )
_Gevent.target.func
end
function Level1()
print( “Level 1: Executed” )
end
btn = display.newRect( 100,100,50,50 )
btn.func = “Level1”
btn:addEventListener( “tap”, tapBtn )[/lua]
If Level1 … Leveln are separate module files and you want to initiate each module directly:
[lua]function tapBtn( event )
level = require(event.target.level)
_G[level].start()
end
btn = display.newRect( 100,100,50,50 )
btn.level = “Level1”
btn:addEventListener( “tap”, tapBtn )[/lua]
That will allow you to keep each level in a separate file, Level1.lua, Level2.lua … But each level module file must have a function in there called “start()”
[import]uid: 11393 topic_id: 4906 reply_id: 15815[/import]
Sorry I didn’t reply sooner. This is exactly what I was looking for! Thank you! I was starting to think I’d have to do it the long way. [import]uid: 8434 topic_id: 4906 reply_id: 16042[/import]
No worries. Glad it helped. Not sure if that method of calling functions globally is “best practice” but it seems to have worked without any dramas so far. [import]uid: 11393 topic_id: 4906 reply_id: 16121[/import]