知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
Cocos2d
發(fā)表時間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):49
本章要講解給怎么在界面上加一個血條,慣例子,照樣在Cocos2d-x地圖隨精靈無窮滾動與邊沿檢測----之游戲開辟《趙云要格斗》 的基本長進行增長功能的。
cocos2d-x版本:2.2.5
工程情況:windows7+VS2010
先來看看效不雅吧:因為還沒有惹人怪物,所以我弄成進擊一次,血條少1或10兩種來看看效不雅
[img]http://img.blog.csdn.net/20150106220045236
[img]http://img.blog.csdn.net/20150106220020088
其拭魅這里改變血量很簡單了,
目次:
一、自定義血條類
二、應用自定義血條類并進行美化
三、改變豪杰血量
一、自定義血條類
本著后頭血條要給怪物來竽暌姑,所以設計了一個血條類。道理其實就是兩個ccsprite對像,控制前景示的大年夜小就可以改變血岑嶺。
起首是資本,在Resources添加以下圖片
紅血條(前景):[img]http://img.blog.csdn.net/20150106215015899
灰血條(背景):[img]http://img.blog.csdn.net/20150106215128174
血條框架:[img]http://img.blog.csdn.net/20150106215143010
<span style="font-size:24px;">最后,有須要的把郵箱留下,不管是工程照樣資本都可以~</span>
趙云左上角小頭像:[img]http://img.blog.csdn.net/20150106215125656
頭文件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;//放大年夜倍數(shù) }; #endif
實現(xiàn)文件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()函數(shù)中初始化:
//設置豪杰的血條 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);效不雅:
[img]http://img.blog.csdn.net/20150106214412013
半血
[img]http://img.blog.csdn.net/20150106214523430
感到好丑啊,想想再給血條加個框,再加個小頭像
將膳綾擎改為:
//設置豪杰的血條 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);
下面再來看看效不雅:
[img]http://img.blog.csdn.net/20150106214745609
事實袈滟次證實,美工是多么重要啊!如許子明顯好看多了,這時血條有了。
三、改變豪杰血量
m_pProgressView->setCurrentProgress(修改); 這一句就可以了,那我們要怎么來驗證了,我想到了一個辦法,進擊一次,血條讓它本身少1(初始是100);
void HelloWorld::update(float delta)函數(shù)中修改:
void HelloWorld::update(float delta) { //斷定是否按下?lián)u桿及其類型 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))//精靈沒達到窗口中心地位或者地圖已經(jīng)移動到邊沿了,精靈才可以移動,不然只播放動畫 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:
[img]http://img.blog.csdn.net/20150106220020088
每次削減10:
[img]http://img.blog.csdn.net/20150106220045236
相關案例查看更多
相關閱讀
- 開發(fā)微信小程序
- 專業(yè)網(wǎng)站建設公司
- 云南軟件定制
- 云南小程序制作
- 云南旅游網(wǎng)站建設
- 小程序密鑰
- 昆明小程序哪家好
- 做網(wǎng)站
- 云南網(wǎng)站建設專家
- 微信分銷系統(tǒng)
- 報廢車回收管理軟件
- web學習路線
- 網(wǎng)站建設制作
- 網(wǎng)站建設需要多少錢
- 網(wǎng)站建設列表網(wǎng)
- 昆明做網(wǎng)站
- 汽車回收管理
- 云南網(wǎng)站建設價格
- 網(wǎng)站沒排名
- 貴州小程序開發(fā)
- 云南小程序哪家好
- 網(wǎng)絡公司聯(lián)系方式
- 云南小程序開發(fā)費用
- 企業(yè)網(wǎng)站
- 網(wǎng)站建設案例
- 網(wǎng)站建設報價
- 報廢車回收
- 云南省住房建設廳網(wǎng)站
- 云南小程序開發(fā)哪家好
- 紅河小程序開發(fā)