在各种游戏中hp大家可能都不陌生,而与hp息息相关的就是hp条;在这里介绍一下用ui中的LoardingBar创建hp条;
创建一个loardingbar的ui图层并添加
auto hpbar=LoadingBar::create("sliderProgress2.png");
hpbar->setPercent(100);
hpbar->setPositionY(60);
newE->addChild(hpbar);
hpbar->setTag(1000);
setPercent设置hp的百分比100为满
将图层与定义的血量血量关联
void Enemy::jshp()
{
auto shp=(LoadingBar *)this->getChildByTag(1000);
shp->setPercent(HP/15.0 *100);
}
实现hp条随着hp的减少而减少。在碰撞检测hp减少后添加代码
nowe->HP--;
nowe->jshp();