Disclaimer: What follows is shameless self-promotion.
PÄSSION has a hook-mechanism, called
Beholder. It can work independently from PÄSSION, as long as you "register the events". This is done automatically when you require passion/init.lua, but you can do it yourself like this:
Code: Select all
-- Showing only presses below. Releases are done exactly the same way
function love.keypressed(key)
Beholder.trigger('keypressed_' .. key) -- Notice that I use Beholder.trigger, not Beholder:trigger here
end
function love.mousepressed(x, y, button)
Beholder.trigger('mousepressed_' .. button, x, y)
end
function love.joystickpressed(joystick, button)
Beholder.trigger('joystickpressed_' .. joystick .. '_' .. button)
end
Beholder can work outside PÄSSION, but it needs
MiddleClass. This is how you use it with a class:
Code: Select all
Player = class('Player')
Player:include(Beholder) -- Includes beholder on the class. Adds methods 'observe' and 'stopObserving'
function Player:initialize(x,y)
super.initialize(self)
self:setPosition(x,y)
self:observe('keypressed_up', 'jump') -- binds the "jump" method to the "up" keypress
end
function Player:jump()
... -- do the jump
end
There's also a method for stopping observation of an event (stopObserving). It is usually a good idea to invoke it before destroying an object.
Note that you can also define your own custom events: 'level_finished', 'player_died' or 'pause', for example.
In short:
Code: Select all
-- trigger the event
Beholder.trigger(eventName, ...)
-- observe one event (in a method)
self:observe(eventName, funtionOrMethodName, ...)
-- stop observing that event
self:stopObserving(eventName)