Impact

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

1 decade ago by isaac

I am trying to spawn a same entity when I click on one on the screen.
eg. Spawn another entityA upon clicking entityA.

Since I want to have many of these to be done. I tried to not hardcode the function, eg. ig.game.spawnEntity(EntityA,x,y);
Instead, I tried to do
ig.game.spawnEntity(this,x,y)

But then it says something like "this is not an object".

Is there a way to do it?

1 decade ago by isaac

The exact error is
Uncaught TypeError: object is not a function game.js:160

1 decade ago by drhayes

The first argument to ig.game.spawnEntity is supposed to be the class or the string name of a class, not an instance of the class. so ig.game.spawnEntity(this, x, y); won't work.

JavaScript is funny about classes, but in Impact classes are functions... hence the error message. It's expecting to call a function to get an instance back but is getting an object instead.

As for making your solution generic, I&039;m not sure there is a good answer other than storing the name of the type somewhere (like #className: 'EntityA') and then having a method that relies on that property:

spawnAnother: function() {
   ig.game.spawnEntity(this.className, this.pos.x, this.pos.y);
}

1 decade ago by isaac

Thanks for clearing my confusing about the function. Looks like hardcoding is still the only way to do it.
Page 1 of 1
« first « previous next › last »