Monday, May 10, 2010

Bad Javascript

‹prev | My Chain | next›

Now that I have my PlayerList object in each browser that encapsulates access to the list of players other than me, I need to user that in the fab.js/comet code that drives players. In the function that broadcasts to all listeners, I access the player being moved in the window's parent (this is comet rendered in an iframe):
function broadcast(obj) {
function(listener) {
var body = '<script type="text/javascript">' + "\nif (console) console.debug('" + obj.body + "')\n</script>\n";
listener({body: body});

body = '<script type="text/javascript">' + "\n" +
"var attrs = " + obj.body + ";\n" +
"window.parent.player_list.add_player(attrs);\n" +
"var player = window.parent.player_list.get_player(;" +
"if (typeof(player) != 'undefined') {\n" +
" player.walk_to(attrs.x, attrs.y);\n" +
"}" +
listener({body: body});
The add_player method is silently ignored if the player is already a member of the room (or is me):
PlayerList.prototype.add_player = function(obj) {
if (!this.other_players[] && != {
if (console) {console.debug("adding:" +;}
this.other_players[] = new Player(, obj);
The walk_to method is already a working method on Player so I should be good to go. Except I am not.

Sadly, I spend the bulk of my night tracking down a problem that boils down to this: in Javascript, do not give the same name to both a method and a property. By way of example, when I try to obtain the list of other players, this is bad:
PlayerList.prototype.others = function() {
var ret = [];
for (var id in this.others) {
return ret;
After renaming the object property to other_players, I eventually get the code mostly working. Mostly, except the fab.js broadcast code is only broadcasting to one client. I will pick up with that next.

Day #99

No comments:

Post a Comment