您好,登錄后才能下訂單哦!
接下來講解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<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下,玩家先***了三下,再打了其他敵人幾下,及回過來只要打兩下就行了。具體的實現下一篇將會處理,有問題歡迎留言。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。