标签:遮罩层 cocos2d-x 3.0遮罩 捕鱼达人滚动表盘 捕鱼达人 马宗扬
欢迎加入Cocos2d-x 交流群:193411763
转载时请注明原文出处 : http://blog.csdn.net/u012945598/article/details/38340845
源码下载地址:http://download.csdn.net/detail/u012945598/7704725
之前在第八篇教程中讲解了遮罩层实现捕鱼达人滚动数字表盘(文章链接:http://blog.csdn.net/u012945598/article/details/17049419),后来有很多朋友问3.x版本中遮罩无效,导致方法不能使用,其实方法还是能用的,只是代码需要略加修改,笔者这里重新贴出ScrollLabel类的实现。各位可直接copy替换即可。
ScrollLabel.h文件
#include <iostream>
#include "cocos2d.h"
#include <stdlib.h>
USING_NS_CC;
class ScrollLabel :publicNode{
public:
virtual bool init();
CREATE_FUNC(ScrollLabel);
std::vector<Label *> labels;/* 用来保存label的数组 */
Node * visibleNode; /* 创建一个节点,将label添加到节点上,便于移动 */
void setNumber(int var ,bool up); /*设置滚动到哪个数字的方法,参数var是滚动到的数字 */
/* ----- 修改内容 ------- */
/* 重载CCNode中的visit方法,此处不使用draw方法 */
virtual void visit(Renderer* renderer,constkmMat4 &parentTransform, bool parentTransformUpdated);
void onAfterVisitScissor();
void onBeforeVisitScissor();
public:
CustomCommand _beforeVisitCmdScissor;
CustomCommand _afterVisitCmdScissor;
};
ScrollLabel.cpp 文件
#include "ScrollLabel.h"
bool ScrollLabel::init(){
visibleNode=Node::create();
this->addChild(visibleNode);
/*创建十个标签,数字0-9 */
for(int i=0;i<10;i++){
char str[2];
str[0]=‘0‘+i;
str[1]=‘\0‘;
Label * single=Label::create();
single->setSystemFontSize(20.0);
single->setColor(Color3B(0,0,0));//设置标签字体颜色
single->setTag(i);
single->setString(str);
single->setAnchorPoint(Point(0,0));//将锚点设为0,0便于设置坐标
single->setPosition(Point(0,20 *i));
visibleNode->addChild(single);
labels.push_back(single); //将标签存入数组中
}
return true;
}
void ScrollLabel::setNumber(int var,bool up =true){
this->stopAllActions();
/*获取到要移动到的label的坐标*/
Label * label=labels[var];
Point moveToPosition =Point(visibleNode->getPositionX(),-(label->getPositionY()));
/*创建一个动作,移动到该label的位置*/
MoveTo * moveAction =MoveTo::create(5, moveToPosition);
visibleNode->runAction(moveAction);
}
void ScrollLabel::visit(Renderer* renderer,constkmMat4 &parentTransform, bool parentTransformUpdated){
_beforeVisitCmdScissor.init(_globalZOrder);
_beforeVisitCmdScissor.func =CC_CALLBACK_0(ScrollLabel::onBeforeVisitScissor,this);
renderer->addCommand(&_beforeVisitCmdScissor);
Node::visit(renderer, parentTransform, parentTransformUpdated);
_afterVisitCmdScissor.init(_globalZOrder);
_afterVisitCmdScissor.func =CC_CALLBACK_0(ScrollLabel::onAfterVisitScissor,this);
renderer->addCommand(&_afterVisitCmdScissor);
}
void ScrollLabel::onBeforeVisitScissor()
{
Point pos=Point(0,0);
pos=visibleNode->getParent()->convertToWorldSpace(pos);
Rect clippingRect =Rect(pos.x, pos.y,20,20);
glEnable(GL_SCISSOR_TEST);
auto glview =Director::getInstance()->getOpenGLView();
glview->setScissorInPoints(clippingRect.origin.x, clippingRect.origin.y, clippingRect.size.width, clippingRect.size.height);
}
void ScrollLabel::onAfterVisitScissor()
{
glDisable(GL_SCISSOR_TEST);
}
使用方法和以前相同:
ScrollLabel * m_scroll=ScrollLabel::create();
m_scroll->setPosition(Point(568,320));
this->addChild(m_scroll);
m_scroll->setNumber(5,true);
最后附上效果图一张:
Cocos2d-x教程(29)-3.x版本遮罩层实现捕鱼达人滚动数字表盘,布布扣,bubuko.com
Cocos2d-x教程(29)-3.x版本遮罩层实现捕鱼达人滚动数字表盘
标签:遮罩层 cocos2d-x 3.0遮罩 捕鱼达人滚动表盘 捕鱼达人 马宗扬
原文地址:http://blog.csdn.net/u012945598/article/details/38340845