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;
}
#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;
}
相关文章推荐
- node.js导出excel ,csv
- LintCode Remove Nth Node From End of List 删除链表中倒数第n个节点
- Nodepad++ 快捷键
- 写给Node.js新手的7个小技巧
- Count Complete Tree Nodes ——LeetCode
- nodejs学习篇
- kafka I wrote this conflicted ephemeral node
- [NodeJS] 优缺点及适用场景讨论
- 开始刷leetcode day49: Populating Next Right Pointers in Each Node
- Count Complete Tree Nodes
- node-webkit 相关使用心得 --系统托盘(tray)的使用方法
- nodejs笔记1:问题杂记
- Node.js中的process.nextTick使用实例
- [转载]七天学会NodeJS
- [leetcode] Swap Nodes in Pairs
- Node.js中的process.nextTick使用实例
- 把Node.js程序加入服务实现随机启动
- nodejs实现获取当前url地址及url各种参数值
- Reverse Nodes in k-Group 指针操作 每k个翻转链表
- LeetCode 116. Populating Next Right Pointers in Each Node