cocos2d-x滚屏简单实现

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);

}

}


你可能感兴趣的:(cocos2d-x滚屏)