if you want to make a hp bar for circle or images
please quotation to
http://www.cocos2d-x.org/boards/6/topics/16779
http://www.pyehouse.com/2011/08/31/cocos2d-health-bar/
it like this situation
yellow is hp
green is buff
blue is shield
// using header file
#include "HpBar.h"
class Scene : public CCLayer
{
// add member
HpBar* m_hpBar;
std::vector m_heroHp; // hp bar data
};
// using cpp file
bool Scene::initMenuImage()
{
m_hpBar = HpBar::create();
m_hpBar->setAnchorPoint(ccp(0, 0.5f));
m_hpBar->setPosition(ccp(80.5f, 745));
this->addChild(m_hpBar, 1000);
m_heroHp.erase(m_heroHp.begin(), m_heroHp.end());
}
// ėŽėĐ
void Scene::update()
{
int pHp = hero->getHP();
int sum = 0;
for(unsigned int i = 0; i < m_heroHp.size(); i++)
{
sum += m_heroHp[i];
}
if ( sum > pHp)
{
int gap = sum - pHp;
for( int i = m_heroHp.size()- 1; i >= 0; --i)
{
int hp = m_heroHp[i] - gap;
if ( hp > 0 )
{
m_heroHp[i] = hp;
break;
}
else
{
gap -= m_heroHp[i];
m_heroHp[i] = 0;
}
}
}
m_hpBar->refresh(m_heroHp, divideHpInBar);
}
HpBar.h.zip (0.5 KB)
HpBar.cpp.zip (1.7 KB)