前言

做了以下优化

  1. 确定不同子弹的功能:

    • 蓝色:冰冻效果
    • 红色:火焰效果
    • 绿色:治疗经过的右方建筑(只能治疗一次)
    • 黑色:无效果
  2. 增加血量变化的数字

子弹效果

Enemy.h

vector<Sprite*> iceStates;        //冰霜状态
vector<Sprite*> fireState;        //火焰状态
void stateUpdate(float dt);        //状态更新
void addIce();                //增加冰
void addFire();                //增加火
void damageOverTime(float dt);        //持续伤害
void hpChangeNum(int type);            //血量变化数字

敌人状态的更新

void Enemy::stateUpdate(float dt) {
    switch (state) {
        case 0:
            if (iceStates.size() > 0) {
                removeChild(iceStates[0], true);
                iceStates.erase(iceStates.begin() + 0);
                if (iceStates.size() == 0) {
                    state = -1;
                    stopActionByTag(0);
                    float second = (getPosition().x - FISH_POSITION_X) / ENEMY_MOVESPEED;    //计算运动时间
                    ActionInterval* move = MoveTo::create(second, Vec2(FISH_POSITION_X, getPosition().y));
                    move->setTag(0);
                    runAction(move);
                }
            }
            break;
        case 1:
            break;
        case 2:
            if (fireState.size() > 0) {
                removeChild(fireState[0], true);
                fireState.erase(fireState.begin() + 0);
                state = -1;
            }
            break;
    }
}

给敌人增加debuff

void Enemy::addIce() {
    auto* ice = Sprite::create("ice.png");
    ice->setPosition(rand() % int(getContentSize().width), rand() % int(getContentSize().height));
    iceStates.push_back(ice);
    addChild(ice);
    stopActionByTag(0);
    float second = (getPosition().x - FISH_POSITION_X) / ENEMY_MOVESPEED * 1.5;    //计算运动时间
    ActionInterval* move = MoveTo::create(second, Vec2(FISH_POSITION_X, getPosition().y));
    move->setTag(0);
    runAction(move);
}
void Enemy::addFire() {
    auto* fire = Sprite::create("fire.png");
    fire->setPosition(rand() % int(getContentSize().width), rand() % int(getContentSize().height));
    fireState.push_back(fire);
    addChild(fire);
}

敌人的持续伤害

void Enemy::damageOverTime(float dt) {
    if (state == 2) {
        hp -= 1;
        hpChangeNum(e_fireCutHp);
    }
}

然后在enemy中绑定定时器,或者在catTD中调用即可

血量变化数字

HpChange类

class HpChange :public Sprite {
public:
    static HpChange* create(int type);
    void initial(Vec2 pos);
    void disappear(float dt);
};
HpChange* HpChange::create(int type) {
    HpChange* sprite = new HpChange();
    const char* filename;
    switch (type) {
        case e_cutHp:
            filename = "cuthp.png";
            break;
        case e_IceCutHp:
            filename = "cuthpice.png";
            break;
        case e_fireCutHp:
            filename = "cuthpfire.png";
            break;
        case e_addHp:
            filename = "addhp.png";
            break;
    }
    if (sprite && sprite->initWithFile(filename)) {
        sprite->autorelease(); return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return nullptr;
}
void HpChange::initial(Vec2 pos) {
    setPosition(pos.x, pos.y);
    schedule(schedule_selector(HpChange::disappear), 0.5f);
}
void HpChange::disappear(float dt) {
    removeFromParentAndCleanup(true);
}

敌人类中

void Enemy::hpChangeNum(int type) {
    if (type == 1) {
        type = 3;
    }
    HpChange* hpChange = HpChange::create(type);
    hpChange->initial(Vec2(getContentSize().width / 2, getContentSize().height / 2));
    addChild(hpChange);
}

塔类中

void Town::hpChangeNum(int type) {
    HpChange* hpChange = HpChange::create(type);
    hpChange->initial(Vec2(getContentSize().width / 2, getContentSize().height / 2));
    addChild(hpChange);
}

然后在catTD中调用即可

演示

Last modification:July 12th, 2020 at 12:58 am