1、在图层中加入两个精灵
CCSprite *pSp1 = CCSprite::create("bg01.jpg");
CCSprite *pSp2 = CCSprite::create("bg01.jpg");
pSp1->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height/2));
pSp2->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height/2+576));//背景图片高为576
this->addChild(pSp1,0,11);
this->addChild(pSp2,0,12);
2、启动滚屏定时器
this->schedule(schedule_selector(GamingLayer::BackgroundScrollLogic),0.1f);
3、滚屏回调函数实现
void GamingLayer::BackgroundScrollLogic(float t)
{
CCSprite *pBg1 = (CCSprite *)getChildByTag(11);
CCSprite *pBg2 = (CCSprite *)getChildByTag(12);
pBg1->setPositionY(pBg1->getPositionY() - 10);//每次移动10个像素
pBg2->setPositionY(pBg1->getPositionY() + 576);//精灵2跟着移动
//如果第二张图片滚动至末尾,重置精灵1的为原始位置
if(pBg2->getPositionY() <= CCDirector::sharedDirector()->getWinSize().height/2)
{
pBg1->setPositionY(CCDirector::sharedDirector()->getWinSize().height/2);
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。