Reputation: 19
as my game progresses the enemies get stronger. To make the enemies stronger I made a private int called thealth (probably a bad programming choice) and each time the sprite is touched thealth--; until it reaches zero and the sprite is removed. This worked fine until I decided to have more than one sprite in my scene at a time. The problem I am having is that if this sprite has a thealth of two (for example)and I touch it, all the other sprites have their thealth--; too. So how can I give each sprite its own health so if I touch it the other sprites will not be 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;
}
Upvotes: 1
Views: 284
Reputation: 8426
Suggestions to fix your code:
Make sure the enemy sprite is a class and thealth is contained in it(a little obvious but just in case)
Make sure the thealth variable doesn't have the static keyword (if it does then that variable will be shared in all instances of the class).
eg.
class Enemy extends Sprite{
int thealth=4; //Or whatever you want really
}
Upvotes: 2