[置頂] Cocos2d-x碰撞檢測原理與英雄要打死怪物--之游戲開發《趙云要格斗》(7)
來源:程序員人生 發布時間:2015-02-10 08:56:54 閱讀次數:3704次
這里是Evankaka的博客,歡迎大家前面討論與交換~~~~~~
轉載請注明出處http://blog.csdn.net/evankaka/article/details/42689689
本文將詳細講述cocos2dx中英雄與怪物的碰撞檢測原理,其實就是精靈和精靈碰撞檢測哈。本文主要從矩形碰撞入手,自己編寫了1個矩形碰撞檢測的函數,并且在游戲中來進行利用。另外一方面,當英雄出動攻擊后,如果英雄和怪物碰撞到的話,怪物就要掉血,并且當怪物血量為0時,怪物死亡,死亡之前它還會倒在地上閃爍幾下。下面,開始吧
cocos2d-x版本:2.2.5
工程環境:windows7+VS2010
打開方式:將工程放在cocos2d-x安裝目錄下的project文件夾下用VS打開
先來看看效果:


目錄
1、精靈碰撞檢測原理
2、自定義碰撞檢測函數
3、英雄要打死怪物
4、思路總結
1、精靈碰撞檢測原理
碰撞檢測網上有很多人在講,但是1般都只講怎樣用,也都沒具體的講講原理,自己下來就摸索了下,發現其實這個確切很簡單。
首先,我們來看看兩個矩形,我們定義以下兩個矩形,矩形1:紅色;矩形2:黑色

如果我們把它們所有的不碰撞的情形列出來,那末其它的不就是碰撞的么,想到這1點,我就從這個動身,然后它們不碰撞的情形我們可以分為4種
矩形1:紅色;矩形2:黑色
1.矩形1在矩形2左方,二者無碰撞

成立條件:x1+w1*0.5<x2-w2*0.5
2.矩形1在矩形2右方,二者無碰撞

成立條件::x1-w1*0.5>x2+w2*0.5
3.矩形1在矩形2下方,二者無碰撞

成立條件::y1+h1*0.5<y2-h2*0.5
4.矩形1在矩形2上方,二者無碰撞

成立條件:y1-h1*0.5>y2+h2*0.5
上面4種就是所有的不碰撞的情況了,然后我們弄個判斷,順次檢測上面4種情形,1旦發現有1種情況成立,就返回無碰撞,如果4種情況都不成立,那恭喜你了,碰撞成功了!
2、自定義碰撞檢測函數
碰撞檢測對精靈類可以用
sprite1->boundingBox().intersectsRect(sprite1->boundingBox())
只不過我這個游戲中的英雄和怪物都是自己定義的類,所以直接調用上面的函數就出點兒問題,所以自己就把前面碰撞檢測的原理寫了個函數,可以直接調用了,不用管你是甚么對像。
首先,在用到碰撞檢測的地方#include "HelloWorldScene.h"
定義函數
//矩形碰撞檢測
bool isRectCollision (CCRect rect1, CCRect rect2);
然后在其實現函數里HelloWorldScene.cpp里:
///碰撞檢測
bool HelloWorld::isRectCollision (CCRect rect1, CCRect rect2)
{
float x1 = rect1.origin.x;//矩形1中心點的橫坐標
float y1 = rect1.origin.y;//矩形1中心點的縱坐標
float w1 = rect1.size.width;//矩形1的寬度
float h1 = rect1.size.height;//矩形1的高度
float x2 = rect2.origin.x;
float y2 = rect2.origin.y;
float w2 = rect2.size.width;
float h2 = rect2.size.height;
if (x1+w1*0.5<x2-w2*0.5)
return false;//矩形1在矩形2左方,二者無碰撞
else if (x1-w1*0.5>x2+w2*0.5)
return false;//矩形1在矩形2右方,二者無碰撞
else if (y1+h1*0.5<y2-h2*0.5)
return false;//矩形1在矩形2下方,二者無碰撞
else if (y1-h1*0.5>y2+h2*0.5)
return false;//矩形1在矩形2上方,二者無碰撞
return true;
}
這個代碼的原理就是我們上面所講的東西,很簡單吧!
3、英雄要打死怪物
現在我們要調用2中的函數,我們先來看看英雄和怪物的碰撞范圍吧
(我把背景弄透明了,實際是這樣的)
(我把背景弄透明了,實際是這樣的)
所以,這里要注意下。這里就是要謹慎,最好不要把全部圖片的寬度和高度都包括進去;
碰撞檢測的1個簡單流程

然后就是實現了啦~
void HelloWorld::update(float delta)函數中添加
if(hero->IsAttack)//英雄正在攻擊
{
if(!monster1->Isdead)//怪物還沒死
{
if(abs(hero->getPositionY()-monster1->getPositionY())<30)//怪物和英雄應當在1個差不多的水平高度上,攻擊才有效
{
//檢測是不是碰撞到怪物,這里要注意要減去1些邊框值
if (this->isRectCollision(CCRectMake(hero->getPositionX(), hero->getPositionY(),hero->GetSprite()->getContentSize().width⑺0, hero->GetSprite()->getContentSize().height⑶0), CCRectMake(monster1->getPositionX(), monster1->getPositionY(), monster1->GetSprite()->getContentSize().width⑶0,monster1->GetSprite()->getContentSize().height⑵0)))
{
monster1->HurtAnimation("monster_hurt",2,monster1->MonsterDirecton);//受傷
}
}
}
}
好了,這里得來說講怪物受傷死亡動畫了了
接著上1篇講的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次
//將動畫包裝成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()⑴0);
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次
//將動畫包裝成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()⑹0, 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();//把怪物和血條都刪除掉;
}
怪物死亡的1個進程,在每次受傷掉血后,立馬檢測當前血量,如果血量為0,馬上播放死亡動畫,接著再播放閃爍動畫,然后就能夠把怪物刪除掉了啦~~就這么簡單
效果:
1、怪物在巡查,這時候攻擊沒有檢測到碰撞
2、英雄在攻擊,檢測到碰撞,怪物受傷并掉血


3、怪物血量為0,怪物死亡,并播放閃爍動畫

4、思路總結
這里碰撞檢測我是反其它道而行,把所有不碰撞的可能都列出來,其它的不就是碰撞的了么?然后再來自己編程,另外一方面,怪物就是受傷、死亡的動畫,和閃爍,這些都是很基礎的,基本上都是相同的函數,只要用1次你就會了。就是這里要記得這是按順序的動作,要記得這點就好了
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈