Cocos2d
发表时间:2020-10-19
发布人:葵宇科技
浏览次数:52
本章要讲解给怎么在界面上加一个血条,惯例子,照样在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;//放大年夜倍数
};
#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); 效不雅:
[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)函数中修改:
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:
[img]http://img.blog.csdn.net/20150106220020088
每次削减10:
[img]http://img.blog.csdn.net/20150106220045236








