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 dmen

So, I have a function that populates the screen - it is called from init(), and then later on from elsewhere. Inside of this function I do like var sky = ig.game.spawnEntity(EntitySky, 0, 0, { level: lvl });
When I call this again, later on, I get a new sky entity on top of the old one - instead of replacing the old one. This probably makes sense if you're up to speed on scope in js but I am still learning. If I change the var sky to this.sky will that take care of it? Or do I really need to kill() the first one before creating a new one? Sorry, just thinking about this (lol) and can't test so I thought I'd post.

1 decade ago by drhayes

You really need to kill the old one. It's not a scope thing, it's an object thing.

Every time you call spawnEntity it creates an object that starts floating around out there in object land. It doesn&039;t matter if you have a reference to it or not (#var sky or even this.sky), that object is still gonna be out there, floating around, responding to calls to getEntitiesByType and update and draw because of how Impact itself is tracking those entities.

By calling kill on it, you&039;re telling Impact not to worry about that object anymore and stop calling #update and draw on it. Once nothing is referencing it the JS runtime will destroy it for you.
Page 1 of 1
« first « previous next › last »