Events
Jump to navigation
Jump to search
This system module defines the EventSprite class which expands on the Sprite class to add definable event functions called handlers. These handlers are triggered under certain conditions such as a mouse being clicked. A special EventLoop is defined to act as a dedicated game loop that manages all of the event classes. Since the EventLoop is continuous until terminated by an event,
Classes
Name | Description |
---|---|
EventHandler | Stores information about the action to take when responding to an Event. |
KeyHandler | Stores the current state of a particular Key including whether it is Up, Down or being Held down. |
EventSprite | Builds on the Sprite class to add event handlers that respond to events such as: onKey, onKeyDown, onKeyUp, onClick, etc. |
Event Loop
Within the module, there is a special singleton class call eventLoop which is meant to only exist once. It governs the game loop and manages the responses to the triggered events. This is a dedicated game loop which will continue to execute until an event terminates the loop or program in some way.
Example
clear
display(4).mode = displayMode.sprite
spr = new EventSprite
spr.image = file.loadImage("/sys/pics/Wumpus.png")
spr.onKey = new KeyHandler
spr.onKey["left"] = function
self.x = self.x - 10
if self.x < 50 then eventLoop.stop
end function
spr.onKey["right"] = function
self.x = self.x + 10
end function
eventLoop.onKey["escape"] = function
eventLoop.stop
end function
eventLoop.onKeyDown["space"] = function; print "space down"; end function
eventLoop.onKey["space"] = function; print "space held"; end function
eventLoop.onKeyUp["space"] = function; print "space up"; end function
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
spr.x = 480
spr.y = 320
spr.start
print "Click and drag, or press left/right."
print "All event-driven. (Esc to exit.)"
eventLoop.run