加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx实现背景的无限滚动

发布时间:2020-12-14 21:06:25 所属栏目:百科 来源:网络整理
导读:滚动的游戏背景在射击类游戏中比较常见(如:雷霆战机),下面用cocos2dx实现背景的无限滚动(基于引擎cocos2dx2.1.4) span style="font-family:KaiTi_GB2312;font-size:14px;"void MainScene::update(float delta){//创建滚动的主游戏背景CCSize spsize = m

滚动的游戏背景在射击类游戏中比较常见(如:雷霆战机),下面用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为背景图片

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读