parkm / oldbpm

A JavaScript game where you shoot at bubbles.
0 stars 1 forks source link

Events #41

Closed dgpt closed 10 years ago

dgpt commented 10 years ago

Create local/global events

Current plan: Create a class that any event-listenable objects are derived from. An instance of this class is accessed by the events module for the global events.

dgpt commented 10 years ago

Check this out; it's awesome.

dgpt commented 10 years ago

P.S. here's how it works

Local event in an object: this.addListener('name', callback, oneShot); this.removeListener('name') or this.removeListener('name', listener); (if no listener passed, all events are removed) this.triggerEvent('name');

Global events: events.global.addListener(), events.global.removeListener(), events.global.triggerEvent()

parkm commented 10 years ago

I think you should keep some of the old names though. emit is much smaller than triggerEvent. And still is descriptive enough if used like this events.emit. But verbosity isn't much of an issue, so whatever.

dgpt commented 10 years ago

The reason I changed it is because now they are on the object instead of on events.

So, say you wanted to trigger an event on a bullet during Bubble's onBulletCollision:

onBulletCollision: function(bullet) {
    ...
    bullet.triggerEvent('poop');
    ...
}

Also, since all BasicObjects and States inherit events.EventHandler, the function names 'remove', and 'emit' are off limits because particles and states. It's also a good idea to be more verbose when it's related to inheritance (moment of clarity: that's why Java is so verbose).