Cocos2d-x 自訂血條及其美化----之遊戲開發《趙雲要格鬥》(4),cocos2d
本章要講解給怎麼在介面上加一個血條,老規矩,還是在Cocos2d-x地圖隨精靈無限滾動與邊緣檢測----之遊戲開發《趙雲要格鬥》 的基礎上進行增加功能的。
在遊戲開發中,血條是一個很重要的東西,這裡的血條是通過兩個圖片來完成,一個是前景圖(紅色),一個是背景圖(灰色),通過改變紅色圖在長度顯示,灰色圖不變就可實現血量的變化了。當然,這裡為了讓介面更加好看些,又為血條加了個血框和人物的小頭像。
cocos2d-x版本:2.2.5
工程環境:windows7+VS2010
先來看看效果吧:由於還沒有引入怪物,所以我弄成攻擊一次,血條少1或10兩種來看看效果
目錄:
一、自訂血條類
二、使用自訂血條類並進行美化
三、改變英雄血量
一、自訂血條類
本著後頭血條要給怪物來用,所以設計了一個血條類。原理其實就是兩個ccsprite對像,控制前景示的大小就可以改變血量了。
首先是資源,在Resources添加以片
紅血條(前景):
灰血條(背景):
血條架構:
趙雲左上方小頭像:
標頭檔ProgressView.h:
#ifndef __PROGRESSVIEW_H__#define __PROGRESSVIEW_H__#include "cocos2d.h"using namespace cocos2d;class ProgressView : public CCNode{public: ProgressView();public://設定血條背景 void setBackgroundTexture(const char *pName);//設定血條前景 void setForegroundTexture(const char *pName);//設定總血量 void setTotalProgress(float total);//設定當前血量 void setCurrentProgress(float progress);//得到當前血量 float getCurrentProgress() const;//得到總血量 float getTotalProgress() const;private://設定前景血條顯示的長度 void setForegroundTextureRect(const CCRect &rect);private: CCSprite *m_progressBackground;//背景血條 CCSprite *m_progressForeground;//前景血條 float m_totalProgress;//總血量 float m_currentProgress;//當前血量 float m_scale;//放大倍數};#endif
實現檔案ProgressView.cpp:
#include "ProgressView.h"ProgressView::ProgressView(): m_progressBackground(NULL), m_progressForeground(NULL), m_totalProgress(0.0f), m_currentProgress(0.0f), m_scale(1.0f){}void ProgressView::setBackgroundTexture( const char *pName ){ m_progressBackground = CCSprite::create(pName); this->addChild(m_progressBackground);}void ProgressView::setForegroundTexture( const char *pName ){ m_progressForeground = CCSprite::create(pName); m_progressForeground->setAnchorPoint(ccp(0.0f, 0.5f));//設定錨點 m_progressForeground->setPosition(ccp(-m_progressForeground->getContentSize().width * 0.5f, 0)); this->addChild(m_progressForeground);}void ProgressView::setTotalProgress( float total ){ if (m_progressForeground == NULL) {return;} m_scale = m_progressForeground->getContentSize().width / total; m_totalProgress = total;}void ProgressView::setCurrentProgress( float progress ){ if (m_progressForeground == NULL) {return;} if (progress < 0.0f) {progress = 0.0f;} if (progress > m_totalProgress) {progress = m_totalProgress;} m_currentProgress = progress; float rectWidth = progress * m_scale; const CCPoint from = m_progressForeground->getTextureRect().origin; const CCRect rect = CCRectMake(from.x, from.y, rectWidth, m_progressForeground->getContentSize().height); setForegroundTextureRect(rect);}void ProgressView::setForegroundTextureRect( const CCRect &rect ){ m_progressForeground->setTextureRect(rect);}float ProgressView::getCurrentProgress() const{ return m_currentProgress;}float ProgressView::getTotalProgress() const{ return m_totalProgress;}
好了,這個血條類就定義好了,編譯下沒報錯,可以移值了。
二、使用自訂血條類並進行美化
首先然要用到的地方,就是HelloWorldScene.h中添加標頭檔#include "ProgressView.h"
然後定義成員變數:
private:HRocker* rocker;//搖杆,第一篇搖杆文章中定義的Hero* hero;///精靈,<span style="font-family: Arial, Helvetica, sans-serif;">第一篇搖杆文章中定義的</span>ControlButton* btn;//按鈕控制項變數,第二篇自訂按鈕定義的 Map* mymap;//地圖 ,第三篇定義的 ProgressView *m_pProgressView; //血條
然後就在init()函數中初始化:
//設定英雄的血條 m_pProgressView = new ProgressView(); m_pProgressView->setPosition(ccp(150, 450)); m_pProgressView->setScale(2.2f); m_pProgressView->setBackgroundTexture("xue_back.png"); m_pProgressView->setForegroundTexture("xue_fore.png"); m_pProgressView->setTotalProgress(100.0f); m_pProgressView->setCurrentProgress(100.0f); this->addChild(m_pProgressView, 2); 效果:
半血
感覺好醜啊,想想再給血條加個框,再加個小頭像
將上面改為:
//設定英雄的血條 m_pProgressView = new ProgressView(); m_pProgressView->setPosition(ccp(150, 450)); m_pProgressView->setScale(2.2f); m_pProgressView->setBackgroundTexture("xue_back.png"); m_pProgressView->setForegroundTexture("xue_fore.png"); m_pProgressView->setTotalProgress(100.0f); m_pProgressView->setCurrentProgress(50.0f); //下面兩個是為了讓血條更好好看CCSprite *xuekuang=CCSprite::create("kuang.png");//添加血條的架構xuekuang->setPosition(ccp(m_pProgressView->getPositionX(),m_pProgressView->getPositionY()));CCSprite *touxiang=CCSprite::create("touxiang.png");//添加英雄的左上方的小頭像touxiang->setPosition(ccp(m_pProgressView->getPositionX()-120,m_pProgressView->getPositionY()));this->addChild(touxiang,2);this->addChild(xuekuang,2);this->addChild(m_pProgressView, 2);
下面再來看看效果:
事實再次證明,美工是多麼重要啊!這樣子明顯好看多了,這時血條有了。
三、改變英雄血量
其實這裡改變血量很簡單了,
m_pProgressView->setCurrentProgress(改動); 這一句就可以了,那我們要怎麼來驗證了,我想到了一個方法,攻擊一次,血條讓它自己少1(初始是100);
void HelloWorld::update(float delta)函數中改動:
void HelloWorld::update(float delta){//判斷是否按下搖杆及其類型CCSize visibleSize1 = CCDirector::sharedDirector()->getVisibleSize();//得到視窗大小switch(rocker->rocketDirection){case 1:hero->SetAnimation("run_animation.plist","run_animation.png","run_",8,rocker->rocketRun);//"run_"為run_animation.png集合圖片中每張圖片的公用名稱部分if(hero->getPositionX()<=visibleSize1.width-8)//不讓精靈超出右邊,8可以改成你喜歡的{if(!hero->JudgePositona(visibleSize1)||mymap->JudgeMap(hero,visibleSize1))//精靈沒到達視窗中間位置或者地圖已經移動到邊緣了,精靈才可以移動,否則只播放動畫hero->setPosition(ccp(hero->getPosition().x+1,hero->getPosition().y)); //向右走//下面是移動地圖mymap->MoveMap(hero,visibleSize1);}break;case 2:hero->SetAnimation("run_animation.plist","run_animation.png","run_",8,rocker->rocketRun);//"run_"為run_animation.png集合圖片中每張圖片的公用名稱部分hero->setPosition(ccp(hero->getPosition().x, hero->getPosition().y+1)); //向上走break;case 3:hero->SetAnimation("run_animation.plist","run_animation.png","run_",8,rocker->rocketRun);//"run_"為run_animation.png集合圖片中每張圖片的公用名稱部分if(hero->getPositionX()>=8)//不讓精靈超出左邊,8可以改成你喜歡的hero->setPosition(ccp(hero->getPosition().x-1,hero->getPosition().y)); //向左走break;case 4:hero->SetAnimation("run_animation.plist","run_animation.png","run_",8,rocker->rocketRun);//"run_"為run_animation.png集合圖片中每張圖片的公用名稱部分hero->setPosition(ccp(hero->getPosition().x,hero->getPosition().y-1)); //向下走break;case 0:hero->StopAnimation();//停止所有動畫和運動break;}//判斷是否出動攻擊if(btn->isTouch){if(hero->IsAttack)//英雄沒在攻擊return;hero->AttackAnimation("attack1_animation.plist","attack1_animation.png","attack_",6,rocker->rocketRun);m_pProgressView->setCurrentProgress(m_pProgressView->getCurrentProgress()-1); //更改血量}}
每次減少1:
每次減少10:
<span style="font-size:24px;">最後,有需要的把郵箱留下,不管是工程還是資源都可以~</span>