接下来讲解enemy的初始化和随玩家移动的生命条:

#ifndef _ENEMY_H_

#define _ENEMY_H_

#include"cocos2d.h"

#include"core/Weapon.h"

#define MaxBuf 20

using namespace cocos2d;

 

class Enemy:public CCNode

{

public:

    Enemy(void);

    ~Enemy(void);

    CCPoint position;

    int startGID;

    int index;

    bool isRemoveTileEnemy;

    bool isRemoveTile;

    bool removeTile();

    CCPoint getEnemyPosition();

    void setEnemyPosition(CCPoint initEnemyPosition);

    Behaviour getBehaviour();

    void setBehaviour (Behaviour be);

    void setRemoveTile(bool isRemoveTile);

    CCSprite* tempEnemy;

    CCSprite* getEnemy();

    static Enemy * setEnemy();

private:

    bool  initT();

    Behaviour be;

    CCPoint initPosition;

};

class EnemyHPBar:public CCNode

{

public:

    EnemyHPBar(void);

    static EnemyHPBar* setHP(CCPoint spritePosition,CCTMXTiledMap* map,int i);

    CCSprite* uiSprite;

    CCSprite* getHPBar();

    int enemyLife[MaxBuf];

    void setEnemyLife(int life,int i);

    int getEnemyLife(int i);

    void printHP(int i);

private:

    bool initHP(CCPoint spritePosition,CCTMXTiledMap* map,int i);

    void updateEnemy(ccTime dt);

};

#endif

以上分两个类

分别是enemyenemyHP的处理

#include"enemy.h"

#include"cocos2d.h"

 

Enemy::Enemy(void)

{

    isRemoveTile=false;

}

Enemy::~Enemy(void)

{

 

}

Enemy* Enemy::setEnemy()

{

    Enemy * t = new Enemy();

    if(t && t->initT())

        return t;

    return NULL;

}

bool Enemy::initT()

{

    bool bRet = false;

    do

    {

        CCTexture2D* enemy=CCTextureCache::sharedTextureCache()->addImage("buouguai.png");

        tempEnemy=CCSprite::spriteWithTexture(enemy,CCRectMake(0,0,81,81));

        //tempEnemy->setPosition(CCPointZero);

        tempEnemy->setScale(0.7f);

        //tempEnemy->setAnchorPoint(CCPointZero);

        bRet = true;

    } while (0);

    return bRet;

}

CCSprite* Enemy::getEnemy()

{

    return this->tempEnemy;

}

bool Enemy::removeTile()

{

    return this->isRemoveTile;

}

void Enemy::setRemoveTile(bool isRemoveTile)

{

    this->isRemoveTile=isRemoveTile;

}

Behaviour Enemy::getBehaviour()

{

    return this->be;

}

void Enemy::setBehaviour(Behaviour be)

{

    this->be = be;

}

CCPoint Enemy::getEnemyPosition()

{

    return this->initPosition;

}

void Enemy::setEnemyPosition(CCPoint initPosition)

{

    this->initPosition=initPosition;

}

//==========================================class EnemyHPBar=============================================

EnemyHPBar::EnemyHPBar(void)

{

    for(int i=0;i<MaxBuf;i++)

    {

        enemyLife[i]=100;

        //CCLOG("~%d:%d",i,enemyLife[i]);

    }

}

EnemyHPBar* EnemyHPBar::setHP(CCPoint spritePosition,CCTMXTiledMap* map,int i)

{

    EnemyHPBar * t = new EnemyHPBar();

    if(t && t->initHP(spritePosition,map,i))

        return t;

    return NULL;

}

bool EnemyHPBar::initHP(CCPoint spritePosition,CCTMXTiledMap* map,int i)

{

    bool bRet = false;

    do

    {

        CCTexture2D* ui=CCTextureCache::sharedTextureCache()->addImage("boss_hpbar.png");

        uiSprite=CCSprite::spriteWithTexture(ui,CCRectMake(0,0,i/2,5));

        CCPoint uiPosition=ccpAdd(spritePosition,ccp(0,50));

        uiSprite->setPosition(uiPosition);

        bRet = true;

    } while (0);

    return bRet;

}

CCSprite* EnemyHPBar::getHPBar()

{

    return this->uiSprite;

}

int EnemyHPBar::getEnemyLife(int i)

{

    return this->enemyLife[i];//

   

}

void EnemyHPBar::setEnemyLife(int life,int i)

{

   

    this->enemyLife[i]=life;

}

void EnemyHPBar::printHP(int i)

{

    //CCLOG("enemyLife[%d]=%d",i,enemyLife[i]);

}

void EnemyHPBar::updateEnemy(ccTime dt)

{

    CCLOG("updateEnemy");

}

基本上实现了,随敌人移动的生命条的功能,利用数组保存每个敌人的生命值,通过编号对指定的enemy进行处理,比如说敌人要***5下,玩家先***了三下,再打了其他敌人几下,及回过来只要打两下就行了。具体的实现下一篇将会处理,有问题欢迎留言。