cocos2d-html5背景循环移动,cocos: 无限循环滚动背景

头文件:

#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: 无限循环滚动背景所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

小编个人微信号 jb51ccc

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值