It looks like you're new here. If you want to get involved, click one of these buttons!
-- [ scenecontroller.lua ] function SceneController:init() self.testvar = "Test" end -- [ interface.lua ] function Interface:init(sceneController) print (sceneController.testvar) -- Test ok self:createInterface(sceneController) end function Interface:createInterface(sceneController) print (sceneController.testvar) -- Test ok *******|| self.nextPage:addEventListener("click", self.nextPageClick, sceneController) ||******* end function Interface:nextPageClick(sceneController) print (sceneController.testvar) -- Error attempt to index a nil value end -- [ main.lua ] sc = SceneController.new() interface = Interface.new(sc) |
Comments
Thank you very much (and also for all your contributions to this community).
I have 2 other questions :
1. I was wondering how the third argument of the addEventListener function (the data parameter) could be used?
I thought it was straightforward to use sceneController as an argument (like "self" is often used in that case) and I don't really get why it didn't work.
(Also See the way it's used in the Custom Events section in the Ultimate Guide)
How can one take advantage of the third argument (data) that is passed in the addEventListener function?
2. How do you remove the EventListener that is created the way suggested :
However
Maybe I am missing something?
....
Here's a quick example:
main.lua
I don't know whether this is good practice, to have a global event dispatcher, but it worked for me .
Likes: newbie2018
Those answers go a little bit beyond my current level but I will study it a few hours and with no doubt learn a lot.
Thank you for taking the time to answer
I was still curious about the use of "data" in :
EventDispatcher:addEventListener(type, listener, data)
I will try to find more examples on top of the ebook/game templates to get an understanding of this parameter.
For example
But all in all, it's really similar to whether you use . or :
Likes: newbie2018
Suppose there are many enemy instance that each has a timed behaviour like this:
Can someone explain this? I'm still new with this lua thing..
Thanks~
Quickly drafted on my ipod and modified later (but didn't test) but should work as a basis :
=================
Enemy.lua
=================
Scene.lua
=================
main.lua
=================
I hope it helps.
Likes: fajarnugroho23
but just to explain the difference, if you define method through colon (:), as you did with function Enemy:onTimer(enemyTimer), then it means that it converts first parameter to self variable inside function. thus if you add event listener like this:
Likes: fajarnugroho23
whoa, thank you!
i'll try it this night~
as @ar2rsawseen just did there are many little subtleties that you will discover if you take time to look at the code. I don't have time to comment but it should get you started.
Good luck
(ps : when I joined this forum I had no experience with lua at all; so no doubt you'll be able to get used to it fast.)
now i have a better understanding about variable passing in lua..
thanks again @Mells
you're welcome