zoukankan      html  css  js  c++  java
  • Cocos2d-x中屏幕截取

    类似半屏幕文字向上滚动,到一定位置,逐渐消失

    这里用到了CCLayer的visit()方法

    首先新建一个类TxtLayer  继承CCLayer

    class TxtLayer : public cocos2d::CCLayer{
        
    public:
        TxtLayer();
        ~TxtLayer();
        virtual void visit(void);
    };
    

     重写visit方法

    .cpp实现

    TxtLayer::TxtLayer()
    {
        
    }
    
    TxtLayer::~TxtLayer()
    {
        
    }
    
    // visit()函数在每帧时调用    
    void TxtLayer::visit()
    {
        glEnable(GL_SCISSOR_TEST);              // 开启显示指定区域
    
        float n_width = this->getContentSize().width;//文字显示长度
        float n_height = this->getContentSize().height;//文字显示宽度
        cocos2d::CCDirector::sharedDirector()->getOpenGLView()->setScissorInPoints(0, 0, n_width,n_height);
    
        CCLayer::visit();                       // 调用下面的方法
        glDisable(GL_SCISSOR_TEST);             // 禁用
    }
    

     在自己的层里初始化该类

        CCLabelTTF *txt = CCLabelTTF::create("Hello 小编", "", 7*4);
        txt->setColor(Utils::strToColor("fcf8b4"));
        txt->setPosition(ccp(10,-200));
        txt->setHorizontalAlignment(kCCTextAlignmentLeft);
        txt->setDimensions(CCSizeMake(size.width-100, 400));
    
        TxtLayer *txtLayer = new TxtLayer();
        txtLayer->setContentSize(CCSize(size.width, size.height/2+70));
        txtLayer->setPosition(ccp(size.width/2, 0));
        txtLayer->addChild(txt);
        this->addChild(txtLayer);
    

     为文字设置移动动作   13s内向上滚动至屏幕顶  由于文字加在了自定义的截屏层里  所有只显示截屏层长宽 故可以实现截屏效果

    CCSequence *seq = CCSequence::create(CCMoveTo::create(13, CCPointMake(txt->getPositionX(), size.height)),CCCallFunc::create(this, callfunc_selector(StoryFull::callBack)),NULL);
    txt->runAction(seq);
    

    // 另一种方法  但不推荐使用

    void TxtLayer::visit()
    {
        CCLayer::visit();
        return;
        glEnable(GL_SCISSOR_TEST);              // 开启显示指定区域
    
        float x = 0;
        float y = 0;
        float n_width = this->getContentSize().width;
        float n_height = this->getContentSize().height;
    
        
        glScissor(x, y, n_width, n_height);     // 只显示当前窗口的区域
        CCLayer::visit();                       // 调用下面的方法
        glDisable(GL_SCISSOR_TEST);             // 禁用
    }
    

     这样同样可以实现截屏效果

    glScissor(x, y, n_width, n_height); 
    

     如果只针对一种机型还可以,考虑到后期适配问题,如果这样写的话

    n_width和
    n_height 都要乘上对应机型的缩放比列
    所以推荐第一种方法。
  • 相关阅读:
    1105 Spiral Matrix (25分)(蛇形填数)
    1104 Sum of Number Segments (20分)(long double)
    1026 Table Tennis (30分)(模拟)
    1091 Acute Stroke (30分)(bfs,连通块个数统计)
    1095 Cars on Campus (30分)(排序)
    1098 Insertion or Heap Sort (25分)(堆排序和插入排序)
    堆以及堆排序详解
    1089 Insert or Merge (25分)
    1088 Rational Arithmetic (20分)(模拟)
    1086 Tree Traversals Again (25分)(树的重构与遍历)
  • 原文地址:https://www.cnblogs.com/cocos2dx-wk/p/3399753.html
Copyright © 2011-2022 走看看