好了,让我们继续;
在上一篇的onAttack方法内,我们看到了这两个定时操作:
if(_relation==good){
this->schedule(schedule_selector(Role::attackFromPlayer),2);
}
else{
this->schedule(schedule_selector(Role::attackFromMonster),3);}
没错,这就是角色的攻击动作,这里写的动作非常简单,可以分解为:
auto rotateLeft=RotateTo::create(0.1, -30);
auto rotateRight=RotateTo::create(0.1,30);
auto rotateBack=RotateTo::create(0.1,0);
auto attackCall=CallFunc::create([&](){
if(_relation==good){
CCNotificationCenter::sharedNotificationCenter()->postNotification("player give a hit",NULL);
}
});
auto seq=Sequence::create(rotateLeft,rotateRight,rotateBack,attackCall,NULL);
this->runAction(seq);
每次攻击出去的动作做出来后,使用回调发出一个指令给到GameLayer,由GameLayer通知对手类,更改对方的状态:
_monster->_fsm->doEvent("doBeHit");
GameLayer就担任调度中心的任务。
下面是补充一些细节:
(1)血量条的扣减:
onBeHit内,刷新血量:
_health=_health-400;
_blood->setPercentage((_health/1000)*100);
如果血量为0,判定角色死亡,否则自动攻击:
if(_health>0){
this->_fsm->doEvent("doAttack");
}else{
log("signa, h1");
_fsm->doEvent("doDead");
}
(2)死亡的消息(汗!)
onDead方法内,角色通知调度层GameLayer,由GameLayer停止这个场景(这里做了简单处理,将另一方也置为dead状态,停止动作,汗)
auto onDead=[&](){
this->unscheduleAllSelectors();
this->stopAllActions();
log("i am dead");
CCNotificationCenter::sharedNotificationCenter()->postNotification("someone is killed",NULL);
};
void GameLayer::func3(CCObject* pSender){
if(_player->_fsm->getStatus()!="dead"){
_player->_fsm->doEvent("doDead");
}
}
好的,更多细节就请看代码吧https://github.com/frank1982/rpg-combat-scene-demo-of-cocos2dx