cocos: 无限循环滚动背景

cocos: 无限循环滚动背景,第1张

概述头文件: #pragma once#ifndef _ROLLER_LAYER_H_#define _ROLLER_LAYER_H_#include "cocos2d.h"USING_NS_CC;class RollerLayer :public Layer{public: static const int VERTICAL = 1; static const int HORIZ 头文件:
#pragma once#ifndef _RolLER_LAYER_H_#define _RolLER_LAYER_H_#include "cocos2d.h"USING_NS_CC;class RollerLayer :public Layer{public:	static const int VERTICAL = 1;	static const int HORIZONTAL = 2;	CREATE_FUNC(RollerLayer);	virtual bool init();	voID update(float dt);	voID setBackgrounds(Sprite *map1,Sprite*map2);	voID setRollOrIEntaion(int orIEntation);	voID setSpeed(int iSpeed);private:	voID rollHorizontal();	voID rollVertical();protected :	Sprite *mMap1;	Sprite *mMap2;	int mOrIEntation;	int mSpeed;};#endif


实现
#include "RollerLayer.h"bool RollerLayer::init() {	if (!Layer::init()) {		return false;	}	mOrIEntation = VERTICAL;	mMap1=mMap2 = NulL;	mSpeed = 10;	scheduleUpdate();	return true;}voID RollerLayer::update(float dt) {	if (mMap1 == NulL || mMap2 == NulL) {		return;	}	if (mOrIEntation == HORIZONTAL) {		rollHorizontal();	}	else if (mOrIEntation == VERTICAL) {		rollVertical();	}}voID RollerLayer::rollHorizontal() {	if (mMap1 == NulL || mMap2 == NulL) {		return;	}	int posX1 = mMap1->getpositionX();	int posX2 = mMap2->getpositionX();	posX1 -= mSpeed;	posX2 -= mSpeed;	auto map1Size = mMap1->getContentSize();	auto map2Size = mMap2->getContentSize();	if (posX1 <= -map1Size.wIDth / 2)	{		posX1 = map1Size.wIDth + map2Size.wIDth / 2;	}	if (posX2 <= -map2Size.wIDth / 2) {		posX2 = map2Size.wIDth + map1Size.wIDth / 2;	}	mMap1->setpositionX(posX1);	mMap2->setpositionX(posX2);}voID RollerLayer::rollVertical() {	int posY1 = mMap1->getpositionY();	int posY2 = mMap2->getpositionY();	posY1 -= mSpeed;	posY2 -= mSpeed;	auto map1Size = mMap1->getContentSize();	auto map2Size = mMap2->getContentSize();	auto origin = Director::getInstance()->getVisibleOrigin();	if (posY1 <= -map1Size.height / 2 + origin.y)	{		posY1 = map1Size.height + map2Size.height / 2 + origin.y;	}	if (posY2 <= -map2Size.height / 2 + origin.y) {		posY2 = map2Size.height + map1Size.height / 2 + origin.y;	}	mMap1->setpositionY(posY1);	mMap2->setpositionY(posY2);}voID RollerLayer:: setBackgrounds(Sprite *map1,Sprite*map2) {	if (mMap1 != NulL)		removeChild(mMap1);	if (map2 != NulL)		removeChild(mMap2);	mMap1 = map1;	mMap2 = map2;	setRollOrIEntaion(mOrIEntation);	addChild(mMap1);	addChild(mMap2);}voID RollerLayer::setRollOrIEntaion(int orIEntation) {	if (mMap1 == NulL || mMap2 == NulL) {		return;	}	mOrIEntation = orIEntation;	Size size = Director::getInstance()->getVisibleSize();	auto origin = Director::getInstance()->getVisibleOrigin();	auto map1Size = mMap1->getContentSize();	if (mOrIEntation == VERTICAL) {		mMap1->setposition(Point(origin.x + size.wIDth / 2,origin.y + size.height / 2));		mMap2->setposition(Point(origin.x + size.wIDth / 2,origin.y + size.height / 2 + map1Size.height));		mMap2->setFlippedY(true);	}	else if (mOrIEntation == HORIZONTAL) {		mMap1->setposition(Point(origin.x + size.wIDth / 2,origin.y + size.height / 2));		mMap2->setposition(Point(origin.x + size.wIDth / 2 + map1Size.wIDth,origin.y + size.height / 2));		mMap2->setFlippedX(true);	}	}voID RollerLayer::setSpeed(int iSpeed) {	mSpeed = iSpeed;}

使用
#include "DemoScene.h"#include "RollerLayer.h"Scene * DemoScene::createScene() {	auto scene = Scene::create();	auto demolayer = DemoScene::create();	scene->addChild(demolayer);	return scene;}bool DemoScene::init() {	if (!Layer::init()){		return false;	}	auto rollBackLayer = RollerLayer::create();	auto back1 = Sprite::create("back.jpg");	auto back2 = Sprite::create("back.jpg");	rollBackLayer->setBackgrounds(back1,back2);	rollBackLayer->setRollOrIEntaion(RollerLayer::VERTICAL);	addChild(rollBackLayer);	return true;}
总结

以上是内存溢出为你收集整理的cocos: 无限循环滚动背景全部内容,希望文章能够帮你解决cocos: 无限循环滚动背景所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1078836.html

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

发表评论

登录后才能评论

评论列表(0条)

保存