加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何实现字幕效果,cocos2dx ,Lua

发布时间:2020-12-14 16:50:25 所属栏目:百科 来源:网络整理
导读:实现这个字幕效果,其实很简单,只需要画一个遮罩即可完成,带遮罩内部显示,外部隐藏,如下有C++,lua两个版本的代码: local GG = self._Panel:getChildByName("notification_bg")local GGW = GG:getContentSize().width local GGH = GG:getContentSize().h

实现这个字幕效果,其实很简单,只需要画一个遮罩即可完成,带遮罩内部显示,外部隐藏,如下有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画出遮罩的模版出来就可以了、。调试模版的形状看个人需求去调试即可、

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读