Cocos2d-x教程(29)-3.x版本遮罩层实现捕鱼达人滚动数字表盘

这篇教程介绍了如何在Cocos2d-x 3.x版本中使用遮罩层实现类似捕鱼达人滚动数字表盘的效果。通过创建ScrollLabel类,利用节点和动作进行滚动动画,并通过GL_SCISSOR_TEST实现遮罩效果。
摘要由CSDN通过智能技术生成

欢迎加入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));//将锚点设为00便于设置坐标

        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);

最后附上效果图一张:


评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值