Health for each enemy

孤者浪人 提交于 2020-01-15 12:27:06

问题


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;
}

回答1:


Suggestions to fix your code:

  1. Make sure the enemy sprite is a class and thealth is contained in it(a little obvious but just in case)

  2. 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
}


来源:https://stackoverflow.com/questions/16630434/health-for-each-enemy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!