Cocos2d-x中屏幕截取
阅读原文时间:2023年07月15日阅读:1

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

这里用到了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 都要乘上对应机型的缩放比列
所以推荐第一种方法。

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章