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

cocos: 无限循环滚动背景

发布时间:2020-12-14 16:59:28 所属栏目:百科 来源:网络整理
导读:头文件: #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

头文件:

#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;
}

(编辑:李大同)

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

    推荐文章
      热点阅读