jensen Posted February 25, 2014 Share Posted February 25, 2014 Hello, I am curious how people are handling the context for group.callAll when the method being called belongs to the prototype of the child. Here is the situation I ran into. I'd love to see how people are handling a similar case in their usage of Phaser. I have a Cell class that has a draw method. Something like this (code slimmed down for illustrative purposes).Cell = function(game, x, y) { Phaser.Graphics.call(this, game, x, y);}Cell.prototype = Object.create(Phaser.Graphics.prototype);Cell.prototype.constructor = Cell;Cell.prototype.draw = function(x, y) { x = typeof x !== 'undefined' ? x : 0; y = typeof y !== 'undefined' ? y : 0; this.clear(); // Error occurs here, when this is a Boolean this.beginFill(this.color); this.drawRect(this.x + x, this.y + y, CELL_SIZE, CELL_SIZE); this.endFill();}; I also have a group that I created to hold a number of cells. I want to call the draw method on each of them. I also want the context to be within the child so I use the value '' for the context parameter.this.cellGroup.callAll('draw', '', x, y);When I do this, I get an error:Uncaught TypeError: Object [object Boolean] has no method 'clear' When I step through the callstack it is obvious why this error is occurring. Inside Group.js#callAll there is a check for context && callback. Since I have provided both it will attempt to determine the context using this.callbackFromArray. The issue I'm running into is that the contextLength is 1 because ''.split('.') is [''], but this.callbackFromArray returns false because it can't find child[callback[0]]. When false is returned it tries to call clear on the boolean and results in the error. I am able to leave my code unchanged by making this change in Group.js#callAll.else{ var contextLength = 0; // Extract the context into an array if (typeof context === 'string' && context != '') { context = context.split('.'); contextLength = context.length; }}I would love to chalk this up to my inadequate understanding of the JavaScript language. Please advise. Thanks,Karl Link to comment Share on other sites More sharing options...
rich Posted February 25, 2014 Share Posted February 25, 2014 I actually updated Group.callAll tonight to cope better with this. It's in the 1.2 branch but essentially all I did was this: if (typeof context === 'undefined' || context === null || context === '') { context = null; } else { // Extract the context into an array if (typeof context === 'string') { context = context.split('.'); var contextLength = context.length; } }So you can pass null or '' as the context now and it won't try and split it. Link to comment Share on other sites More sharing options...
Recommended Posts