java - Health for each enemy -


as game progresses enemies stronger. make enemies stronger made private int called thealth (probably bad programming choice) , each time sprite touched thealth--; until reaches 0 , sprite removed. worked fine until decided have more 1 sprite in scene @ time. problem having if sprite has thealth of 2 (for example)and touch it, other sprites have thealth--; too. how can give each sprite own health if touch other sprites not effected?

@override public boolean onareatouched(final touchevent pscenetouchevent, final float ptoucharealocalx, final float ptoucharealocaly) {     if (pscenetouchevent.isactiondown()) {         if (this.getalpha() > 0.8f) {             thealth--;         }         if (thealth == 0) {             this.detachself();             mscene.unregistertoucharea(this);             score++;             if (score < 35) {                 thealth = 1;             }             if (score > 35) {                 thealth = 2;             }             if (score > 100) {                 thealth = 3;             }         }         return true;     }     return false; } 

suggestions fix code:

  1. make sure enemy sprite class , thealth contained in it(a little obvious in case)

  2. make sure thealth variable doesn't have static keyword (if variable shared in instances of class).

eg.

class enemy extends sprite{     int thealth=4; //or whatever want } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -