1

随着我的游戏进行,敌人会变得更强大。为了让敌人更强大,我创建了一个名为 thealth 的私有 int(可能是一个糟糕的编程选择),每次精灵被触动时,thealth--;直到它达到零并且精灵被移除。这工作得很好,直到我决定一次在我的场景中有多个精灵。我遇到的问题是,如果这个精灵有两个(例如)并且我触摸它,那么所有其他精灵都有他们的 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;
}
4

1 回答 1

2

修复代码的建议:

  1. 确保敌人的精灵是一个类并且包含在其中(有点明显但以防万一)

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

例如。

class Enemy extends Sprite{
    int thealth=4; //Or whatever you want really
}
于 2013-05-19T03:56:40.567 回答