思路和上一篇(利用裁剪节点生成)一样。现在师范下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/346544.html