Cocos2d-x "Thunder War"-double-layer Map Unlimited scrolling

Source: Internet
Author: User

Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka

This article to achieve the aircraft shooting game Map infinite scrolling function, here is divided into two layers, a layer of infinite downward scrolling, a layer of infinite upward scrolling, this kind of combined effect is very hierarchical sense, also very lifelike, here I put the map layer are written a class, their own map to change, you can become your own! Now, let's get started.

first look at the effect :



Cocos2d-x Version: 3.4

Engineering Environment: VS30213

First, the realization of ideas

is actually two pictures, and then together down (up) scrolling, when a picture completely out of sight, it is transferred to the top. The formation of two pictures alternately appear, however, generally for the game we all feel like a picture, that is because the two pictures of the tail and the connection is connected together. The principle I drew some pictures:

Second, the Code

1, infinite downward scrolling backlayerdown class

Header file:

#ifndef __backlayerdown_h__#define __backlayerdown_h__/*** function is not limited to scrolling down * author Lin Bingwen ([email protected] Blog:/http Blog.csdn.net/evankaka) * Time 2015.2.27*/#include "cocos2d.h" #define MAP_1_TAG   1       //macros define TAG for two maps  #define Map_2_tag   2 class backlayerdown:public Cocos2d::layer{public:    virtual bool init ();    Create_func (backlayerdown);p rivate:void update (float time); virtual void OnExit ();}; #endif//__backlayerdown_h__

Implementation file:

#include "BackLayerDown.h" Using_ns_cc;bool backlayerdown::init () {if (!    Layer::init ()) {return false; } Size visiblesize = Director::getinstance ()->getvisiblesize (); Point origin = Director::getinstance ()->getvisibleorigin (); sprite* Map1 = sprite::create ("Back3_1.png"); sprite* map2 = sprite::create ("Back3_2.png"); Map1->setposition (VEC2 (VISIBLESIZE.WIDTH/2 + origin.x, VISIBLESIZE.HEIGHT/2 + origin.y); Map2->setposition (VEC2 (VISIBLESIZE.WIDTH/2 + origin.x, VisibleSize.height + orig IN.Y + map2->getcontentsize (). HEIGHT/2); This->addchild (map1, 0, Map_1_tag); This->addchild (map2, 0, MAP_2_        TAG); This->scheduleupdate (); return true;} Move and judge the background void backlayerdown::update (float time) {Size visiblesize = director::getinstance ()->getvisiblesize (); Point origin = Director::getinstance ()->getvisibleorigin (); sprite* TemMap1 = (sprite*) this->getchildbytag (Map_1_tag); sprite* temMap2 = (sprite*) this->getchildbytag (Map_2_tag); temmap1-> Setpositiony (Temmap1->getpositiony ()-1); Temmap2->setpositiony (Temmap2->getpositiony ()-1); if (temMap1- >getpositiony () + temmap1->getcontentsize (). HEIGHT/2 <= origin.y) {Float offset = temmap1->getpositiony () + Temmap1->getcontentsize (). HEIGHT/2-Origin.y;temmap1->setposition (VEC2 (VISIBLESIZE.WIDTH/2 + origin.x, Temmap1->getcontentsize (). HEIGHT/2 + ORIGIN.Y + visiblesize.height + offset);} if (Temmap2->getpositiony () + temmap2->getcontentsize (). HEIGHT/2 <= origin.x) {Float offset = temmap2-> Getpositiony () + temmap2->getcontentsize (). HEIGHT/2-Origin.y;temmap2->setposition (VEC2 (VISIBLESIZE.WIDTH/2 + origin.x, Temmap2->getcontentsize (). HEIGHT/2 + ORIGIN.Y + visiblesize.height + offset));}} void Backlayerdown::onexit () {this->unscheduleupdate (); Layer::onexit ();}


2. Unlimited upward scrolling Backlayerup class

Header file:

#ifndef __backlayerup_h__#define __backlayerup_h__/*** function is not limited to scroll up * author Lin Bingwen ([email protected] Blog: http://blog.csdn.net /evankaka) * Time 2015.2.27*/#include "cocos2d.h" #define MAP_1_TAG   1       //macros define TAG of two MAP  #define Map_2_tag   2 class Backlayerup:public Cocos2d::layer{public:    virtual bool init ();    Create_func (Backlayerup);p rivate:void update (float time); virtual void OnExit ();}; #endif//__backlayerup_h__

Implementation file:

#include "BackLayerUp.h" Using_ns_cc;bool backlayerup::init () {if (!    Layer::init ()) {return false; } Size visiblesize = Director::getinstance ()->getvisiblesize (); Point origin = Director::getinstance ()->getvisibleorigin (); sprite* Map1 = sprite::create ("Back4_2.png"); sprite* map2 = sprite::create ("Back4_1.png"); Map1->setposition (VEC2 (VISIBLESIZE.WIDTH/2 + origin.x, VISIBLESIZE.HEIGHT/2 + origin.y); Map2->setposition (VEC2 (VISIBLESIZE.WIDTH/2 + origin.x, Origin.y-map2->getco Ntentsize () HEIGHT/2); This->addchild (map1, 0, Map_1_tag); This->addchild (map2, 0, Map_2_tag);this->        Scheduleupdate (); return true;} Move and judge the background void backlayerup::update (float time) {Size visiblesize = director::getinstance ()->getvisiblesize (); Point origin = Director::getinstance ()->getvisibleorigin (); sprite* TemMap1 = (sprite*) this->getchildbytag (Map_1_tag); sprite* temMap2 = (sprite*) this->getchildbytag (Map_2_tag); Temmap1->setpositiony (temmap1->gEtpositiony () + 1); Temmap2->setpositiony (Temmap2->getpositiony () + 1); if (Temmap1->getpositiony ()-temMap1- >getcontentsize (). HEIGHT/2 >= visiblesize.height) {Float offset = temmap1->getpositiony ()-temmap1-> Getcontentsize (). HEIGHT/2-Visiblesize.height;temmap1->setposition (VEC2 (VISIBLESIZE.WIDTH/2 + origin.x,- Temmap1->getcontentsize (). HEIGHT/2-Origin.y-offset));} if (Temmap2->getpositiony ()-temmap2->getcontentsize (). HEIGHT/2 >= visiblesize.height) {Float offset = Temmap2->getpositiony ()-temmap2->getcontentsize (). HEIGHT/2-Visiblesize.height;temmap2->setposition ( VEC2 (VISIBLESIZE.WIDTH/2 + origin.x,-temmap2->getcontentsize (). HEIGHT/2-Origin.y-offset));}} void Backlayerup::onexit () {this->unscheduleupdate (); Layer::onexit ();}

3. Description

In fact, these two classes can be written together, but here I can let different needs to separate, write them separately, to pay attention to the above method of judgment, infinite downward and infinite upward judgment method is not kind, and, here in order to prevent the black edge, remember to set the position to add a certain amount of offset, As offset in the above function, it is very important here, if there is no edge on this thing, there may be two pictures in the switch, there appears black edge.

Iii. Methods of Use

In the place to be used, add the head file

 1:   #include   "BackLayerDown.h"  
   2:#include"BackLayerUp.h"

Then the init () function in the project is added:

 1:  Size visiblesize = director::getinstance ( )->getvisiblesize (); 
   2: Point origin = Director::getinstance ()->getvisibleorigin ();
   3://This is the ground layer
   4:This->addchild (Backlayerup::create ());
   5://This is a white cloud layer
   6:This->addchild (Backlayerdown::create ());
   7:
 8:  //add an airplane 
 9:  Sprite *airplane_sprite = sprite::create (  "air1.png" ); 
 airplane_sprite->setposition ( VEC2 (VISIBLESIZE.WIDTH/2, VISIBLESIZE.HEIGHT/5)); 
One   :this->addchild (airplane_sprite);

Effect:


Lin Bingwen Evankaka Original works. Reprint please specify the source Http://blog.csdn.net/evankaka



Cocos2d-x "Thunder War"-double-layer Map Unlimited scrolling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.