Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何实现字幕效果,cocos2dx ,Lua大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

实现这个字幕效果,其实很简单,只需要画一个遮罩即可完成,带遮罩内部显示,外部隐藏,如下有C++,lua两个版本的代码:

local GG  = self._Panel:getChildByName("notification_bg")
	local GGW = GG:getContentSize().width 
	local GGH = GG:getContentSize().height
	local OrgX = GG:getPositionX() - GGW / 2
	local OrgY = GG:getPositionY() - GGH / 2 - 10
	local DesX = GG:getPositionX() + GGW / 2 - 70
	local DesY = GG:getPositionY() + GGH / 2

	local text = cc.Label:createWithTTF("首次充值将会活动额外奖励","res/font.TTF",20)
    text:setPosition(cc.p(GG:getPositionX(),GGH / 2))

	local clip = cc.ClippingNode:create()
	self:addChild(clip)

	-- --以下模型是带图像遮罩
	local bgSharp = cc.DrawNode:create()
	local Point = {
		[1] = cc.p(OrgX,OrgY),[2] = cc.p(DesX,[3] = cc.p(DesX,DesY),[4] = cc.p(OrgX,}
	bgSharp:drawPolygon(Point,4,cc.c4f(1,1,1),2,1))
	clip:setStencil(bgSharp)
	clip:setAnchorPoint(cc.p(0.5,0.5))
	clip:setPosition(cc.p(OrgX - GGW / 2 + 20,GGH / 2))
	clip:addChild(text)

	local function displayAdvise()
		-- body
		if text:getPositionX() <  0 then
			--todo
			text:setPosition(cc.p(DesX,GG:getPositionY() - 20))
		else
			text:setPositionX(text:getPositionX() - 10)
		end
	end
	self._timerHandle = cc.Director:geTinstance():getscheduler():scheduleScriptFunc(displayAdvise,0.1,falsE) 


C++版本
bool HelloWorld::init()
{

	CCSize size = CCDirector::sharedDirector()->getVisibleSize();

	//创建要显示的文字

	text = CCLabelTTF::create("ddddddddddddddddddddddddddddddddddddddddd","",30);

	text->setPosition(ccp(20,80));
	//this->addChild(text);

	//绘制裁剪区域

	CCDrawNode* shap = CCDrawNode::create();

	CCPoint point[4] = { ccp(80,60),ccp(200,200),ccp(80,200) };

	shap->drawPolygon(point,ccc4f(355,255,255),ccc4f(255,255));

	CCClippingNode* cliper = CCClippingNode::create();

	cliper->setStencil(shap);

	cliper->setAnchorPoint(ccp(.5,.5));

	cliper->setPosition(ccp(120,80));

	addChild(cliper);

	//把要滚动的文字加入到裁剪区域

	cliper->addChild(text);

	//文字滚动,超出范围后从新开始

	this->schedule(schedule_SELEctor(HelloWorld::updateMovE),0.1);
    return true;
}

void HelloWorld::updateMove(float f)
{	
	if (text->getPositionX() > 120)
	{
		text->setPositionX(20);
	}
	text->setPositionX(text->getPositionX() + 10);
}
注:利用DrawNode画出遮罩的模版出来就可以了、。调试模版的形状看个人需求去调试即可、

大佬总结

以上是大佬教程为你收集整理的如何实现字幕效果,cocos2dx ,Lua全部内容,希望文章能够帮你解决如何实现字幕效果,cocos2dx ,Lua所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。