cocos2d-x学习日志(15) --公告栏的实现(文字左右移动)
2014-03-11 17:36
399 查看
公告栏用的是CClayer(层)或者node节点,锚点位置是(0,0),文字信息使用CCLabelTTF保存,锚点位置是(0,0),使用的时候将它加入到node里面就好了
文字移动的思路是:
每次都update公告的CCLabelTTF的坐标,为了让它从右往左进行移动,右边栏出来,左边栏消失,需要设置一下CCLabelTTF的可显示区域,CCLabelTTF::setTextureRect函数正是设置Label的可显示区域,因此左右边界需要特殊处理,解决方法:
HelloWorldScene.h
参考博文:http://blog.csdn.net/yeweiouyang/article/details/12075039
文字移动的思路是:
每次都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; class HelloWorld : public cocos2d::CCLayer { public: // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); CREATE_FUNC(HelloWorld); // implement the "static node()" method manually virtual void update(float delta); CCLabelTTF* adLabel; CCLayerColor * adCClayer; CCRect m_informRect; float m_informScrollX; }; #endif // __HELLOWORLD_SCENE_H__HelloWorldScene.cpp
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); adCClayer = CCLayerColor::create(ccc4(255,0,0,100),300,30); adCClayer->setPosition(ccp(visibleSize.width/10, visibleSize.height/2 + origin.y)); adCClayer->setAnchorPoint(ccp(0,0)); this->addChild(adCClayer); CCSize size = adCClayer->getContentSize(); adLabel = CCLabelTTF::create("helloworld", "Arial", 24); adLabel->setAnchorPoint(ccp(0,0)); adLabel->setPosition(CCSize(size.width,0)); m_informScrollX = size.width; m_informRect = adLabel->getTextureRect(); this->scheduleUpdate(); adCClayer->addChild(adLabel); return true; }
void HelloWorld::update(float delta) { CCPoint pt = adCClayer->getPosition(); CCSize size = adCClayer->getContentSize(); // 文字X轴的左边界 m_informScrollX -= 1.0f; if (m_informScrollX < -m_informRect.size.width) { m_informScrollX = size.width; adLabel->setTextureRect(CCRectMake(0, 0, m_informRect.size.width, size.height)); CCLog("helloworld"); this->unscheduleUpdate(); //滚动文字越过左边界 } // 文字从右边出来 int expose = size.width - m_informScrollX; if (expose < m_informRect.size.width) { // 文字部分未全部显示出来 adLabel->setTextureRect(CCRectMake(0, 0, expose, size.height)); } else { // 文字部分已经从右边全部显示出来 adLabel->setTextureRect(CCRectMake(0, 0, m_informRect.size.width, size.height)); } // 文字从左边消失 if (m_informScrollX <= 0) { float offset = fabs(m_informScrollX); adLabel->setTextureRect(CCRectMake(offset,0,adLabel->getTextureRect().size.width-offset,size.height)); return; } adLabel->setPosition(CCSize(m_informScrollX,0)); }效果图:
参考博文:http://blog.csdn.net/yeweiouyang/article/details/12075039
相关文章推荐
- 关于指针的一些事情
- C/C++数据对齐详细解析
- C++中引用的使用总结
- C与C++之间相互调用实例方法讲解
- C++中引用(&)的用法与应用实例分析
- 解析C++ 浮点数的格式化输出
- 深入分析C++中几个最不常用的关键字
- c++中inline的用法分析
- C++ Primer 第一部分基本语言
- 深入解析C++ Data Member内存布局
- 从汇编看c++中默认构造函数的使用分析
- 关于C++中的友元函数的一些总结
- C++的sstream标准库详细介绍
- 基于C++自动化编译工具的使用详解
- 浅谈C++中的string 类型占几个字节
- C/C++ 宏详细解析
- 深入分析C++中两个大数相乘结果不正确的问题
- 探讨C++中数组名与指针的用法比较分析
- 深入解析C++中的引用类型
- C++可变参数的实现方法