【cocos2d-x 3.7 飞机大战】 决战南海I (八) 背景移动

        采用双层背景,这样效果更好


.h

class BackgroundMove : public Layer
{
public:
	BackgroundMove();
	~BackgroundMove();
	virtual bool init();
	virtual void onEnterTransitionDidFinish();	//等进入场景之后在进行背景的移动
	CREATE_FUNC(BackgroundMove);

public:
	void move(float dt);
private:
	Sprite* m_background1;
	Sprite* m_background2;
	Sprite* m_background3;
	Sprite* m_background4;

	enum 
	{
		OFFSET = 3
	};
};


背景无限滚动的方式有很多,只要不出现黑边即可

.cpp

BackgroundMove::BackgroundMove() : m_background1(NULL), m_background2(NULL), m_background3(NULL), m_background4(NULL)
{

}
BackgroundMove::~BackgroundMove()
{
	CC_SAFE_DELETE(m_background1);
	CC_SAFE_DELETE(m_background2);
	CC_SAFE_DELETE(m_background3);
	CC_SAFE_DELETE(m_background4);
}
bool BackgroundMove::init()
{
	bool bRect = false;

	do 
	{
		if (!Layer::init())
			return false;

		//加载背景图片
		m_background1 = Sprite::createWithSpriteFrameName("backgroundTollgate2.png");
		m_background1->setPosition(Point(0,0));
		m_background1->setAnchorPoint(Vec2(0, 0));

		this->addChild(m_background1,1);

		m_background2 = Sprite::createWithSpriteFrameName("backgroundTollgate2.png");
		m_background2->setPosition(Point(0, 0));
		m_background2->setAnchorPoint(Vec2(0, 0));
		m_background2->setFlipY(true);

		this->addChild(m_background2,1);

		//加载背景图片
		m_background3 = Sprite::createWithSpriteFrameName("backgroundTollgateThree.png");
		m_background3->setPosition(Point(0, 0));
		m_background3->setAnchorPoint(Vec2(0, 0));

		this->addChild(m_background3, 0);

		m_background4 = Sprite::createWithSpriteFrameName("backgroundTollgateThree.png");
		m_background4->setPosition(Point(0, 0));
		m_background4->setAnchorPoint(Vec2(0, 0));
		m_background4->setFlipY(true);

		this->addChild(m_background4, 0);

		CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/BackgroundMusic.mp3", true);

		bRect = true;
	} while (0);

	return bRect;
}
void BackgroundMove::onEnterTransitionDidFinish()
{
	Layer::onEnterTransitionDidFinish();
	this->schedule(SEL_SCHEDULE(&BackgroundMove::move), 0.01f);
}

void BackgroundMove::move(float dt)
{
	Vec2 origin = Director::getInstance()->getVisibleOrigin();

	m_background1->setPositionY(m_background1->getPositionY() - OFFSET);
	m_background2->setPositionY(m_background1->getPositionY() + m_background1->getContentSize().height);
	if (m_background2->getPositionY() <= origin.y)
		m_background1->setPositionY(0);

	m_background3->setPositionY(m_background3->getPositionY() + OFFSET);
	m_background4->setPositionY(m_background3->getPositionY() - m_background3->getContentSize().height);
	if (m_background4->getPositionY() >= origin.y)
		m_background3->setPositionY(0);

}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值