Cocos2d-X 3.4版-自定义血量条《赵云要格斗》

Cocos2d-X 3.4版-自定义血量条《赵云要格斗》,第1张

概述在ProgressView中,主要有变化的地方就是Rect的构造方式改变了。从CCReckMake改为Rect::Rect(...); 血量条的基本思路就是以要显示在Scene上的血量条的长度作为基准,通过比较玩家当前的血量,和总血量 对学两条选择性的显示一部分,来表示玩家血量的变化。 ProgressView.h //// ProgressView.h// Fight//// Cr

在ProgressVIEw中,主要有变化的地方就是Rect的构造方式改变了。从CCReckMake改为Rect::Rect(...);

血量条的基本思路就是以要显示在Scene上的血量条的长度作为基准,通过比较玩家当前的血量,和总血量

对学两条选择性的显示一部分,来表示玩家血量的变化。

ProgressVIEw.h

////  ProgressVIEw.h//  fight////  Created by Cytzrs on 15/2/3.////#ifndef __fight__ProgressVIEw__#define __fight__ProgressVIEw__#include <iostream>#include "cocos2d.h"using namespace cocos2d;class ProgressVIEw : public Node{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 Rect &rect);    private:    Sprite *m_progressBackground;//背景血条    Sprite *m_progressForeground;//前景血条    float m_totalProgress;//总血量    float m_currentProgress;//当前血量    float m_scale;//放大倍数};#endif /* defined(__fight__ProgressVIEw__) */
ProgressVIEw.cpp
////  ProgressVIEw.cpp//  fight////  Created by Cytzrs on 15/2/3.////#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 = Sprite::create(pname);    this->addChild(m_progressBackground);}voID ProgressVIEw::setForegroundTexture( const char *pname ){    m_progressForeground = Sprite::create(pname);    m_progressForeground->setAnchorPoint(Vec2(0.0f,0.5f));//设置锚点    m_progressForeground->setposition(Vec2(-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 Point from = m_progressForeground->getTextureRect().origin;    const Rect rect = Rect::Rect(from.x,from.y,rectWIDth,m_progressForeground->getContentSize().height);    setForegroundTextureRect(rect);}voID ProgressVIEw::setForegroundTextureRect( const Rect &rect ){    m_progressForeground->setTextureRect(rect);}float ProgressVIEw::getCurrentProgress() const{    return m_currentProgress;}float ProgressVIEw::getTotalProgress() const{    return m_totalProgress;}

最后将血量条加入Scene:
    //设置英雄的血条	m_pProgressVIEw = new ProgressVIEw();	m_pProgressVIEw->setposition(Vec2(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);        //下面两个是为了让血条更好好看	Sprite *xuekuang=Sprite::create("kuang.png");//添加血条的框架	xuekuang->setposition(Vec2(m_pProgressVIEw->getpositionX(),m_pProgressVIEw->getpositionY()));	Sprite *touxiang=Sprite::create("touxiang.png");//添加英雄的左上角的小头像	touxiang->setposition(Vec2(m_pProgressVIEw->getpositionX()-120,m_pProgressVIEw->getpositionY()));	this->addChild(touxiang,2);	this->addChild(xuekuang,2);	this->addChild(m_pProgressVIEw,2);
其中xuekuang,touxiang两个精灵与血量条的实现无关,只是起到装饰作用,所以要通过m_pProgressVIEw的位置

来决定把自己放在那里来进行装饰。

然后再血量需要改变的地方调用

m_pProgressVIEw->setCurrentProgress(m_pProgressVIEw->getCurrentProgress()-1);
通过改变setCurrentProgress的参数来改变血量值,通常的做法是对当前的血量加减来实现。


关于学两条,在我自己的工作经历中,我选择了使用ControlSlIDer控件来实现,下边是我的方式:

首先在XXX.h中(我的是HellWorld.h)声明一个ControlSlIDer实例:

ControlSlIDer *slIDer;
然后再.cpp中这样使用:
    slIDer = ControlSlIDer::create(Sprite::create("xue_back1.png"),Sprite::create("xue_fore1.png"),Sprite::create() );    slIDer->setAnchorPoint(Vec2(0.0f,0.5f));    slIDer->setMinimumValue(0.0f); // Sets the min value of range    slIDer->setMaximumValue(100.0f); // Sets the max value of range    slIDer->setposition(kuang->getpositionX()+1,kuang->getpositionY());    slIDer->setEnabled(false);    this->addChild(slIDer);        slIDer->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::valueChanged),Control::EventType::VALUE_CHANGED);

同样的,我们还是采用类似的方法来改变血量条的现实:
slIDer->setValue(slIDer->getValue()+1);

最后,通过给ControllSlIDer设置回调,我们可以再slIDer发生变化时做一些特定的是,不过我现在还没想到要做什么

暂且打个log吧。

代码入下:

voID valueChanged(Ref *sender,Control::EventType controlEvent);

voID HelloWorld::valueChanged(Ref *sender,Control::EventType controlEvent){    log("---df-ds-f-%f",slIDer->getValue());    if(slIDer->getValue() > -0.0000001 && slIDer->getValue() < 0.0000001)    {        for(int i = 0; i < 100; i++)        {            log("U dE");        }    }}
PS:多写博客,帮助自己,方便他人! 总结

以上是内存溢出为你收集整理的Cocos2d-X 3.4版-自定义血量条《赵云格斗》全部内容,希望文章能够帮你解决Cocos2d-X 3.4版-自定义血量条《赵云要格斗》所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1037520.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存