本文介紹游戲中常見(jiàn)的滾動(dòng)播放的公告欄實(shí)現(xiàn)
要點(diǎn)
- 文字內(nèi)容橫向或者縱向滾動(dòng)
- 文字只在區(qū)域內(nèi)顯示拔创,超出區(qū)域部分不顯示
相關(guān)頭文件
** "2d/CCClippingNode.h" **
注意:在cocos2dx 3.x版本中洽沟,方法名中的CC命名方式已經(jīng)棄用,但是文件名中的CC依然有,所以此處的頭文件名中的CC打頭無(wú)法去除
實(shí)現(xiàn)
頭文件
#pragma once
#include "cocos2d.h"
#include "2d/CCClippingNode.h"
USING_NS_CC;
class PannelNews :public Node
{
protected:
Node* _holder;
Sprite* _newsBg;
Label* _news;
DrawNode* _shap;
ClippingNode* _cliper;
public:
PannelNews();
~PannelNews();
virtual bool init() override;
void layout();
void scrollText(float);
CREATE_FUNC(PannelNews);
};
源文件
#include "PannelNews.h"
PannelNews::PannelNews()
{
_holder = NULL;
_newsBg = NULL;
_news = NULL;
_shap = NULL;
_cliper = NULL;
}
PannelNews::~PannelNews() {}
bool PannelNews::init()
{
if (!Node::init())
return false;
setPosition(Point(640.f, 585.f));
_holder = Node::create();
addChild(_holder);
_newsBg = Sprite::create("newsBg.png");
_holder->addChild(_newsBg);
_shap = DrawNode::create();
Point point[4] = { Point(60.f,0.f), Point(710.f,0.f), Point(710.f,35.f), Point(60.f,35.f)};
//標(biāo)記顯示的矩形區(qū)域
_shap->drawPolygon(point, 4, Color4F(355, 255, 255, 255), 2, Color4F(255, 255, 255, 255));
//剪切用于顯示的矩形區(qū)域憨栽,參數(shù)對(duì)應(yīng):用于標(biāo)識(shí)區(qū)域的Point(Vec2)數(shù)組指針浓领,數(shù)組長(zhǎng)度反镇,填充顏色,邊框?qū)挾人突穑吙蝾伾? _cliper = ClippingNode::create();
_cliper->setStencil(_shap);
_cliper->setAnchorPoint(Point(0.5, 0.5));
_newsBg->addChild(_cliper);
_news = Label::createWithSystemFont(UserDefault::getInstance()->getStringForKey(KEY_LOCAL_NEWS, "公告內(nèi)容"), "Microsoft Yahei", 24);
_news->setColor(Color3B(214, 213, 213));
_cliper->addChild(_news);
layout();
return true;
}
void PannelNews::layout()
{
_news->setAnchorPoint(Point::ZERO);
_news->setPosition(Point(720, 10));//設(shè)置公告文字內(nèi)容的初始位置
schedule(schedule_selector(PannelNews::scrollText));//實(shí)現(xiàn)公告文字滾動(dòng)
}
void PannelNews::scrollText(float)
{
_news->getPosition().x < (-1 * _news->getContentSize().width) ? _news->setPositionX(720) : _news->setPositionX(_news->getPositionX() - 2);
}
- ClippingNode可以用于設(shè)定一個(gè)區(qū)域作為其顯示區(qū)域,其子節(jié)點(diǎn)只可在該區(qū)域內(nèi)才可顯示先匪,超出區(qū)域部分則無(wú)法顯示种吸,可用于公告、通知呀非、聊天框坚俗、區(qū)域彈幕、彈幕游戲的保護(hù)罩等