本节课学习实现玩家血槽创建与玩家血槽为空时的游戏结束(即:游戏场景切换)
1. 玩家血槽创建:这里和怪物血条创建类似
//创建血条
auto hp=LoadingBar::create("sliderProgress2.png"); //创建血条
hp->setTag(2005);
hp->setPercent(100);
spritetool->addChild(hp,1);
hp->setPosition(Vec2(spritetool->getContentSize().width/3+400, spritetool->getContentSize().height / 2)); //设置坐标
hp->setScale(3); //设置血槽形状大小
2. 通过血条值的改变(类似怪物掉血),使得游戏继续进行或结束。这里我们需要写一个OverScene.h和OverScene.cpp来实现血条为零跳转到游戏结束场景。使用一个changePlayerHp()函数来实现,首先我们在GameScene.h里定义一个void changePlayerHp()方法,再到GameScene.cpp里实现这个方法:
void GameScene::changePlayerHp(){
this->playerHp-=20; //设置每次掉20滴血
if(playerHp>=0)
{
LoadingBar * hp=(LoadingBar * )this->getChildByTag(2000)->getChildByTag(2005);
hp->setPercent(this->playerHp);
}else
{
//游戏结束,并跳转到GameOver界面
auto scene=OverScene::createScene();
Director::getInstance()->replaceScene(scene);
}
}
3. OverScene的创建类似于一般场景的创建。