1. 程式人生 > >cocos2d-x 3.2 建立一個無限滾動的背景

cocos2d-x 3.2 建立一個無限滾動的背景

標頭檔案中增加:

void backgroundMove(float dt);
cocos2d::Sprite* background1;
cocos2d::Sprite* background2;


init函式中增加:

background1 = Sprite::create("background.png");
 background2 = Sprite::create("background.png");
 background1->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
 background2->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
 background1->setPosition(Point::ZERO);
 
 this->addChild(background1,0);
 this->addChild(background2,0);
 
 this->schedule(schedule_selector(Welcome::backgroundMove),0.01f);

實現backgroundMove函式

void Welcome::backgroundMove(float dt)
{
 background1->setPositionX(background1->getPositionX()-2.0f);
 background2->setPositionX(background1->getPositionX() + background1->getContentSize().width-2.0f);
 if(background2->getPositionX()<=0)
 {
  background1->setPositionX(0);
 }
}