interfaces callback - wxyz-abcd/node-haxball GitHub Wiki
This object consists of functions regarding event callbacks. Here is a detailed documentation of the default event callbacks for this API.
-
add(eventName: string, metadata: object): void
Creates all custom callbacks about a new event called
eventName. For example, this function might have to be used (and maybe overridden for usage ofmetadata) in a GUI application to define event callbacks related to gui events such as keyboard, mouse, touch, timer etc. The main event callback defined in allRoomobjects to trigger the new callbacks is"_on" + eventName.Parameters:
-
eventName: string: The name of the new event, which should start with a capital letter. -
metadata: object: This value is currently not used anywhere; but just in case, the default keys for this object is as follows:-
params: string[]: Short explanations for each parameter of this event.
-
Return value: void
-
-
remove(eventName: string): void
Removes the callbacks created by
Callback.add. Added for convenience. This function should not normally be needed.Parameters:
-
eventName: string: The name of the event to be removed.
Return value: void
-
Let's say that we are working in browser environment and we need our plugins to react to mouse wheel events on a HTMLElement. Here are the steps to make this happen:
-
Just after initializing the API, add this line:
Callback.add("Wheel")
This will define the main
_onWheel()callback in everyRoomobject that is created after this line is executed. -
When the wheel event happens, call the
_onWheel()callback.document.getElementById("canvas").onwheel = function(event){ room._onWheel(event); };
It will trigger all RoomConfig, Plugin and Renderer callbacks for all
Roomobjects. -
Now we might define and use the callbacks wherever we want. Note that the structure of these functions will always be the same for every callback created by
Callback.add.-
Inside a RoomConfig: (We do not need to add all 3 callbacks.)
this.onBeforeWheel = function(event){ //return customData; }; this.onWheel = function(event, customData){ //... }; this.onAfterWheel = function(event, customData){ //... };
-
Inside a Plugin or a Renderer:
this.onWheel = function(event, customData){ //... };
-
By directly modifying the built-in RoomConfig inside a Room object: (We do not need to add all 3 callbacks.)
room.onBeforeWheel = function(event){ //return customData; }; room.onWheel = function(event, customData){ //... }; room.onAfterWheel = function(event, customData){ //... };
-