|
公告栏用的是CClayer(层)或者node节点,锚点位置是(0,0),
文字信息使用CCLabelTTF保存,锚点位置是(0,0),使用的时候将它加入到node里面就好了
文字移动的思路是:
每次都update公告的CCLabelTTF的坐标,为了让它
从右往左进行移动,右边栏出来,左边栏消失,需要设置一下CCLabelTTF的可
显示区域,CCLabelTTF::setTextureRect函数正是设置Label的可显示区域,因此左右边界需要特殊处理,解决方法:
HelloWorldScene.h
- #ifndef__HELLOWORLD_SCENE_H__
- #define__HELLOWORLD_SCENE_H__
-
- #include"cocos2d.h"
- #include<renren-ext.h>
- USING_NS_CC;
- classHelloWorld:publiccocos2d::CCLayer
- {
- public:
-
- virtualboolinit();
-
- staticcocos2d::CCScene*scene();
-
- //aselectorcallback
- voidmenuCloseCallback(CCObject*pSender);
- CREATE_FUNC(HelloWorld);
- //implementthe"staticnode()"methodmanually
- virtualvoidupdate(floatdelta);
- CCLabelTTF*adLabel;
- CCLayerColor*adCClayer;
- CCRectm_informRect;
- floatm_informScrollX;
- };
- #endif//__HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
boolHelloWorld::init()
- {
- //////////////////////////////
- //1.superinitfirst
- if(!CCLayer::init())
- returnfalse;
- }
- CCSizevisibleSize=CCDirector::sharedDirector()->getVisibleSize();
- CCPointorigin=CCDirector::sharedDirector()->getVisibleOrigin();
- adCClayer=CCLayerColor::create(ccc4(255,100),300,30);
- adCClayer->setPosition(ccp(visibleSize.width/10,visibleSize.height/2+origin.y));
- adCClayer->setAnchorPoint(ccp(0,0));
- this->addChild(adCClayer);
- CCSizesize=adCClayer->getContentSize();
- adLabel=CCLabelTTF::create("helloworld","Arial",24);
- 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));
- m_informScrollX=size.width;
- m_informRect=adLabel->getTextureRect();
- this->scheduleUpdate();
- adCClayer->addChild(adLabel);
- true;
- }
voidHelloWorld::update(floatdelta)
- CCPointpt=adCClayer->getPosition();
- CCSizesize=adCClayer->getContentSize();
- //文字X轴的左边界
- m_informScrollX-=1.0f;
- if(m_informScrollX<-m_informRect.size.width)
- m_informScrollX=size.width;
- adLabel->setTextureRect(CCRectMake(0,m_informRect.size.width,size.height));
- CCLog("helloworld");
- this->unscheduleUpdate();
- }
- //文字从右边出来
- intexpose=size.width-m_informScrollX;
- if(expose<m_informRect.size.width)
- //文字部分未全部显示出来
- else
- //文字部分已经从右边全部显示出来
- adLabel->setTextureRect(CCRectMake(0,size.height));
- //文字从左边消失
- if(m_informScrollX<=0)
- floatoffset=fabs(m_informScrollX);
- adLabel->setTextureRect(CCRectMake(offset,adLabel->getTextureRect().size.width-offset,153); background-color:inherit; font-weight:bold">return;
- 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
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|