cocos2d-x 3.x之卷轴地图-让背景滚动起来

cocos2d-x 3.x之卷轴地图-让背景滚动起来,第1张

概述卷轴地图是一个可以滚动背景地图,其实现原理就是利用两张交替的图片,让它们按照一个方向同步位移,当一张图片超过窗口界限的时候进行一次位置更替,使它回到另一张图片的后面。通过这样循环的使用这两张图片,背景看起来就在不停的滚动了。 接下来实现一个背景层,把它添加到游戏场景就可以了。 #ifndef __BackgroundLayer_H__#define __BackgroundLayer_H__

卷轴地图是一个可以滚动的背景地图,其实现原理就是利用两张交替的图片,让它们按照一个方向同步位移,当一张图片超过窗口界限的时候进行一次位置更替,使它回到另一张图片的后面。通过这样循环的使用这两张图片,背景看起来就在不停的滚动了。

接下来实现一个背景层,把它添加到游戏场景就可以了。

#ifndef __BackgroundLayer_H__#define __BackgroundLayer_H__#include "cocos2d.h"// 游戏背景层class BackgroundLayer : public cocos2d::Layer{public:	BackgroundLayer(voID);	~BackgroundLayer(voID);	virtual bool init() overrIDe;	CREATE_FUNC(BackgroundLayer);	// 滚动卷轴地图	voID scrollBackground();private:	// 初始化背景地图	voID initBackground();	private:	// 背景地图	cocos2d::Sprite *background_1;	cocos2d::Sprite *background_2;	// 沿Y轴滚动速度	double speedY;};#endif
#include "BackgroundLayer.h"USING_NS_CC;BackgroundLayer::BackgroundLayer(voID){}BackgroundLayer::~BackgroundLayer(voID){}bool BackgroundLayer::init(){	if(!Layer::init())	{		return false;	}	this->initBackground();	return true;}// 初始化背景voID BackgroundLayer::initBackground(){	// 缓存pList文件	SpriteFrameCache::getInstance()->addSpriteFramesWithfile("gameArts-hd.pList","gameArts-hd.png");	// 把两张背景地图加载进来	this->background_1 = Sprite::createWithSpriteFramename("background_2.png");	this->background_1->setAnchorPoint(Vec2(0,0));	this->background_1->setposition(0,0);	this->addChild(this->background_1);	this->background_2 = Sprite::createWithSpriteFramename("background_2.png");	this->background_2->setAnchorPoint(Vec2(0,0));	this->background_2->setposition(0,this->background_1->getpositionY() + this->background_1->getContentSize().height);	this->addChild(this->background_2);	// 设置初始滚动速度	this->speedY = 2;	// 防止背景滚动的时候两张图片衔接部分出现黑边	this->background_1->getTexture()->setAliasTexParameters();}// 滚动背景voID BackgroundLayer::scrollBackground(){	// 计算出地图下次滚动到的Y轴坐标,这里是向下位移	auto nextPos_1 = this->background_1->getpositionY() - this->speedY;	auto nextPos_2 = this->background_2->getpositionY() - this->speedY;	this->background_1->setpositionY(nextPos_1);	this->background_2->setpositionY(nextPos_2);	// 当一张地图移除屏幕边界的时候,重新放置到另一张地图的上面	if(fabs(nextPos_1) == this->background_1->getContentSize().height)	{		this->background_1->setpositionY(this->background_2->getpositionY() + this->background_2->getContentSize().height);		Sprite *t = this->background_1;		this->background_1 = this->background_2;		this->background_2 = t;	}}
  

然后在游戏场景中使用这张卷轴地图

#ifndef __GameScene_H__#define __GameScene_H__#include "cocos2d.h"#include "BackgroundLayer.h"// 游戏主场景class GameScene : public cocos2d::Layer{public:	GameScene(voID);	~GameScene(voID);	static cocos2d::Scene *createScene();	virtual bool init() overrIDe;	CREATE_FUNC(GameScene);private:	// 定时器,每一帧调用	virtual voID update(float delta) overrIDe;	// 此场景加载完毕后的 *** 作	virtual voID onEnterTransitionDIDFinish() overrIDe;private:	// 游戏背景	BackgroundLayer *backgroundLayer;};#endif
#include "GameScene.h"USING_NS_CC;GameScene::GameScene(voID){}GameScene::~GameScene(voID){}Scene *GameScene::createScene(){	auto scene = Scene::create();	auto layer = GameScene::create();	scene->addChild(layer);		return scene;}bool GameScene::init(){	if(!Layer::init())	{		return false;	}	// 加载背景地图	this->backgroundLayer = BackgroundLayer::create();	this->backgroundLayer->setAnchorPoint(Vec2::ZERO);	this->backgroundLayer->setposition(Vec2::ZERO);	this->addChild(backgroundLayer);	return true;}voID GameScene::onEnterTransitionDIDFinish(){	Node::onEnterTransitionDIDFinish();	// 场景加载完毕才滚动背景	this->scheduleUpdate();}voID GameScene::update(float delta){	this->backgroundLayer->scrollBackground();}
总结

以上是内存溢出为你收集整理的cocos2d-x 3.x之卷轴地图-让背景滚动起来全部内容,希望文章能够帮你解决cocos2d-x 3.x之卷轴地图-让背景滚动起来所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1040310.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存