相似半屏幕文字向上滾動,到必定位置,逐漸消失函數
這裏用到了CCLayer的visit()方法this
首先新建一個類TxtLayer 繼承CCLayerblog
class TxtLayer : public cocos2d::CCLayer{ public: TxtLayer(); ~TxtLayer(); virtual void visit(void); };
重寫visit方法繼承
.cpp實現ci
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); // 禁用 }
在本身的層裏初始化該類get
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內向上滾動至屏幕頂 因爲文字加在了自定義的截屏層裏 全部只顯示截屏層長寬 故能夠實現截屏效果it
CCSequence *seq = CCSequence::create(CCMoveTo::create(13, CCPointMake(txt->getPositionX(), size.height)),CCCallFunc::create(this, callfunc_selector(StoryFull::callBack)),NULL); txt->runAction(seq);
// 另外一種方法 但不推薦使用io
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); // 禁用 }
這樣一樣能夠實現截屏效果class
glScissor(x, y, n_width, n_height);
若是隻針對一種機型還能夠,考慮到後期適配問題,若是這樣寫的話select
n_width和
n_height 都要乘上對應機型的縮放比列因此推薦第一種方法。