每个敌人的健康

时间:2013-05-19 00:47:26

标签: java android andengine

随着我的游戏进程,敌人变得更强大。为了让敌人变得更强大,我创建了一个名为thealth的私有int(可能是一个糟糕的编程选择),并且每次精灵触及thealth--;直到它达到零并且精灵被移除。这个工作正常,直到我决定一次在我的场景中有多个精灵。我遇到的问题是,如果这个精灵有两个健康(例如)并触摸它,所有其他精灵都有其健康 - ;太。那么我怎样才能给每个精灵赋予自己的健康,所以如果触摸它,其他的精灵就不会受到影响?

@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 个答案:

答案 0 :(得分:2)

修改代码的建议:

  1. 确保敌人精灵是一个类,其中包含健康(有点明显,但以防万一)

  2. 确保thealth变量没有static关键字(如果确实,那么该变量将在该类的所有实例中共享)。

  3. 例如

    class Enemy extends Sprite{
        int thealth=4; //Or whatever you want really
    }
    
相关问题