Javascript element articulates out of list

If I have an array of objects, is there any way that the element can be spliced ​​from the array that contains it?

For example: if a bad guy dies, he grows up from an array of active enemies.

I probably lost my mind, but this ability has greatly simplified my code, so I hope for something cool =)

+5
source share
5 answers

The way you do this is as follows:

var game_state = { active_enemies: [] }; function Enemy() { // Various enemy-specific things go here } Enemy.prototype.remove = function() { // NOTE: indexOf is not supported in all browsers (IE < 8 most importantly) // You will probably either want to use a shim like es5-shim.js // or a utility belt like Underscore.js var i = game_state.active_enemies.indexOf(this); game_state.active_enemies.splice(i, 1); } 

Cm:

Notta bene: There are several problems with this appeal to the game state. Make sure that you are consistent (i.e. do not have enemies removing themselves from the list of active enemies, but heroes remove enemies from the map). This will also complicate the situation as the code becomes more complex (your enemy is not only an enemy in the game, but also a map state manager, but he is probably not the only map state manager. When you want to make changes to how you manage the state of the map, you want to make sure that the code is structured in such a way that you only need to change it in one place [preferred]).

+12
source

Assuming the bad guy knows which list he is on, why not?

 BadGuy.prototype.die = function() { activeEnemies.splice(activeEnemies.indexOf(this), 1); } 

By the way, for older browsers use indexOf in arrays, you will need to add it manually .

+6
source

Do you want to avoid circular links

0
source

I would suggest creating an object / class that represents a list of active enemies. Create methods in this instance to add / remove this element from the list - abstracting the internal work of the data structure from the outside world. If the list of active enemies is global (for example, only one of them), you can simply reference it directly to call the delete function when you die. If this is not global, you will need to give each element a link to the list so that it can call the function to delete.

0
source

You can also use the object and, instead of splicing, remove the adversary:

 var activeEnemies = {}; function Enemy() { this.id = Enemy.getId(); // function to return unique id activeEnemies[this.id] = this; // .... } Enemy.getId = (function() { var count = 0; return function() { return 'enemyNumber' + count++; } }()); Enemy.prototype.exterminate = function() { // do tidy up delete activeEnemies[this.id]; } Enemy.prototype.showId = function() { console.log(this.id); } Enemy.prototype.showEnemies = function() { var enemyList = []; for (var enemy in activeEnemies) { if (activeEnemies.hasOwnProperty(enemy)) { enemyList.push(enemy); } } return enemyList.join('\n'); } var e0 = new Enemy(); var e1 = new Enemy(); console.log( Enemy.prototype.showEnemies() ); // enemyNumber0 // enemyNumber1 e0.exterminate(); console.log( Enemy.prototype.showEnemies() ); // enemyNumber1 
-1
source

All Articles