健康对每个敌人 [英] Health for each enemy

查看:129
本文介绍了健康对每个敌人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的游戏进行的敌人变得更强。为了使敌人强盛我做了一个私人诠释称为thealth(可能是一个不好的编程选择),每个精灵被触摸thealth--时间;直到达到零和精灵被除去。这工作得很好,直到我决定在一次在我的场景不止一个精灵。我遇到的问题是,如果这个精灵有两个(例如)thealth,我触摸它,所有其他的精灵都有自己thealth--;太。所以,我怎么可以给每个精灵自身的健康,所以如果我触摸它其他的精灵不会被影响?

  @覆盖
公共布尔onAreaTouched(最终的TouchEvent pSceneTouchEvent,最终浮动pTouchAreaLocalX,最终浮动pTouchAreaLocalY){
    如果(pSceneTouchEvent.isActionDown()){
        如果(this.getAlpha()> 0.8f){
            thealth--;
        }
        如果(thealth == 0){
            this.detachSelf();
            mScene.unregisterTouchArea(本);
            分数++;
            如果(得分&下; 35){
                thealth = 1;
            }
            如果(得分→35){
                thealth = 2;
            }
            如果(得分大于100){
                thealth = 3;
            }
        }
        返回true;
    }
    返回false;
}


解决方案

建议,以解决您code:


  1. 确认敌方精灵是一个类,thealth它所包含的(有点明显,但以防万一)


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


如:

 阶级敌人扩展Sprite {
    INT thealth = 4; //或者任何你想要的真的
}

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

解决方案

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
}

这篇关于健康对每个敌人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆