Cocos2d-x game development parkour (6) map Loop

Source: Internet
Author: User

 

Now, a person can run, jump, and squat. But he is still a lonely runner, running on a endless online in a dark world. Now, we add background and floor to him to let him run in a colorful world.

 

This map is a custom class. In C ++, try not to define a class. Otherwise, you can create a new object and delete it. Although it can run the same way, it looks strange because it is different from the engine style.

 

Here I create a MapManager that inherits CCNode and also gives it a create static function and an init function, which is the same as the engine structure:

The main logic is as follows:

1. Set the anchor of the background and floor to the lower left corner, and place the two images together.

2. The camera moves. if the first one is out of the camera range, the heavy-duty Pointer Points to this genie.

3. Reset the x-axis position by reloading the pointer

 

////  Map.h//  Parkour////  Created by lerry on 3/25/14.////#ifndef __Parkour__Map__#define __Parkour__Map__#include cocos2d.h#include resources.h#include Box2D/Box2d.hUSING_NS_CC;class MapManager : public CCNode{    CCLayer* mLayer;        b2World* mWorld;        float mMapWidth;        int mMapCount;        CCSprite* mMap0;        CCSprite* mMap1;        CCSprite* mGround0;        CCSprite* mGround1;        int mCurMap;    public:        MapManager();            static MapManager* create(CCLayer* layer, b2World* world);        bool init(CCLayer* layer, b2World* world);        bool chechReloadMap(float eyeX);};#endif /* defined(__Parkour__Map__) */

See the implementation below:

 

 

//// Map. cpp // Parkour /// Created by lerry on 3/25/14. //// # include Map. hMapManager: MapManager (): mLayer (NULL), mWorld (NULL), mMapWidth (0), mMapCount (2), mMap0 (NULL), mMap1 (NULL ), mGround0 (NULL), mGround1 (NULL), mCurMap (0) {} MapManager * MapManager: create (cocos2d: CCLayer * layer, b2World * world) {MapManager * map = new MapManager (); if (map & map-> init (layer, world) {map-> autorelease (); return map;} else {Delete map; map = NULL; return NULL ;}/ ** upload a master CCLayer, the physical world in the master Layer */bool MapManager: init (cocos2d :: CCLayer * layer, b2World * world) {if (! CCNode: init () {return false;} mLayer = layer; mWorld = world; // two backgrounds, two floors mMap0 = CCSprite: create (map0 ); mMap1 = CCSprite: create (map1); mGround0 = CCSprite: create (ground0); mGround1 = CCSprite: create (ground1); // background 0, set mMap0-> setAnchorPoint (ccp (0, 0); mMap0-> setPosition (ccp (0, 0); mLayer-> addChild (mMap0 ); // Floor 0, set mGround0-> setAnchorPoint (ccp (0, 0); CCSize groundSize = mGround0-> getContentSize (); mGround0-> setPosition (ccp (0, GROUND_HEIGHT-groundSize. height); mLayer-> addChild (mGround0); // map width mMapWidth = mMap0-> getContentSize (). width; // Background 1, set the lower left corner of the anchorage, location mMap1-> setAnchorPoint (ccp (0, 0); mMap1-> setPosition (ccp (mMapWidth, 0 )); mLayer-> addChild (mMap1); // floor 1, set the lower left corner of the anchor, position mGround1-> setAnchorPoint (ccp (0, 0); mGround1-> setPosition (ccp (mMapWidth, GROUND_HEIGHT-groundSize. height); mLayer-> addChild (mGround1); return true;}/** map loop logic */bool MapManager: chechReloadMap (float eyeX) {int newCur = eyeX/mMapWidth; if (mCurMap = newCur) {return false;} // The overloaded genie pointer CCSprite * tempMap; CCSprite * tempGround; // reload the pointer to the rolling background and floor genie if (0 = newCur % mMapCount) {tempMap = mMap1; tempGround = mGround1;} else {tempMap = mMap0; tempGround = mGround0;} mCurMap = newCur; // reset the x coordinate tempMap-> setPositionX (mMapWidth * (mCurMap + 1 )); tempGround-> setPositionX (mMapWidth * (mCurMap + 1); return true ;}

Add this node to the init function in PlayScene. cpp, otherwise it will be killed and cause an error.

 

 

    mMapManager = MapManager::create(this, mWorld);    this->addChild(mMapManager);

Then add the map reload logic to the update file.

 

 

    if (mMapManager->chechReloadMap(mLastEyeX)) {        CCLog(reload map);    }

Okay, this is done. The next article is about to complete this project. Let's look forward to it.

 

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.