Difference between revisions of "Events"
m (→Example) |
|||
Line 1: | Line 1: | ||
− | 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 | + | 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 eventLoop.stop call, you would have to design your game around using this module by making all Sprites in your game EventSprites. |
== Classes == | == Classes == | ||
Line 14: | Line 14: | ||
− | + | == 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. | 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. | ||
+ | |||
+ | === Methods === | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! Name !! Description | ||
+ | |- | ||
+ | | run || The main game loop. Continues to execute until stop is called or the program is interrupted. | ||
+ | |- | ||
+ | | stop || Terminates the main loop on the next frame. | ||
+ | |- | ||
+ | | update || Called from the main loop each frame. Responsible for maintaining state of all events. | ||
+ | |- | ||
+ | | doAfter || Schedules a function call to happen at a later physical time. | ||
+ | |- | ||
+ | | cancelDoAfter || Removes a previously scheduled call by doAfter. | ||
+ | |} | ||
Latest revision as of 12:40, 5 September 2025
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 eventLoop.stop call, you would have to design your game around using this module by making all Sprites in your game EventSprites.
Contents
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.
Methods
Name | Description |
---|---|
run | The main game loop. Continues to execute until stop is called or the program is interrupted. |
stop | Terminates the main loop on the next frame. |
update | Called from the main loop each frame. Responsible for maintaining state of all events. |
doAfter | Schedules a function call to happen at a later physical time. |
cancelDoAfter | Removes a previously scheduled call by doAfter. |
Example
import "events"
clear
display(4).mode = displayMode.sprite
spr = new events.EventSprite
spr.image = file.loadImage("/sys/pics/Wumpus.png")
spr.onKey = new events.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
events.eventLoop.onKey["escape"] = function
eventLoop.stop
end function
events.eventLoop.onKeyDown["space"] = function; print "space down"; end function
events.eventLoop.onKey["space"] = function; print "space held"; end function
events.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.)"
events.eventLoop.run