cocos2d-x利用CCClippingNode实现滚动字幕

直接上代码,这是封装好了的layer,可以直接调用  //白白原创

bool TestLayer::init()
{
    CCSize size = CCDirector::sharedDirector()->getVisibleSize();
      
	//创建要显示的文字
    text = CCLabelTTF::create("text\nbaibai\nfdffdd\nzimu\nhaha\njahjahjkd\njhahaj\n", "", 30);
    text->setPosition(ccp(100, -120));
    
	//绘制裁剪区域
    CCDrawNode* shap = CCDrawNode::create();
    CCPoint point[4] = {ccp(0,0), ccp(200, 0), ccp(200, 200), ccp(0, 200)};
    shap->drawPolygon(point, 4, ccc4f(355, 255, 255, 255), 2, ccc4f(255, 255, 255, 255));
    CCClippingNode* cliper = CCClippingNode::create();
    cliper->setStencil(shap);
    cliper->setAnchorPoint(ccp(.5, .5));
    cliper->setPosition(ccp(100, 20));
    addChild(cliper);
	//把要滚动的文字加入到裁剪区域
    cliper->addChild(text);
    
	//文字滚动,超出范围后从新开始
    schedule(schedule_selector(CTestLayer::rollText));
	return true;
}

void TestLayer::rollText(float)
{
    text->getPositionY()>300 ? text->setPositionY(-150) : text->setPositionY(text->getPositionY()+2);
}


你可能感兴趣的:(cocos2dx)