Does the dispatchEvent method in the Sprite class do anything other than just fall through to the EventDispatcher.dispatchEvent method? I'm asking because I'm thinking about overriding the Sprite's dispatchEvent method. My overridden method will call EventDispatcher.dispatchEvent(self, event) after it's done doing its thing (because I couldn't figure out a way to make it call back to the original Sprite.dispatchEvent) and I want to know if I'll be losing any functionality by doing so. If so, I can create my own class, "MySprite", and use that wherever I'd currently create a Sprite then in MySprite.dispatchEvent do my custom code and call Sprite.dispatchEvent(self, event), but I'd like to avoid that if it's not necessary. Also, I'd lose the override code in anything that's based on Sprite, such as the stage object.
In case you're wondering WHY I'm thinking of doing this, here's the background:
I'm new to Gideros and lua and trying to implement some general error-handling in my (first) Gideros program. I'm wondering what the best way to do this might be or what other Gideros users are doing. I saw the excellent article by bowerhaus ("Proper Log Crash Logging for Gideros") but that's iOS-specific and I'm writing for Android right now (no Mac to compile on). So, until I'm up to trying to write my own Gideros plug-in, I'm trying to come up with an alternate method.
One thing I'm considering is using pcall() to catch errors and implementing my own logging (similar to bowerhaus' use of freopen() to write errors to a file, but internally in lua). I found through experimentation that this works through method chains so only the topmost method in the chain needs to be surrounded by pcall() (although you lose the stack trace, something I'm trying to figure out a way around). However, events start their own method chain so just starting the application with a single pcall() surrounding a method that kicks everything off won't work. I wrote a general "try()" global method that takes a function (with no arguments) as an argument and calls it using pcall() and does basic error-handling on the result, but putting that around the code inside EVERY event handler seems a bit awkward.
My thought is that I could override Sprite:dispatchEvent and put my try() call around the base dispatchEvent call to catch errors, like this:
function Sprite:dispatchEvent(event)
try(
function()
EventDispatcher.dispatchEvent(self, event)
end
)
end
I can provide more code for try() and the supporting methods if it helps you understand what I'm asking or if anyone is curious.
It seems to work fine, but I'm worried that maybe Sprite:dispatchEvent is doing something other than just calling EventDispatch.dispatchEvent and I'm losing some essential functionality with this approach.
Anyone have any info or feedback on this or general error-handling advice?
Thanks!
Comments
attempt to call field '_dispatchEvent' (a nil value)"
I looked at the table for Sprite and I see "dispatchEvent" but no "_dispatchEvent".
Sprite._dispatchEvent = Sprite.dispatchEvent