Cocos2d-X 3.4版-自定义血量条《赵云要格斗》
来源:互联网 发布:淘宝宝贝规格颜色分类 编辑:程序博客网 时间:2024/04/29 20:10
在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"); } }}
0 0
- Cocos2d-X 3.4版-自定义血量条《赵云要格斗》
- Cocos2d-X 3.4版-扣血飘字 《赵云要格斗》
- Cocos2d-X 3.4版-血条跟随怪物运动《赵云要格斗》
- Cocos2d-x 自定义血条及其美化----之游戏开发《赵云要格斗》(4)cocos2dx 3.3移植版
- Cocos2d-x 自定义血条及其美化----之游戏开发《赵云要格斗》(4)
- cocos2d-x中怎么添加血量条
- cocos2d-x 血量条 CCProgressTimer实现
- Cocos2d-X 3.4版-按钮控制精灵攻击《赵云要格斗》
- Cocos2d-X 3.4版-怪物的AI《赵云要格斗》
- Cocos2d-X 3.4版-碰撞检测原理《赵云要格斗》
- Cocos2d-X 3.4版-技能冷却按钮《赵云要格斗》
- Cocos2d-x游戏开发——血量条
- Cocos2d-x血条跟随怪物运动--之游戏开发《赵云要格斗》(5)cocos2dx 3.3移植版
- Cocos2d-X 3.4版-虚拟摇杆控制精灵上下左右《赵云要格斗》
- Cocos2d-X 3.4版-地图无限滚动与边缘检测《赵云要格斗》
- Cocos2d-X 3.4版-游戏继续,游戏重新开始,回到主界面的实现《赵云要格斗》
- Cocos2d-x自定义按钮类控制精灵攻击----之游戏开发《赵云要格斗》(2) cocos2dx 3.3移植版
- Cocos2d-x血条跟随怪物运动----之游戏开发《赵云要格斗》(5)
- Monkey 命令使用说明
- 日经春秋 20150204
- LeetCode 14.Longest Common Prefix
- 外挂辅助技术-更新任务列表分析
- Linux 实时信号程序中锁的探索
- Cocos2d-X 3.4版-自定义血量条《赵云要格斗》
- 设置vs2010的默认包含路径
- C语言取模和数学取模的区别
- 触发器的分类
- C++ 单链表反转
- 日经社説 20150204 国は汚染土の輸送にも万全を
- oracle returning into
- UVA 10029 Edit Step Ladders(最长上升子序列)
- Visual C++从入门到精通精装版视频教程