Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

calling functions from dynamically generated strings?
Started by andyhullinger Sep 05 2010 11:05 PM

- - - - -
1 reply to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

andyhullinger

[GLOBAL: userInfoPane.html]
andyhullinger
  • Enthusiast

  • 30 posts
  • Corona SDK

I'm working on an iPad "book" engine and also my horrible Lua skills!

Imagine a stack of pages to the right (unread) and left (read) of the ipad's screen. Each page turn in this book requires a custom animated transition and no two are alike. I've got a bunch of "transition" functions and have put them into a table for easy indexing with the page count.

Is it possible to dynamically create a function name on the fly and call it?
This won't work...
-- current page numberlocal pageCount = 3--lots of functions with custom page transitionsfunction drawpage3()	transition.to (page2, {time=1000, x=-1024})	transition.from (page3.art, {time=2000, x=1024})	transition.from (page3.text, {delay=1500, alpha=0, transition=easing.outQuad})end-- main function to flip pagesfunction turnpage()      drawpage..pageCount()end


However! I can get this code to work by "hacking" it to using the callback of transition.onComplete method (with a duration of "zero")

--a table of all the transition functions (1 per page)local animData = {drawpage1,drawpage2,drawpage3}--logic to transition the pages increment the pagecount --(e.g. page1 becomes oldPg and is positioned off screen to the left as--page2 is pladed on screen)function turnpage(oldPg, newPg)	transition.to (oldPg, {time=0, x=-1024})	transition.to (newPg, {time=0, x=0, onComplete=animData[pageCount]})end


Any tips or pointers on my flawed logic appreciated. I'm struggling with understanding functions as first class objects and how or even why to use them with/in tables?
uid: 5339 topic_id: 1918 reply_id: 301918


[TOPIC: post.html]
#2

tetu

[GLOBAL: userInfoPane.html]
tetu
  • Contributor

  • 166 posts
  • Corona SDK

hi andy,

the "hack" works because you cannot add parentheses to the onComplete function
animData[3] right below the variable declaration results in error

replace the turnpage function with this:

function turnpage()
      animData[3]()
end

you can also pass arguments to the function (if it expects some), by using

animData[3](param1,param2,...)

uid: 6459 topic_id: 1918 reply_id: 5684



[topic_controls]
[/topic_controls]