cocos2d-x2.2--公告栏的实现(文字左右移动)

前端之家收集整理的这篇文章主要介绍了cocos2d-x2.2--公告栏的实现(文字左右移动)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
公告栏用的是CClayer(层)或者node节点,锚点位置是(0,0), 文字信息使用CCLabelTTF保存,锚点位置是(0,0),使用的时候将它加入到node里面就好了

文字移动的思路是:

每次都update公告的CCLabelTTF的坐标,为了让它 从右往左进行移动,右边栏出来,左边栏消失,需要设置一下CCLabelTTF的可 显示区域,CCLabelTTF::setTextureRect函数正是设置Label的可显示区域,因此左右边界需要特殊处理,解决方法

HelloWorldScene.h
  1. #ifndef__HELLOWORLD_SCENE_H__
  2. #define__HELLOWORLD_SCENE_H__
  3. #include"cocos2d.h"
  4. #include<renren-ext.h>
  5. USING_NS_CC;
  6. classHelloWorld:publiccocos2d::CCLayer
  7. {
  8. public:
  9. //Here'sadifference.Method'init'incocos2d-xreturnsbool,insteadofreturning'id'incocos2d-iphone
  10. virtualboolinit();
  11. //there'sno'id'incpp,sowerecommendreturningtheclassinstancepointer
  12. staticcocos2d::CCScene*scene();
  13. //aselectorcallback
  14. voidmenuCloseCallback(CCObject*pSender);
  15. CREATE_FUNC(HelloWorld);
  16. //implementthe"staticnode()"methodmanually
  17. virtualvoidupdate(floatdelta);
  18. CCLabelTTF*adLabel;
  19. CCLayerColor*adCClayer;
  20. CCRectm_informRect;
  21. floatm_informScrollX;
  22. };
  23. #endif//__HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
    boolHelloWorld::init()
  1. {
  2. //////////////////////////////
  3. //1.superinitfirst
  4. if(!CCLayer::init())
  5. returnfalse;
  6. }
  7. CCSizevisibleSize=CCDirector::sharedDirector()->getVisibleSize();
  8. CCPointorigin=CCDirector::sharedDirector()->getVisibleOrigin();
  9. adCClayer=CCLayerColor::create(ccc4(255,100),300,30);
  10. adCClayer->setPosition(ccp(visibleSize.width/10,visibleSize.height/2+origin.y));
  11. adCClayer->setAnchorPoint(ccp(0,0));
  12. this->addChild(adCClayer);
  13. CCSizesize=adCClayer->getContentSize();
  14. adLabel=CCLabelTTF::create("helloworld","Arial",24);
  15. adLabel->setAnchorPoint(ccp(0,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> adLabel->setPosition(CCSize(size.width,0));
  16. m_informScrollX=size.width;
  17. m_informRect=adLabel->getTextureRect();
  18. this->scheduleUpdate();
  19. adCClayer->addChild(adLabel);
  20. true;
  21. }

    voidHelloWorld::update(floatdelta)
  1. CCPointpt=adCClayer->getPosition();
  2. CCSizesize=adCClayer->getContentSize();
  3. //文字X轴的左边界
  4. m_informScrollX-=1.0f;
  5. if(m_informScrollX<-m_informRect.size.width)
  6. m_informScrollX=size.width;
  7. adLabel->setTextureRect(CCRectMake(0,m_informRect.size.width,size.height));
  8. CCLog("helloworld");
  9. this->unscheduleUpdate();//滚动文字越过左边界
  10. }
  11. //文字从右边出来
  12. intexpose=size.width-m_informScrollX;
  13. if(expose<m_informRect.size.width)
  14. //文字部分未全部显示出来
  15. else
  16. //文字部分已经从右边全部显示出来
  17. adLabel->setTextureRect(CCRectMake(0,size.height));
  18. //文字从左边消失
  19. if(m_informScrollX<=0)
  20. floatoffset=fabs(m_informScrollX);
  21. adLabel->setTextureRect(CCRectMake(offset,adLabel->getTextureRect().size.width-offset,153); background-color:inherit; font-weight:bold">return;
  22. adLabel->setPosition(CCSize(m_informScrollX,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> }
效果图:



参考博文:http://blog.csdn.net/yeweIoUyang/article/details/12075039
原文链接:https://www.f2er.com/cocos2dx/347134.html

猜你在找的Cocos2d-x相关文章