return false;//矩形1在矩形2下方,两者无碰撞
else if (y1-h1*0.5>y2+h2*0.5)
return false;//矩形1在矩形2上方,两者无碰撞
return true;
}
好了,这里得来讲讲怪物受伤死亡动画了了。
接着上一篇讲的Monster.h增加函数
//受伤动画
void HurtAnimation(const char *name_each,const unsigned int num,bool run_directon);
//受伤动画结束
void HurtEnd();
//判断是否在受伤动画
bool IsHurt;
//死亡动画
void DeadAnimation(const char *name_each,const unsigned int num,bool run_directon);
//死亡动画结束
void DeadEnd();
//判断是否死亡
bool Isdead;
//怪物死亡闪烁结束
void BlinkEnd();
然后在实现函数Monster.cpp
//受伤动画
void Monster:
:
HurtAnimation(const char *name_each,const unsigned int num,bool run_directon)
{
if(IsHurt||Isdead)
return;
//受伤优先
if(IsRunning||IsAttack)
{
m_MonsterSprite->stopAllActions();//当前精灵停止所有动画
//恢复精灵原来的初始化贴图
this->removeChild(m_MonsterSprite,TRUE);//把原来的精灵删除掉
m_MonsterSprite=CCSprite:
:
create(Monster_name);//恢复精灵原来的贴图样子
m_MonsterSprite->setFlipX(MonsterDirecton);
this->addChild(m_MonsterSprite);
IsRunning=false;
IsAttack=false;
}
CCAnimation* animation = CCAnimation:
:
create();
for( int i=1;i<=num;i++)
{
char szName[100] = {0};
sprintf(szName,"%s%d.png",name_each,i);
animation->addSpriteFrameWithFileName(szName); //加载动画的帧
}
animation->setDelayPerUnit(2.8f/14.0f);
animation->setRestoreOriginalFrame(true);
animation->setLoops
(1); //动画循环1次
//将动画包装成一个动作
CCAnimate* act=CCAnimate:
:
create(animation);
//创建回调动作,受伤动画结束调用HurtEnd()
CCCallFunc* callFunc=CCCallFunc:
:
create(this,callfunc_selector(Monster:
:
HurtEnd));
//创建连续动作
CCActionInterval* hurtackact=CCSequence:
:
create(act,callFunc,NULL);
m_MonsterSprite->runAction(hurtackact);
IsHurt=true;
}
//受伤动画结束
void Monster:
:
HurtEnd()
{
IsHurt=false;
Monster_xue->setCurrentProgress(Monster_xue->getCurrentProgress()-10);
if(Monster_xue->getCurrentProgress()==0)
{
//播放怪物死亡动画
DeadAnimation("monster_dead",2,MonsterDirecton);
}
}
//死亡动画
void Monster:
:
DeadAnimation(const char *name_each,const unsigned int num,bool run_directon)
{
Isdead=true;
CCAnimation* animation = CCAnimation:
:
create();
for( int i=1;i<=num;i++)
{
char szName[100] = {0};
sprintf(szName,"%s%d.png",name_each,i);
animation->addSpriteFrameWithFileName(szName); //加载动画的帧
}
animation->setDelayPerUnit(2.8f/14.0f);
animation->setRestoreOriginalFrame(true);
animation->setLoops
(1); //动画循环1次
//将动画包装成一个动作
CCAnimate* act=CCAnimate:
:
create(animation);
//创建回调动作,死亡结束后调用deadact()
CCCallFunc* callFunc=CCCallFunc:
:
create(this,callfunc_selector(Monster:
:
DeadEnd));
//创建连续动作
CCActionInterval* deadact=CCSequence:
:
create(act,callFunc,NULL);
m_MonsterSprite->runAction(deadact);
}
//死亡动画结束
void Monster:
:
DeadEnd()
{
//恢复死亡的样子
this->removeChild(m_MonsterSprite,TRUE);//把原来的精灵删除掉
m_MonsterSprite=CCSprite:
:
create("monster_dead2.png");//恢复死亡的样子
m_MonsterSprite->setFlipX(MonsterDirecton);
this->addChild(m_MonsterSprite);
//存在血条
if(Monster_xue!
=NULL)
{
if(MonsterDirecton)//因为怪物中心不在图片中心,所以只能根据怪物的脸朝向,设置血条的横坐标
Monster_xue->setPosition(ccp(m_MonsterSprite->getPositionX()+60, m_MonsterSprite->getPositionY()));//设置在怪物上头
else
Monster_xue->setPosition(ccp(m_MonsterSprite->getPositionX()-60, m_MonsterSprite->getPositionY()));
}
//怪物闪两下再死亡
CCBlink* blinkact=CCBlink:
:
create(3,6);//3是持续时间,6是闪的次数
//创建回调动作,闪烁结束后调用BlinkEnd()
CCCallFunc* callFunc=CCCallFunc:
:
create(this,callfunc_selector(Monster:
:
BlinkEnd));
//创建连续动作
CCActionInterval* deadact=CCSequence:
:
create(blinkact,callFunc,NULL);
m_MonsterSprite->runAction(deadact);
}
//闪烁结束
void Monster:
:
BlinkEnd()
{
this->removeAllChildren();//把怪物和血条都删除掉;
}
怪物死亡的一个过程,在每次受伤掉血后,立马检测当前血量,如果血量为0,马上播放死亡动画,接着再播放闪烁动画,然后就可以把怪物删除掉了啦~~就这么简单。
效果:
1、怪物在巡逻,这时攻击没有检测到碰撞
2、英雄在攻击,检测到碰撞,怪物受伤并掉血
3、怪物血量为0,怪物死亡,并播放闪烁动画
四、思路总结
这里碰撞检测我是反其它道而行,把所有不碰撞的可能都列出来,其它的不就是碰撞的了么?
然后再来自己编程,另一方面,怪物就是受伤、死亡的动画,以及闪烁,这些都是很基础的,基本上都是相同的函数,只要用一次你就会了。
就是这里要记得这是按顺序的动作,要记得这点就行了。