#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)); }效果图:
cocos2d-x学习日志(15) --公告栏的实现(文字左右移动),布布扣,bubuko.com
cocos2d-x学习日志(15) --公告栏的实现(文字左右移动)
原文:http://blog.csdn.net/rexuefengye/article/details/21026891