cocos2d-x 3.2 创建一个无限滚动的背景

来源:互联网 发布:修改数据导入考勤机 编辑:程序博客网 时间:2024/05/16 09:15

头文件中增加:

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

0 0
原创粉丝点击