.h
#pragma once
#include "cocos2d.h"
#include <iostream>
#include <stdio.h>
USING_NS_CC;
using namespace std;
class FlowWorld :public Node
{
protected:
Node* _holder;
Sprite* _newsBg;
Label* _news;
DrawNode* _shap;
ClippingNode* _cliper;
public:
static FlowWorld* fWorld;
static FlowWorld* getIntance();
FlowWorld* createFW(Sprite* sp);
FlowWorld();
~FlowWorld();
void scrollText(float);
CREATE_FUNC(FlowWorld);
};
2..cpp
#include "FlowWorld.h"
#include "ScriptData.h"
#include "utill/SXMJUtill.h"
FlowWorld::FlowWorld()
{
_holder = NULL;
_newsBg = NULL;
_news = NULL;
_shap = NULL;
_cliper = NULL;
}
FlowWorld::~FlowWorld() {}
FlowWorld* FlowWorld::fWorld = NULL;
FlowWorld* FlowWorld::getIntance(){
if (!fWorld)
{
fWorld = new FlowWorld();
}
return fWorld;
}
FlowWorld* FlowWorld::createFW(Sprite* sp){
Point p = Point(130.0f, 0.0f);
float w = 600.0f;
float h = 80.0f;
auto shap = DrawNode::create();
shap->clear();
//标记显示的矩形区域
Point point[4] = { Point(p.x, p.y), Point(p.x + w, p.y), Point(p.x + w, p.y + h), Point(p.x, h) };//标记显示的矩形区域
shap->drawPolygon(point, 4, Color4F(355, 255, 255, 255), 2, Color4F(255, 255, 255, 255));
shap->setColor(Color3B::RED);//剪切用于显示的矩形区域,参数对应;用于标识区域的Point(Vec2)数组指针,数组长度,填充颜色,边框宽度,边框颜色。
auto cliper = ClippingNode::create();
cliper->setStencil(shap);
cliper->setAnchorPoint(Point(0.5, 0.5));
vector<string>str;
str.push_back(ScriptData<string>("Message1").Value());
str.push_back(ScriptData<string>("Message2").Value());
str.push_back(ScriptData<string>("Message3").Value());
str.push_back(ScriptData<string>("Message1").Value());
str.push_back(ScriptData<string>("Message2").Value());
str.push_back(ScriptData<string>("Message3").Value());
str.push_back(ScriptData<string>("Message1").Value());
_news = Label::create(SXMJUtill::getIntance()->vectorToString(str), "Microsoft Yahei", 24);
_news->setColor(Color3B::YELLOW);
_news->setPosition(Point(p.x + w - 10, 37));
_news->setAnchorPoint(Point::ZERO);
cliper->addChild(_news);
cliper->addChild(shap);
sp->addChild(cliper);
//layout();
schedule(schedule_selector(FlowWorld::scrollText));//实现公告文字滚动
return this;
}
void FlowWorld::scrollText(float)
{
_news->getPosition().x < (-1 * _news->getContentSize().width) ? _news->setPositionX(720) : _news->setPositionX(_news->getPositionX() - 2);
}