It looks like you're new here. If you want to get involved, click one of these buttons!
-- example 1 ClassA = Core.class(EventDispatcher) ClassB = Core.class(EventDispatcher) function ClassA:funcA(event) print("funcA", self, event:getType(), event:getTarget()) end local a = ClassA.new() local b = ClassB.new() b:addEventListener("myevent", a.funcA, a) -- when b dispatches an "myevent" event, -- a.funcA will be called with 'a' -- as first parameter b:dispatchEvent(Event.new("myevent")) -- will print "funcA" -- example 2 Ball = Core.class(Sprite) function Ball:onEnterFrame() self:setX(self:getX() + 1) end ball = Ball.new() ball:addEventListener(Event.ENTER_FRAME, ball.onEnterFrame, ball) |
Comments
My understanding is it starts at the beginning and continues in a linear fashion until it reaches the end, this is why you must declare everything before it's used (or else lua assumes it's a global and if not found there is nil)
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
Moreover;
Are you trying to learn more about lua or do you have a specific instance where something isn't working as you'd assumed?
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
A function has to be declared first before you use it (you can also use a forward declaration). If the function is not called, it's not executed.
So "funcA" will never be printed if the method ClassA:funcA is never called, or triggered by an event.
With addEventListener you are adding a 'listener' to "myevent".
If "myevent" happens, a.funcA is called. In the meantime, nothing happens.
You can call your function :
If you asked for something else, please be more specific.
How is it working then ?
And all the events are dispatched inside UrlLoader class - internally. That's why they does not appear in the code.
You can create you own class i separate file which dispatches events. And then use it in another file (creating class instance, etc) and there will be no code to dispatch event, because you are doing it inside your class in another file.
Hope that clears something
Given that a URL is passed via the .new() function - internally the UrlLoader class will actually start the download process working, I'm not 100% sure of the exact implementation as to how it works but you can think of it as running on a separate thread parallel to what's currently happening if you like.
Lines 2 -> 17 then tell the INTERPRETER to define functions so that later on they can be called.
lines 18-20 then adds the references to the above functions to the loader table (or object instance if you prefer) so that once the UrlLoader instance that is loader has finished (or develops an error) it can tell you about it.
Remember the actual process of doing the http request etc for UrlLoader will take a specific amount of time - in terms of computing an absolute age compared to the time it takes lua to process and action the rest of the code, so the chances of UrlLoader completing BEFORE the event listeners are added is tiny.
Does that help?
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill