cocos2d-x学习日志(15) --公告栏的实现(文字左右移动)

公告栏用的是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;

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) --公告栏的实现(文字左右移动)_第1张图片

参考博文:http://blog.csdn.net/yeweiouyang/article/details/12075039

你可能感兴趣的:(C++,cocos2d-x)