接下来讲解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
以上分两个类
分别是enemy和enemyHP的处理
#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 { 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下,玩家先***了三下,再打了其他敌人几下,及回过来只要打两下就行了。具体的实现下一篇将会处理,有问题欢迎留言。 另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前题目:Cocos2D-X系列之RPG横版过关游戏完整版实例5-创新互联
标题链接:http://cdkjz.cn/article/jpjop.html