分析:

背景不断地向左滚动

可以用2个背景图片,交替向左移动实现背景素材:


代码:

BackGround.h#include “cocos2d.h”

USING_NS_CC;

class BackGround : public Layer

{

public:

virtual bool init();

CREATE_FUNC(BackGround);

//定义2个背景,实现滚动效果

Sprite *land1;

Sprite *land2;

void landMove(float dt);

};

BackGround.cpp

#include “BackGround.h”

USING_NS_CC;

bool BackGround::init()

{

Vec2 origin = Director::getInstance()->getVisibleOrigin();

Size visibleSize = Director::getInstance()->getVisibleSize();

//定义2个背景

land1 = Sprite::create(“bg.png”);

land1->setPosition(Point(origin.x+land1->getContentSize().width/2,origin.y+land1->getContentSize().height/2));

land1->getTexture()->setAliasTexParameters();

addChild(land1);

land2 = Sprite::create(“bg.png”);

land2->setPosition(Point(origin.x+land1->getPositionX()+land1->getContentSize().width-2,land1->getPositionY()));

land2->getTexture()->setAliasTexParameters();

addChild(land2);

//开启计时器,不断交替左移

schedule(schedule_selector(BackGround::landMove), 1/60);

return true;

}

void BackGround::landMove(float dt)

{

land1->setPositionX(land1->getPositionX()-2);

land2->setPositionX(land1->getPositionX()+land1->getContentSize().width-2);

if (land2->getPositionX()==land2->getContentSize().width/2)

{

land1->setPositionX(land2->getContentSize().width/2);

}

}