思路和上一篇(利用裁剪节点生成)一样。现在师范下ui::scrollVIEw 实现跑马灯,注:可以是文本跑马灯也可以是其他Node.
那么废话就暂不说了,先上代码
头文件
/**************************************************************************** copyright (c) 2014-10-11 Real.xm Create by q229827701 http://blog.csdn.net/q229827701 ****************************************************************************/#pragma once#include "cocos2d.h"#include "ui/UIScrollVIEw.h"class ScrollText:public cocos2d::Node{ public: enum class Font_TYPE { BolD,NOMAL,SliM }; public: CREATE_FUNC(ScrollText); /** @pragma pMoveChild 需要移动的Node @pragma otherChID 其他需要显示的Node */ static ScrollText* create(cocos2d::Node* pMoveChild,...); static ScrollText* create(Font_TYPE mType); /** @pragma isScroll 设置是否需要自动滚动 @pragma byWIDth 设置是否需要根据长度来滚动 PS:如果按照长度来设置,则长度小于遮罩长度的将不予滚动 */ voID setautoScroll(bool isScroll,bool byWIDth=false);CC_CONSTRUCTOR_ACCESS: ScrollText(); virtual ~ScrollText(); virtual bool init(); virtual bool initWithDatas(cocos2d::Node* &pMoveChild); virtual bool init(Font_TYPE mType); bool initLable(Font_TYPE mType); bool initScroll(); voID update(float delta); virtual voID onEnterTransitionDIDFinish();CC_SYNTHESIZE_Readonly(cocos2d::Node*,_mLable,Lable);CC_SYNTHESIZE_Readonly(cocos2d::Size,_mSize,TextSize);CC_SYNTHESIZE(std::string,_content,Content); public: voID setSize(const cocos2d::Size &size); private: cocos2d::Vector<Node*> _mNodes; bool _autoScroll; bool _byWIDth; cocos2d::ui::ScrollVIEw* _scrollVIEw;};
源文件
#include "ScrollText.h"#include "ui/UIScrollVIEw.h"USING_NS_CC;#define IF_RETURN(cont,p) if ((cont)){return (p);}#define IF_RETURN_FALSE(cont) IF_RETURN(cont,false)ScrollText::ScrollText():_autoScroll(false),_byWIDth(false),_mLable(nullptr),_content("content"),_scrollVIEw(nullptr),_mSize(120,40){}ScrollText::~ScrollText(){ CC_SAFE_RELEASE(_mLable); CC_SAFE_RELEASE(_scrollVIEw);}bool ScrollText::init(){ IF_RETURN_FALSE(!init(Font_TYPE::NOMAL)); return true;}bool ScrollText::init( Font_TYPE mType ){ bool ret = false; if (Node::init()) { IF_RETURN_FALSE(!initLable(mType)); IF_RETURN_FALSE(!initScroll()); return true; } return ret;}bool ScrollText::initScroll(){ _scrollVIEw = ui::ScrollVIEw::create(); IF_RETURN_FALSE(!_scrollVIEw); _scrollVIEw->setContentSize(_mSize); addChild(_scrollVIEw); this->setContentSize(_scrollVIEw->getContentSize()); for (auto child:_mNodes) { IF_RETURN_FALSE(!child); _scrollVIEw->addChild(child); } IF_RETURN_FALSE(!_mLable); _scrollVIEw->addChild(_mLable); return true;}voID ScrollText::update( float delta ){ //log("ScrollText::update"); if (!_mLable) { return; } float currentX = _mLable->getpositionX(); float contentX = getContentSize().wIDth*(-1.0f); float lableX = _mLable->getContentSize().wIDth*(-1.0f); if(_byWIDth) { _autoScroll = _mLable->getContentSize().wIDth>getContentSize().wIDth?true:false; } if (_autoScroll) { if(_mLable->getpositionX()>=lableX) _mLable->setpositionX(_mLable->getpositionX()-1.0f); else { _mLable->setpositionX(-contentX); } } else { _mLable->setpositionX(0); } auto label = dynamic_cast<Label*>(_mLable); if(label&&label->getString()!=_content) { label->setString(_content); }}voID ScrollText::setautoScroll( bool isScroll,bool byWIDth/*=false*/ ){ _byWIDth = byWIDth; if(isScroll&&byWIDth) { _autoScroll = _mLable->getContentSize().wIDth>getContentSize().wIDth?true:false; } else _autoScroll = isScroll;}ScrollText* ScrollText::create(cocos2d::Node* pMoveChild,... ){ auto *sTxt = new ScrollText(); if (sTxt ) { va_List lst; va_start(lst,pMoveChild); Node* pNow; pNow=pMoveChild; bool isFirst = true; while(pMoveChild) { if (nullptr!=pNow) { if(isFirst) isFirst = false; else sTxt->_mNodes.pushBack(pNow); pNow=va_arg(lst,Node*); } else break; } va_end(lst); if(sTxt->initWithDatas(pMoveChild)) { sTxt->autorelease(); return sTxt; } else { delete sTxt; sTxt = NulL; return NulL; } } else { delete sTxt; sTxt = NulL; return NulL; } }ScrollText* ScrollText::create( Font_TYPE mType ){ auto text = new ScrollText(); if(text&&text->init(mType)) { text->autorelease(); return text; } else { delete text; text = nullptr; return nullptr; }}bool ScrollText::initWithDatas(cocos2d::Node* &pMoveChild){ bool ret = false; if (Node::init()) { IF_RETURN_FALSE(!pMoveChild); _mLable = pMoveChild; _mLable->setAnchorPoint(Vec2::ANCHOR_MIDDLE_left); IF_RETURN_FALSE(!initScroll()); //scheduleUpdate(); return true; } return ret;}voID ScrollText::setSize( const cocos2d::Size &size ){ _mSize = size; _scrollVIEw->setContentSize(_mSize); this->setContentSize(_mSize);}bool ScrollText::initLable( Font_TYPE mType ){ //typedef struct //{ // Font_TYPE Font_type; // std::function<TTFConfig()> callTTF; //} TTFModel; //TTFModel ttfConfigs[]={ // {Font_TYPE::BolD,[](){ TTFConfig ttfConfig("Fonts/HKYuanMini.ttf",30,GlyphCollection::CUSTOM,"应用名称");return ttfConfig;}},// {Font_TYPE::NOMAL,[](){TTFConfig ttfConfig("Fonts/arial.ttf",GlyphCollection::DYNAMIC,nullptr,true); return ttfConfig;}},// {Font_TYPE::SliM,true); return ttfConfig;}} //}; //auto ttf = ttfConfigs[(int)mType].callTTF(); ////TTFConfig ttf("Fonts/HKYuanMini.ttf","应用名称"); //IF_RETURN_FALSE(!&ttf); //_mLable = Label::createWithTTF(ttf,"content",TextHAlignment::left,_mSize.wIDth); _mLable = Label::createWithSystemFont("content","Arial Bold",40); IF_RETURN_FALSE(!_mLable); _mLable->setAnchorPoint(Vec2::ANCHOR_BottOM_left); _mLable->retain(); return true;}voID ScrollText::onEnterTransitionDIDFinish(){ scheduleUpdate();}总结
以上是内存溢出为你收集整理的cocos2dx 3.2下利用ui::scrollView实现跑马灯滚动效果全部内容,希望文章能够帮你解决cocos2dx 3.2下利用ui::scrollView实现跑马灯滚动效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)