Friday, June 25, 2010

No Joy with eventPhase

‹prev | My Chain | next›

I had a bit of a setback last night. I had hoped to be able to send click events to multiple, overlapping players in my (fab) game.

I am able to generate decorated click events, which I hoped to be able to send to all players in the game at a particular coordinate. Unfortunately, I found yesterday that I had to send the event directly to individual players rather than to the room as a whole (and then onto multiple players).

I am going to make one more run at this today. My only hope at this point is that everything so far has been operating in event bubbling mode. I would like for the room to capture the event, which is then sent to the room descendants—the players.

To simulate events, I have been using the three related methods document.createEvent, event.initMouseEvent, and element.dispatchEvent along the lines of:
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 0, 0, 50, 450, false, false, false, 0, null);
evt.is_collision = true
room.paper.canvas.dispatchEvent(evt);
The list of arguments to event.initMouseEvent is ludicrously long. Of that list, only one argument (number 2) deals with bubbling or capturing, and it only specifies whether or not bubbling has been halted (event.bubbles).

My only hope is the event.eventPhase attribute. For some reason, that defaults to zero in Chrome:



According to the documentation, it ought to be 1, 2, or 3. I want to see if 1, or Event.CAPTURING_PHASE might work:
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 0, 0, 250, 250, false, false, false, 0, null);
evt.is_collision = true // decorate the event to differentiate it from real mouse clicks
evt.eventPhase = Event.CAPTURING_PHASE
Nothing happens, but I am not too surprised, I have not set up a capture listener, which I do by setting the third parameter on addEventListener to true:
  avatar.node.addEventListener("click", function(evt) {
console.debug("here!");
if (evt.is_collision) self.bounce_away();
}, true);
Sadly, that still has no effect.

I ultimately find that setting the eventPhase on the event has no effect. In fact, when I try it in Firefox, I get:
TypeError: setting a property that has only a getter
Sigh. It looks as though it is simply not possible to create capture events. It was fun to explore this stuff, but it looks to be a dead end. I will circle back to where I left off in my (fab) game development tomorrow night.

Day #145

No comments:

Post a Comment