It looks like you're new here. If you want to get involved, click one of these buttons!
local function printHello() print("Hello") end printHello() |
printHello() local function printHello() print("Hello") end |
local printHello printHello() function printHello() print("Hello") end |
Comments
Is there any specific reason for you to define it in scene init?
Likes: Ninjadoodle
You always need to declare a function before calling it.
To make a function available at any time (but you need to have declared it already) you can make it global; or define it as a property/method of an object, and use it "inside" it, or send it as a constructor/method parameter.
Likes: Ninjadoodle
Likes: Ninjadoodle
Thanks for the feedback
@talis - the main reason for defining it inside init(), is that I have 16 levels (all different - basically mini games) - so it mainly organisational purposes. Everything for a level sits inside it's own lua file
Thanks again for the help!
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Anyway hope you will solve it, by the way @kesgezh 's way seems to be the solution.I just tried now and it is working.
Thank you very much for your help!
@talis - haha c64 was first computer (and still my favourite). I'm trying to get into good habits, as I know the pain it causes me to come back to a game and have to dig through code I rushed through
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Awesome, thanks again Just one more thing if you have time ...
How would you set up an onTouch function in this format?
Total code
Likes: Ninjadoodle
Fragmenter - animated loop machine and IKONOMIKON - the memory game
inside init ...
You can use button's own event listener already. Is there any more spesific reaspon to this way?
Of course i am supposing you are using Button.lua
Inside init() this works no problem, but I can't seem to get it working this "new" way.
Also, I'm not using button.lua - in the particular example I posted, the sprite is called "buttonEye", but that's just a coincidence. In my level, I have touchable objects that do stuff on interaction. They might animate, tween - increment a variable etc.
this is explained in several forum topics and most probably in the oop example video.
see e.g.
http://giderosmobile.com/forum/discussion/5924/problem-in-inheritence-and-using-self-keyword/p1
you should rather use a button class for buttons, button class:
https://github.com/gideros/Button , and then
self.myButton:addEventListener("click",self.processButton,self)
will work as expected.
Likes: Ninjadoodle
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: Ninjadoodle, talis
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Likes: Ninjadoodle
Fragmenter - animated loop machine and IKONOMIKON - the memory game
I do really need to use a custom function and not the "button class", so thank you very much for your last example.
Do you think that calling the function this way ...
I'm just worried about problems popping up when I get deeper.
PS. You guys are awesome, I really appreciate you help!!
http://giderosmobile.com/forum/discussion/926/how-to-pass-variables-through-event-listeners-attempt-to-index-a-nil-value/p1
Likes: Ninjadoodle
see e.g. http://giderosmobile.com/forum/discussion/926/how-to-pass-variables-through-event-listeners-attempt-to-index-a-nil-value/p1
on the other hand a separate class/object for all your touchable items will make your code better and it will be easier to add new functions to it. also you can use it in any of your levels etc. just change the buttons class for your needs.
Likes: Ninjadoodle
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Fragmenter - animated loop machine and IKONOMIKON - the memory game