在update中实现地图循环
方法1:
void XXX::update(float dt)
{
CCSprite *bg1 = (CCSprite *)this->getChildByTag(tag_oneImg);
CCSprite *bg2 = (CCSprite *)this->getChildByTag(tag_twoImg);
CCSize size = bg1->getContentSize();
int posY1 = bg1->getPositionY();
int posY2 = bg2->getPositionY();
posY1 -= 2;// 别写成与float的运算
posY2 -= 2;
if (posY1 < -size.height/2)
{
posY1 = size.height*1.5;
posY2 = size.height/2;
}
if (posY2 < -size.height/2)
{
posY2 = size.height*1.5;
posY1 = size.height/2;
}
bg1->setPositionY(posY1);
bg2->setPositionY(posY2);
}
方法2:
void XXX::update(float dt)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCSprite *bg1 = (CCSprite *)this->getChildByTag(tag_oneImg);
CCSize size = bg1->getContentSize();
//当第一张地图超出屏幕外,将其重置坐标,接在当前显示的图片下
if(bg1->getPositionY() <= -bg1->getContentSize().height / 2)
bg1->setPosition(ccp(size.width / 2, size.height * 1.5f - 1));
else
bg1->setPosition(ccpAdd(bg1->getPosition(), ccp(0, -1)));
CCSprite *bg2 = (CCSprite *)this->getChildByTag(tag_twoImg);
if(bg2->getPositionY() <= -bg2->getContentSize().height / 2)
bg2->setPosition(ccp(size.width / 2, size.height * 1.5f - 1));
else
bg2->setPosition(ccpAdd(bg2->getPosition(), ccp(0, -1)));
}