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

news/2025/2/23 17:00:55

        採用双层背景。这样效果更好


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

}




转载于:https://www.cnblogs.com/yutingliuyl/p/7110744.html


http://www.niftyadmin.cn/n/712401.html

相关文章

学习进度十三

所花时间&#xff1a;1.5小时 代码量&#xff1a;大约200行 博客量&#xff1a;2篇 了解到的知识点&#xff1a;javascript的验证功能&#xff0c;正则表达式的应用等转载于:https://www.cnblogs.com/jbwen/p/11071574.html

php 学习路线 赵兴壮2014年4月28 日 加油

第一阶段 第一讲&#xff0c;WEB基础 1.1 网站基本知识&#xff1b; 1.2 网络协议介绍&#xff1b; 1.3 B/S与C/S结构的区别&#xff1b; 1.4 WEB编程、网站开发技术介绍。 第二讲&#xff0c;网页设计 2.1 Dreamweaver介绍及使用&#xff1b; 2.2 静态网页HTML语…

视高视频会议助力亿群矿业建立集团信息化

亿群矿业集团是集采矿、加工、物流、贸易于一体的一家集团公司&#xff0c;至今集团已走过了15个春秋。集团下属有十几个子公司&#xff0c;还有分部在全国各地的销售部门&#xff0c;集团公司现配有完善的物流保障系统&#xff0c;按照ISO9001&#xff1a;2000国际质量管理体系…

web前端学习(三)——HTML5的字体、特殊符号、插入图片及头部元素的相关标签设置

1.HTML5中字体的相关标签设置 ① <em>...</em> 标签&#xff1a;将其中的内容转换为斜体。&#xff08;类似于 <i>...</i>&#xff09; ② <strong>...</strong> 标签&#xff1a;将其中的内容进行加粗。 &#xff08;类似于 <b>...…

python加载模型进行测试代码实现_手把手教你使用Python实战反欺诈模型|原理+代码...

作者 | 萝卜本文将基于不平衡数据&#xff0c;使用Python进行反欺诈模型数据分析实战&#xff0c;模拟分类预测模型中因变量分类出现不平衡时该如何解决&#xff0c;具体的案例应用场景除反欺诈外&#xff0c;还有客户违约和疾病检测等。只要是因变量中各分类占比悬殊&#xff…

web前端学习(四)——HTML5的超链接标签设置(页面间链接、锚链接以及邮件链接)

1.HTML5的超链接——页面间链接 <a href "链接网址" target“目标窗口位置”>文本或图像</a> 这里的 href代表的是链接路径&#xff08;#代表空链接&#xff09;。 target后面跟的如果是 -self&#xff0c;则打开自身窗口&#xff1b;如果是 -blank&…

python实例方法不可以用类调用_Python-为什么我可以用实例调用类方法?

刚接触Python并阅读了一些内容&#xff0c;我正在自定义类类方法中创建一些方法&#xff0c;而不是实例方法。所以我测试了我的代码&#xff0c;但是我没有改变一些方法调用来调用类中的方法而不是实例&#xff0c;但是它们仍然有效&#xff1a;class myClass:classmethod:def …

printf打印一段内存_成为高手必须弄懂的问题,C++语言中类在内存中是如何存储的?...

本文转自&#xff1a;头条号IT刘小虎C/C语言特别适合用于开发压榨机器性能的程序&#xff0c;在资源有限的情况下&#xff0c;C/C程序员不能放过机器的每一点计算力以及每一字节内存。当然了&#xff0c;要做到这一点&#xff0c;编程语言本身要提供精准的控制能力&#xff0c;…