#include "BaseLayer.h" BaseLayer::BaseLayer() { } BaseLayer::~BaseLayer() { } bool BaseLayer::init() { bool bRes = false; do { CC_BREAK_IF(!Layer::init()); bRes = true; } while (0); return bRes; } void BaseLayer::SetBkGround( const char* bkImage ) { m_SpBk1 = Sprite::create(bkImage); m_SpBk1->setAnchorPoint(Vec2::ZERO); m_SpBk1->setScale(GetWinSize().width / m_SpBk1->getContentSize().width); m_SpBk1->setPosition(Vec2::ZERO); m_SpBk2 = Sprite::create(bkImage); m_SpBk2->setFlipX(true); m_SpBk2->setAnchorPoint(Vec2::ZERO); m_SpBk2->setScale(GetWinSize().width / m_SpBk1->getContentSize().width); m_SpBk2->setPosition(Vec2(GetWinSize().width,0)); this->addChild(m_SpBk1); this->addChild(m_SpBk2); this->schedule(schedule_selector(BaseLayer::ScrollBkGround),1/60); } Size BaseLayer::GetWinSize() { return Director::getInstance()->getWinSize(); } void BaseLayer::ScrollBkGround(float dt) { if (m_SpBk1->getPositionX() <= -GetWinSize().width) { m_SpBk1->setPosition(GetWinSize().width,0); } if (m_SpBk2->getPositionX() <= -GetWinSize().width) { m_SpBk2->setPosition(GetWinSize().width,0); } m_SpBk1->setPosition(Vec2(m_SpBk1->getPositionX() - 1,0)); m_SpBk2->setPosition(Vec2(m_SpBk2->getPositionX() - 1,0)); }
#ifndef __BASELAYER_H__ #define __BASELAYER_H__ #include "cocos2d.h" USING_NS_CC; class BaseLayer : public Layer { public: BaseLayer(); ~BaseLayer(); virtual bool init() override; Size GetWinSize(); void SetBkGround(const char* bkImage); void ScrollBkGround(float dt); private: Sprite *m_SpBk1; Sprite *m_SpBk2; }; #endif原文链接:https://www.f2er.com/cocos2dx/344890.html