使用4中方式实现滚动喇叭广播功能

方法一:使用定时器和ScrollView来实现

//定时器启动
schedule(schedule_selector(LoginScene::scheduleNotice), 0.01f);
//停止
unschedule(schedule_selector(LoginScene::scheduleNotice));


//----------喇叭---------------
void LoginScene::scheduleNotice(float t)
{
	Widget* pNotice = static_cast(this->csb->getChildByName("notice"));
	pNotice->setTouchEnabled(false);
	ui::ScrollView* pScroll = static_cast(pNotice->getChildByName("sv_notice"));
	pScroll->setTouchEnabled(false);

	Widget* pWidget = static_cast(pScroll->getChildByName("Panel_txt"));
	pWidget->setTouchEnabled(false);
	//pScroll->setDirection(SCROLLVIEW_DIR_HORIZONTAL);
	Text* pLabel = static_cast(pWidget->getChildByName("txt_notice"));
	int nTextLen = 0;
	if (pLabel){
		nTextLen = pLabel->getString().length();
	}

	float width1 = pWidget->getContentSize().width;
	float x = pWidget->getPositionX();

	static int s_nUpdateTimes = 0;
	bool bSetNotice = false;
	if (nTextLen <= 0 && (s_nUpdateTimes > 300)){
		bSetNotice = true;
	}

	if (x + width1 > 0 && !bSetNotice){
		pWidget->setPositionX(x - 1.5f);
		if (nTextLen <= 0){
			s_nUpdateTimes++;
		}
	}
	else
	{
		if (!strMessage.empty())
		{		
			setNotice(strMessage);
			s_nUpdateTimes = 0;
		}
		else
		{
			if (s_nUpdateTimes > 300){
				s_nUpdateTimes = 0;				
			}
			else
			{
				s_nUpdateTimes += 1;
			}
		}

		setNotice(strMessage);
	}
}

void LoginScene::setNotice(string strNotice)
{
	Widget* pNotice = static_cast(this->csb->getChildByName("notice"));
	ui::ScrollView* pScroll = static_cast(pNotice->getChildByName("sv_notice"));
	CCSize scrollSize = pScroll->getContentSize();

	Widget* pWidget = static_cast(pScroll->getChildByName("Panel_txt"));
	Text* pLabel = static_cast(pWidget->getChildByName("txt_notice"));

	if (pLabel)
	{
		pLabel->setTouchEnabled(false);
		pLabel->setText(strNotice);
		CCSize labelSize = pLabel->getContentSize();
		pScroll->setInnerContainerSize(CCSize(1120, scrollSize.height));
		pWidget->setContentSize(labelSize);
		pWidget->setPosition(Vec2(1120, pWidget->getPosition().y));
	}
}

方法二:使用裁剪节点实现

	//----喇叭2(裁剪节点)----
	laba("this is a clippingNode Test...this is a clippingNode Test...this is a clippingNode Test...");





//----------喇叭2---------------
void LoginScene::laba(const std::string& text)
{
	auto pNotice = static_cast(this->csb->getChildByName("Image_8"));

	Label* txt = Label::create("", "Arial", 32);
	txt->setColor(Color3B::RED);     //裁剪内容
	txt->setString(text);
	txt->setAnchorPoint(Vec2::ZERO);
	txt->setAlignment(TextHAlignment::LEFT);
	txt->setPosition(Vec2(1300, 0));

	ClippingNode* clip = ClippingNode::create();

	Sprite* sp = Sprite::create("hall/hall_notice_bj.png");  //裁剪模板
	//sp->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT);
	sp->setAnchorPoint(Vec2::ZERO);
	clip->setStencil(sp);


	clip->addChild(txt);

	clip->setInverted(false);    //设置裁剪区域可见还是非裁剪区域可见  这里为裁剪区域可见
	clip->setAlphaThreshold(0);
	clip->setPosition(pNotice->getPosition());
	this->addChild(clip);

	//txt->setPositionX(clip->getContentSize().width);

	MoveTo* to2 = MoveTo::create(20, Vec3(-txt->getContentSize().width + 0, 0, 0));
	txt->runAction(Sequence::create(DelayTime::create(0.5f), to2, NULL));
}

方法三:使用定时器和CCLabelTTF来实现

	//----喇叭3----
	initLaba();





//----------喇叭3---------------
void LoginScene::initLaba()
{
	//背景图片
	auto pNotice = static_cast(this->csb->getChildByName("Image_8"));
	Size _s = pNotice->getContentSize();

	//设置文本信息
	CCLabelTTF * text = CCLabelTTF::create("跑马灯,跑马灯,跑马灯,跑马灯,跑马灯!", "Arial", 22);
	text->setColor(ccc3(0, 255, 255));
	text->setTag(9527);
	text->setPosition(ccp(_s.width / 2, _s.height * 4 / 5));
	this->addChild(text, 1, 0);

	//使用定时器,每0.005秒回调一次,产生移动的效果
	this->schedule(schedule_selector(LoginScene::scroll), 0.005f);
}

void LoginScene::scroll(float tm)
{
	auto pNotice = static_cast(this->csb->getChildByName("Image_8"));
	Size _s = pNotice->getContentSize();

	CCLabelTTF * text = (CCLabelTTF *)this->getChildByTag(9527);
	CCPoint point = text->getPosition();
	text->setPosition(ccp(point.x - 1, point.y));

	Size _cs = text->getContentSize();
	float _w = _cs.width;
	if (point.x < -_w / 2)
	{
		text->setPosition(ccp(_s.width + _w / 2, _s.height * 4 / 5));
	}
}

 

方法四:使用ScrollText来实现

	//----喇叭4(ScrollText)----
	initScrollTextNotice();





//----------喇叭4---------------
void LoginScene::initScrollTextNotice()
{
	auto pNotice = static_cast(this->csb->getChildByName("Image_8"));
	Size _s = pNotice->getContentSize();
	
	auto st = ScrollText::create();
	st->setAutoScroll(true);
	st->setContent("this is a clippingNode Test...this is a clippingNode Test...this is a clippingNode Test...");
	st->setSize(Size(300, 40));
	st->setPosition(pNotice->getPosition());
	addChild(st);
}

 

上面的Image_8是UI加载的喇叭背景底图;

实现效果:

使用4中方式实现滚动喇叭广播功能_第1张图片

 

 

 

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