EventHandlr.js
A timed events library derived from Full Screen Mario This has two functions:
- Provide a flexible alternative to setTimeout and setInterval that respects pauses and resumes in time (such as from game pauses)
- Provide functions to automatically 'cycle' between certain classes on an object
Essential Functions
Code | Output |
---|---|
window.MyEventHandler = new EventHandlr();
MyEventHandler.addEvent(function() { console.log("It's starting!"); });
MyEventHandler.addEvent(function() { console.log("It has ended.."); }, 49);
MyEventHandler.addEvent(function() { console.log("Can you see why this won't be reached?"); }, 96);
MyEventHandler.addEventInterval(function() { console.log("Running..."); }, 7, 6);
for(var i = 0; i < 70; ++i)
MyEventHandler.handleEvents();
|
It's starting!
Running...
Running...
Running...
Running...
Running...
Running...
It has ended..
|
Command | Usage | Result |
---|---|---|
new EventHandlr |
new EventHandlr();
(or)
window.MyEventHandler = new EventHandlr({
onSpriteCycleStart: "onadding",
doSpriteCycleStart: "placed",
cycleCheckValidity: "alive",
timingDefault: 9
});
|
Creates a new EventHandlr object with the given settings. The following attributes (with defaults in parenthesis) may be passed in:
|
handleEvents |
handleEvents)
|
Increments the game count by one tick, and runs every event scheduled for the new time.
MyEventHandler.addEvent(function() { console.log("Hi!"); });
MyEventHandler.handleEvents();
// Console logs: "Hi!"
|
addEvent |
addEvent(function, time_delay=1[, arguments...]);
|
Will run the
MyEventHandler.addEvent(
function(name) {
console.log("I, " + name + ", will show in seven ticks!"); },
7, "Robert"
);
|
addEventInterval |
addEventInterval(function, time_delay=1, num_repeats[, arguments...]);
|
Will run the
MyEventHandler.addEventInterval(function(name) { console.log("I, " + name + ", will show every seven ticks, fourteen times!"); }, 7, 14, "Blake");
|
clearEvent |
clearEvent(event);
|
Stops a given event from executing ever again.
var event = MyEventHandler.addEvent(function() { console.log("This never shows!"); });
MyEventHandler.clearEvent(event);
|
clearAllEvents |
clearAllEvents();
|
Completely erases all events from the scheduler.
var event = MyEventHandler.addEvent(function() { console.log("This never shows!"); });
var event = MyEventHandler.addEventInterval(function() { console.log("This neither!"); }, 7, Infinity);
MyEventHandler.clearAllEvents();
|
Class Cycling
Code | Output |
---|---|
window.MyEventHandler = new EventHandlr();
var me = { className: "myclass", doSpriteCycleStart: true};
MyEventHandler.addSpriteCycle(me, ["one", "two", "three"]);
MyEventHandler.addEventInterval(function(my_obj) { console.log(my_obj.className); }, 7, Infinity, me);
console.log(me.className, "(starting)");
for(var i = 0; i < 49; ++i)
MyEventHandler.handleEvents();
|
myclass one (starting)
myclass two
myclass three
myclass one
myclass two
myclass three
myclass one
myclass two
|
Command | Usage | Output |
---|---|---|
addSpriteCycle |
addSpriteCycle(me, classnames[, cyclename[, cycletime]])
|
Initializes a cycle of
var me = { className: "myclass", doSpriteCycleStart: true};
MyEventHandler.addSpriteCycle(me, ["one", "two", "three"]);
|
addSpriteCycleSynched |
addSpriteCycleSynched(me, classnames[, cyclename[, cycletime]])
|
Waits to call
var me = { className: "myclass", doSpriteCycleStart: true};
MyEventHandler.addSpriteCycleSynched(me, ["one", "two", "three"]);
|