2023-05-21 22:06:04 -04:00
..
2023-05-21 22:06:04 -04:00
2023-05-21 22:06:04 -04:00

EventHandlr.js

A timed events library derived from Full Screen Mario This has two functions:

  1. Provide a flexible alternative to setTimeout and setInterval that respects pauses and resumes in time (such as from game pauses)
  2. 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:
  • Default timing
    • time (0): What time the game starts at. Only useful if events is also passed in.
    • events ({}): A pre-existing list of events to be run.
    • timingDefault (7): The default amount of ticks between cycleClass cycles.
  • Names of attributes used by cycleClass functions
    • cycles ("cycle"): What to store an object's cycles under, such as me.cycles.
    • className ("className"): The actual class name string to be manipulated.
    • onSpriteCycleStart ("onSpriteCycleStart"): The attribute under which the function to start a cycle is stored.
    • doSpriteCycleStart ("doSpriteCycleStart"): The boolean attribute to check whether an object should immediately start a cycle.
    • cycleCheckValidity (null): The (optional) boolean attribute to determine if an object should no longer have a cycle.
  • Special functions for manipulating classes
    • addClass: Normally just adds a class to className.
    • removeClass: Normally just uses a quick regular expression to remove a class from className.

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 function in time_delay ticks, passing any given arguments. This is equivalent to setTimeout(function).

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 function in time_delay ticks a num_repeats number of times, passing any given arguments. num_repeats may be a function to evaluate or a typical number. This is equivalent to setInterval(function).

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 .classNames for an object. The object will continuously cycle through them until clearAllCycles or clearClassCycle are called.

var me = { className: "myclass", doSpriteCycleStart: true};
MyEventHandler.addSpriteCycle(me, ["one", "two", "three"]);

addSpriteCycleSynched

addSpriteCycleSynched(me, classnames[, cyclename[, cycletime]])

Waits to call addSpriteCycle until it's synchronized with time (using modular arithmetic). Calling multiple of these at different times will result in the events being in sync.

var me = { className: "myclass", doSpriteCycleStart: true};
MyEventHandler.addSpriteCycleSynched(me, ["one", "two", "three"]);