首先,准备两张图片,一张是bar.png,本质上就是一个框,里面可以装血;另一张是blood.png,从名字就可以看出来,是红色的血。预览一下。**
setPosition(Point(size.width/2,size.height-20)); //设置框的位置this->addChild(sprite); //加到默认图层里面去auto sprBlood = Sprite::create("res/game/blood.png"); //创建血条ProgressTimer * progress = ProgressTimer::create(sprBlood); //创建progress对象progress->setType(ProgressTimer::Type::BAR); //类型:条状progress->setPosition(Point(size.width/2,size.height-20));//从右到左减少血量progress->setMidpoint(Point(0,0.5)); //如果是从左到右的话,改成(1,0.5)即可progress->setBarChangeRate(Point(1,0));progress->setTag(BLOOD_BAR); //做一个标记this->addChild(progress);schedule(schedule_selector(GameScene::scheduleBlood), 0.1f); //刷新函数,每隔0.1秒
void GameScene::scheduleBlood(float delta){auto progress &#61; (ProgressTimer *)this->getChildByTag(BLOOD_BAR);progress->setPercentage((((float)existLife)/PLAYER_LIFE)*100); //这里是百分制显示if (progress->getPercentage() <0) {this->unschedule(schedule_selector(GameScene::scheduleBlood));}
}
这里注意一下&#xff0c;setPercentage()函数里面的值是进度条的长度&#xff0c;比如说输入30&#xff0c;显示百分之三十的血量。每次回调如果existLife的值变化的话&#xff0c;就会看到血条的动态改变。上图&#xff1a;