Hey all! Need some feedback on something.
I've been thinking for a while about how to make programming interactive games/etc. a little easier...
how to take some of the common, repetitive code and handle it for you via an include module.
So today I made a little module to support an event-driven style.
It makes a subclass of Sprite, called EventSprite, that automatically invokes various methods when stuff happens.
So for example, there are handlers for when the sprite is clicked, when the mouse is dragged after it was clicked, and when the mouse is released after it was clicked.
These are called onClick
, onDrag
, and onDragEnd
. So after creating an EventSprite, you can just do something like this:
spr.onClick = function()
self.scale = 1.5
end function
spr.onDrag = function()
self.x = mouse.x
self.y = mouse.y
end function
spr.onDragEnd = function()
self.scale = 1
end function
Then I have similar event handlers for when keys are pressed. But since there are a lot of different keys you might want to respond to, rather than define functions for all of them, I bundle them up into maps. You use them like this:
spr.onKey["right"] = function()
self.x = self.x + 10
end function
(That one fires as long as the key is held; there's also onKeyDown
and onKeyUp
if you just want to respond to key down/up.)
And there's also an update method you can define that simply gets called on every frame. 🤔 Though I suppose to be consistent, maybe I should call that one onFrame
or onUpdate
or similar.
Here's the question...
What other events might you want to respond to? And apart from sprites, what else should be able to respond to events?