头文件:
#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; }