Scroll Map of cocos2d-x 3. x-to scroll the background
A scroll map is a rolling background map. Its implementation principle is to use two alternate images to synchronize the movement in one direction, when an image exceeds the window limit, the position is replaced to make it return to the back of another image. By repeating these two images in this way, the background seems to be rolling continuously.
Next we will implement a background layer and add it to the game scenario.
# Ifndef _ BackgroundLayer_H __# define _ BackgroundLayer_H __# include "cocos2d. h "// game background Layer class BackgroundLayer: public cocos2d: Layer {public: BackgroundLayer (void );~ BackgroundLayer (void); virtual bool init () override; CREATE_FUNC (BackgroundLayer); // the scroll bar map void scrollBackground (); private: // initialize the background map void initBackground (); private: // background map cocos2d: Sprite * background_1; cocos2d: Sprite * background_2; // double speedY;}; # endif
# Include "BackgroundLayer. h" USING_NS_CC; BackgroundLayer: BackgroundLayer (void) {} BackgroundLayer ::~ BackgroundLayer (void) {} bool BackgroundLayer: init () {if (! Layer: init () {return false;} this-> initBackground (); return true;} // initialize the background void BackgroundLayer: initBackground () {// cache plist file SpriteFrameCache: getInstance ()-> addSpriteFramesWithFile ("gameArts-hd.plist", "gameArts-hd.png "); // Load two background maps in this-> background_1 = Sprite: createWithSpriteFrameName ("background_2.png"); this-> background_1-> setAnchorPoint (Vec2 (0, 0 )); this-> background_1-> setPosition (0, 0); this-> addChild (this-> background_1); this-> background_2 = Sprite: createWithSpriteFrameName ("background_2.png "); this-> background_2-> setAnchorPoint (Vec2 (0, 0); this-> background_2-> setPosition (0, this-> background_1-> getPositionY () + this-> background_1-> getContentSize (). height); this-> addChild (this-> background_2); // you can specify the initial rolling speed. this-> speedY = 2; // when the background is scrolling, black borders appear on the link of the two images. this-> background_1-> getTexture ()-> setAliasTexParameters ();} // scroll the background void BackgroundLayer :: scrollBackground () {// calculate the y-axis coordinate of the next map rolling. Here the downward displacement is auto nextPos_1 = this-> background_1-> getPositionY ()-this-> speedY; auto nextPos_2 = this-> background_2-> getPositionY ()-this-> speedY; this-> background_1-> setPositionY (nextPos_1); this-> background_2-> setPositionY (nextPos_2 ); // when a map is removed from the screen boundary, place it on another map. if (fabs (nextPos_1) = this-> background_1-> getContentSize (). height) {this-> background_1-> setPositionY (this-> background_2-> getPositionY () + this-> background_2-> getContentSize (). height); Sprite * t = this-> background_1; this-> background_1 = this-> background_2; this-> background_2 = t ;}}
Use this scroll map in the game scenario
# Ifndef _ GameScene_H __# define _ GameScene_H __# include "cocos2d. h "# include" BackgroundLayer. h "// main game scenario class GameScene: public cocos2d: Layer {public: GameScene (void );~ GameScene (void); static cocos2d: Scene * createScene (); virtual bool init () override; CREATE_FUNC (GameScene); private: // timer, each frame calls the virtual void update (float delta) override; // The virtual void onEnterTransitionDidFinish () override operation after the scenario is loaded; private: // game background BackgroundLayer * backgroundLayer;}; # endif
# Include "GameScene. h" USING_NS_CC; GameScene: GameScene (void) {} GameScene ::~ GameScene (void) {} Scene * GameScene: createScene () {auto scene = Scene: create (); auto layer = GameScene: create (); scene-> addChild (layer); return scene;} bool GameScene: init () {if (! Layer: init () {return false;} // load the background map this-> backgroundLayer = BackgroundLayer: create (); this-> backgroundLayer-> setAnchorPoint (Vec2 :: ZERO); this-> backgroundLayer-> setPosition (Vec2: ZERO); this-> addChild (backgroundLayer); return true;} void GameScene: onEnterTransitionDidFinish () {Node :: onEnterTransitionDidFinish (); // The Rolling background is displayed only after the scenario is loaded. this-> scheduleUpdate ();} void GameScene: update (float delta) {this-> backgroundLayer-> scrollBackground ();}