cocos2dx实现背景的无限滚动

前端之家收集整理的这篇文章主要介绍了cocos2dx实现背景的无限滚动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

滚动的游戏背景在射击类游戏中比较常见(如:雷霆战机),下面用cocos2dx实现背景的无限滚动(基于引擎cocos2dx2.1.4)

<span style="font-family:KaiTi_GB2312;font-size:14px;">void MainScene::update(float delta){

	//创建滚动的主游戏背景
	CCSize spsize = m_bgsprite1->getContentSize();
	//CCSize spsize = CCSizeMake(854,480);
	int m1 = m_bgsprite1->getPositionX();
	int m2 = m_bgsprite2->getPositionX();
	int xspeed = 1;
	m1 -= xspeed;
	m2 -= xspeed;
	if(m1 < -spsize.width/2){
		m1 = spsize.width+spsize.width/2;
		m2 = spsize.width/2;
	}
	if(m2 < -spsize.width/2){
		m2 = spsize.width+spsize.width/2;
		m1 = spsize.width/2;
	}
	m_bgsprite1->setPositionX(m1);
	m_bgsprite2->setPositionX(m2);

	//将主角设置为可移动对象
	CCPoint curpos = this->getChildByTag(1)->getPosition();
	this->getChildByTag(1)->setPosition(ccp(curpos.x+xSpeed,curpos.y+ySpeed));

	
}</span>

xspeed为背景滚动速度,m_bgsprite1为背景图片

原文链接:https://www.f2er.com/cocos2dx/343388.html

猜你在找的Cocos2d-x相关文章