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));
}
效果图:



参考博文:http://blog.csdn.net/yeweiouyang/article/details/12075039
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热血枫叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值