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 cjennison

I am trying to make it so when a player hits the "Bomb" item, all entities under the correct name on the screen are destroyed, however, sometimes only some of them are destroyed!

killEnemiesOnScreen: function(){
		for (var i = 0; i <= this.getEntitiesByType(EntitySpike).length; i++){
			console.log("KILL");
			this.getEntitiesByType(EntitySpike)[i].kill();
		}
	},

What's wrong?

1 decade ago by paulh

did you upgrade to 1.2 yet?

1 decade ago by dominic

When you kill an entity and call getEntitiesByType() again, it won't return this killed entity.

So, if your first call to getEntitiesByType() returns 10 entities and you delete one, your second call will only return 9 entities, but your i variable is already incremented to 1 - skipping the the first entity of the remaining 9.

Also, getEntitiesByType() is quite expensive. You shouldn't call it multiple times if you don't need to.

Storing the returned array in a local variable solves both issues:
killEnemiesOnScreen: function(){
	var enemies = this.getEntitiesByType(EntitySpike);
	for (var i = 0; i < enemies.length; i++){
		enemies[i].kill();
	}
},

1 decade ago by cjennison

what seems to happen is now the method only kills all the enemies the 2nd time it is called.
What could be causing that?
Page 1 of 1
« first « previous next › last »