• Content Count

  • Joined

  • Last visited

About begedin

  • Rank

Contact Methods

  • Twitter
  1. I don't have full access to the project at the moment, so I can't try it out. However, I did dig through the documentation and the forum a bit and found the "addChild" option, so I agree it might be a better choice. But the question remains, at what point should I extend the events object of the parent class when extending a Phaser object? It looks like the events object remains undefined in the constructor even after calling the base constructor, so I can't extend it there. Should I do it in init or create, or at some other point?
  2. There is also another way. If you don't need the value of the "this" variable within the event handler, you can override it by using "bind()". In your case this would look like: Strategy.Game.prototype = { create: function() { this.greenGroup =; this.greenGroup.create(100,100, 'greenParticle'); this.getPath(); }, getPath: function() { $.getJSON("", function (data) { console.log(JSON.stringify(data)); data.forEach(function(step)) { // need access to "game.greenGroup" here to create group entities and move existing ones. } }.bind(this)); }} By Adding ".bind(this) at the end, you have overridden the default value of this within the handler with the local value. If you don't need the local value, that's certainly on option. .bind() actually works with multiple arguments to, but I don't think that helps you here. for instance, if you used .bind(this, "bla") instead of just .bind(this), then the value of the "data" argument in the function will also change to "bla". .bind() is somewhat related to .call() and .apply(). Here's some reading that explains it in more detail: Basically, .bind is used to say "when you later call this function, call it with the context I'm telling you to use". On the other hand, .call() and .apply() both mean "call this function now with this context" except there's a minor, but potentially important difference in how you specify the context between those two.
  3. In a project I'm working on, we have a card object, which is an extended group. It represents a sort of TCG card graphic. It's an extended group because it consists of several different sprites, some of which are fixed, while some vary with the card type. There's the background card sprite, three different icons (one for each but the top corner) and the character graphic that goes on top. The thing is, I'd like to add some events to this extended group, but I'm not sure how to do it without overriding the existing events object that get's added on the group some time later in the initialization. To explain a bit better what I'm trying to do, here's a code snippet of my constructor: var Combatant = function (game, team, position, texture) { = game;,; // removed code that's adding adding various sprites to the group here var self = this; this.customEvents = { onDamaged: new Phaser.Signal(), onHealed: new Phaser.Signal() } }; Combatant.prototype = Object.create(Phaser.Group.prototype); Combatant.prototype.constructor = Combatant;Now, as you can see, I've added a "customEvents" object where all my extra events will go. However, I feel it would be better to have those objects within the regular events object of the base Phaser.Group base. The thing is, if I do it at this point in the constructor, the regular events object is still undefined, so there's nothing to extend. On the other hand, just plain renaming "this.customEvents" to "" also somehow prevents the default group events from being added to the object. So, any idea? What would be the best way to extend the default events object? Is it done in init? Create? Some other time? What's the convention?