您的位置:首页 > Web前端 > Node.js

ClippingNode实现跑马灯文字(例如游戏公告等)

2015-06-25 15:01 579 查看
<1>Marquee.h

#ifndef __JNTest__Marquee__
#define __JNTest__Marquee__

#include "cocos2d.h"
USING_NS_CC;
#include <string>
using namespace std;

typedef enum Marquee_State
{
Marquee_State_Idle, //空闲状态
Marquee_State_Playing //播放状态
}Marquee_State;

class Marquee : public Node
{
public:
Marquee();
~Marquee();
bool init();
CREATE_FUNC(Marquee);
public:
void addMessage(const string & text);
void show(const string& text);
public:
CC_SYNTHESIZE(string, _font, Font);
CC_SYNTHESIZE(float, _fontSize, FontSize);
CC_SYNTHESIZE(Rect, _showRect, ShowRect);
CC_SYNTHESIZE_READONLY(Marquee_State, _state, State);
private:
Label* _label;
queue<string> _texts;
};

#endif /* defined(__JNTest__Marquee__) */<2>Marquee.cpp
#include "Marquee.h"

Marquee::Marquee():_font(""), _fontSize(24), _showRect(0, 0, 200, 30), _state(Marquee_State_Idle){

}

Marquee::~Marquee(){

}

bool Marquee::init(){

//1,模板
auto stencil = Sprite::create();
stencil->setTextureRect(_showRect);

//跑马灯文字内容
_label = Label::createWithSystemFont("", _font, _fontSize);
_label->setAnchorPoint(Vec2::ANCHOR_MIDDLE_LEFT); // 0, 0.5
_label->setAlignment(TextHAlignment::LEFT);

//2,裁剪节点
auto clippingNode = ClippingNode::create(stencil);
clippingNode->setInverted(false);
clippingNode->addChild(_label);

//3,底板
this->addChild(clippingNode);

//
stencil->setColor(Color3B::BLACK);
this->addChild(stencil, -1);

return true;
}

void Marquee::addMessage(const string & text){
if(text.empty()){
return;
}

if(_state == Marquee_State_Idle){
show(text);
}else{
_texts.push(text);
}
}

void Marquee::show(const string& text){
_state = Marquee_State_Playing;
_label->setString(text);
_label->setPosition(Vec2::ZERO);

auto sequ = Sequence::create(
Show::create(),
MoveBy::create(5.0f, Vec2(-(_label->getContentSize().width + _showRect.size.width/2), 0)),
Hide::create(),
DelayTime::create(1.0f),
CallFunc::create([&](){
if(_texts.size() == 0){
_state = Marquee_State_Idle;
}else{
show(_texts.front());
_texts.pop();
}
}),
NULL
);
_label->runAction(sequ);
}
<3>GameScene.cpp

bool GameScene::init(){
if(!Layer::init()){
return false;
}

Size winSize = Director::getInstance()->getWinSize();

Marquee* marquee = Marquee::create();
marquee->addMessage("恭喜贾楠同学在欢乐场抽取到iPhone 6 plus一台!");
marquee->addMessage("恭喜李博同学在比赛场赢得10000金币!");
marquee->addMessage("恭喜陈新同学在高级场获得大满贯!");
marquee->setPosition(Vec2(winSize.width/2, winSize.height/2));
this->addChild(marquee);

return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: