How can you make the implementation of the code so that if you change the stats, each Enemy object has its own personal animation ... here's the code:

 public class Enemy{ enum StateEnemy { Walking, Deadening } private StateEnemy stateEnemy = Enemy.StateEnemy.Walking; public boolean isDie = false; } 

via switch choose what I need ( int num in the field of the Game class):

 if(arrayEnemy.size != 0){ switch (arrayEnemy.get(num).getStateEnemy()) { case Walking: frameEnemy = animWalkEnemy.getKeyFrame(stateTime); break; case Deadening: frameEnemy = animDeadEnemy.getKeyFrame(stateTime); break; } } 

then I get to this class by code ( int num in the field of the Game class):

 if(arrayEnemy.get(num).isDie){ arrayEnemy.get(num).setStateEnemy(StateEnemy.Deadening); arrayEnemy.removeIndex(num); break; } 

Rendering goes in the renderer in this way ( int num in the renderer method):

  int num = 0; TextureRegion frameEnemy = null; if(arrayEnemy.size != 0){ switch (arrayEnemy.get(num).stateEnemy) { case Walking: frameEnemy = animWalkEnemy.getKeyFrame(stateTime); break; case Deadening: frameEnemy = animDeadEnemy.getKeyFrame(stateTime); break; } } for(num=0; num < arrayEnemy.size; num++){ batch.draw(frameEnemy, arrayEnemy.get(num).getRect().x, arrayEnemy.get(num).getRect().y, Enemy.WIDTH, Enemy.HEIGHT); } 

The fact is that if two Enemy objects appear on the map in a game, then when one is Deadening stat is also assigned to the Deadening . I assume that the problem lies in the rendering itself.

  • And how is the process of destroying the enemy? Ie how exactly is this in the code you have? - SlandShow
  • as usual I do through arrayEnemy - dev3java
  • @SlandShow if (arrayEnemy.get (num) .isDie) {arrayEnemy.removeIndex (num);} - dev3java
  • @SlandShow the problem is not how I do it ... but the fact that Enum is static ... so the entire animation is played on all objects ... and Enum itself is declared in the Enemy class, this is obvious. - dev3java
  • @SlandShow tobish getting to this class, I expose the array arrayEnemy.get (num) .setStateEnemy (StateEnemy.Deadening); and it stupidly applies to all objects. it's not good - dev3java

1 answer 1

Here is a solution for a variety of objects:

 for(Enemy enemy : arrayEnemy) { TextureRegion frameEnemy = null; switch (enemy.getStateEnemy()) { case Walking: frameEnemy = animWalkEnemy.getKeyFrame(stateTime); break; case Deadening: frameEnemy = animDeadEnemy.getKeyFrame(stateTime); break; } Batch batch = levelMap.getRenderer().getBatch(); batch.begin(); for(num=0; num < arrayEnemy.size; num++){ batch.draw(frameEnemy, enemy.getRect().x, enemy.getRect().y, Enemy.WIDTH, Enemy.HEIGHT); } batch.end(); } 

It was necessary only to do a drawing for each object!