如何實現字幕效果,cocos2dx ,Lua

實現這個字幕效果,其實很簡單,只需要畫一個遮罩即可完成,帶遮罩內部顯示,外部隱藏,如下有C++,lua兩個版本的代碼:


function GameClientView:updateAdvertisement()
	-- body
	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 voice_icon = GG:getChildByName("voice_icon") --吶吧

    local text = cc.Label:createWithTTF("首次充值將會活動額外獎勵", "fonts/fnt_forestparty.TTF", 20)
    text:setPosition(cc.p(DesX + DesX / 2, GGH / 2))

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

	-- --以下模型是帶圖像遮罩
	local bgSharp = cc.DrawNode:create()
    local beginX = voice_icon:getContentSize().width
    local Point = {
        [1] = cc.p(beginX / 2, 0),
        [2] = cc.p(GGW - beginX / 2, 0),
        [3] = cc.p(GGW - beginX / 2, GGH),
        [4] = cc.p(beginX / 2, GGH),
    }
	bgSharp:drawPolygon(Point, 4, cc.c4f(1, 0, 0, 1), 2, cc.c4f(0, 1, 0, 1))
	clip:setStencil(bgSharp)
	clip:setAnchorPoint(cc.p(0.5, 0.5))
    clip:setPosition(beginX / 2, 0)
	clip:addChild(text)

	local function displayAdvise()
		-- body
		if text:getPositionX() <  0 then
			--todo
            text:setPosition(cc.p(DesX + DesX / 2, GGH / 2))
		else
			text:setPositionX(text:getPositionX() - 10)
		end
	end
    self._adviseTimerHandle = cc.Director:getInstance():getScheduler():scheduleScriptFunc(displayAdvise, 0.1, false) 
end


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, 60), ccp(200, 200), ccp(80, 200) };

	shap->drawPolygon(point, 4, ccc4f(355, 255, 255, 255), 2, ccc4f(255, 255, 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畫出遮罩的模版出來就可以了、。調試模版的形狀看個人需求去調試即可、

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章