This forum is read only and just serves as an archive. If you have any questions, please post them on

1 decade ago by ape

What's the best way to ensure that is available in entities that are spawned within game.main?

For example, let's say I have an entity I use for grouping "child" entities. I spawn my "parent" within the init method in game.main using this.spawnEntity().

I then want the "parent" to take care of spawning its children. So I find myself doing this a lot:

update: function() {
  if ( && this.loaded == false) {
    this.loaded = true;,0,0);

It works fine, but it doesn't feel right. I kinda wish I could just rely on being available.


1 decade ago by MikeL

Funny you should mention that. We were just talking about that on IRC earlier. I think that Dominic is going to come up with a more formalized way of doing this. There were some suggestions made.

It appears that in your main.js init, you can do this: = this;

In your case, before you spawn anything. Again, I think that Dominic will be making a more formal way of doing this.

1 decade ago by dominic

ig.system creates your game class like this: = new (gameClass)();

So first the init() method of your game class is called and only after it has completely finished, the instance is assigned to Use the workaround from MikeL if you want to use during your game's init().

I think I will put an init() method into the base ig.Game() class for the next version. It then assigns itself to - requiring you to call this.super() in your game's init() seems like a cleaner solution than setting yourself. Still not perfect though :/

1 decade ago by Jesse

That could almost be worse for someone that doesn't call the parent (the implementation of "init" for the Game class) because will never be defined! You could also leave in what you currently have as a back-up.

A fool proof way to solve it at the engine-level is to define staticInstantiate in the Game class and set it there. I just tried it and saw it go from undefined to an instance of my subclassed Game.
	staticInstantiate: function() { = this;

Now I can use "" from within any Entity, whereas before it was throwing a null reference exception for at least one "update" call of one of my entities. Because I was loading a level inside of the "init" of my game, wasn't set yet...but now it is.

1 decade ago by ape

@MikeL: Thanks! that's so embarrassingly simple.

@Jesse: You bring up a good point. Though I wonder how often people are subclassing Game, let alone subclassing it without calling this.parent();

I think for now I'll go with MikeL's solution so it's both simple and explicit. In fact, I'd be comfortable having it work that from now on. It's far more explicit than calling this.super() as dominic suggested for future releases.
Page 1 of 1
« first « previous next › last »