[top] cocos2d-x 3.0 game development Xcode 5 handsome blog teaching 003.[HoldTail] game world and background...

Posted by soul on Sun, 20 Oct 2019 16:26:36 +0200

Original link: http://www.cnblogs.com/riasky/p/3455228.html

cocos2d-x 3.0 game development Xcode 5 handsome blog teaching 003.[HoldTail] game world and background screen

The foreword I wrote to you, when I was learning cocos2d-x, I went a lot of detours and encountered a lot of problems. No matter it was simple or difficult, now I have overcome them step by step. Actually, it's very simple to use cocos2d-x to play games. Don't be scared by playing games. The reason why I support opening up games is because of the support of everyone in eoe Shanghai. Hold. Come on, come on, let's make it clear that if you don't write well, please include more. I believe you follow me to blog, step by step will be successful.


Yesterday I made the starting screen of the main scene of the game. Today I want to make the whole game world and the background of the game rolling.

Note that the frame is not high because it is on the door of the simulator, but if it is on the real machine, it is running at 60 full frames.


Design sketch


First, set up the background map class

 

//
//  SWGameMap.h
//  Holdtail
//
//  Created by handsome on 13-12-02.
//
//

#ifndef __Holdtail__SWGameMap__
#define __Holdtail__SWGameMap__

#include <iostream>
#include "cocos2d.h"

using namespace cocos2d;

//Defines the type of enumeration for pictures
typedef enum{
    tag_Img01_01,
    tag_Img01_02,
    tag_Img02_01,
    tag_Img02_02,
    tag_Img03_01,
    tag_Img03_02,
    tag_Img04_01,
    tag_Img04_02,
    tag_Img05_01,
    tag_Img05_02,
    tag_Img06_01,
    tag_Img06_02
}tagMap;

class SWGameMap:public cocos2d::Layer{
public:
    static SWGameMap* createMap (const char* fileName01,
                                 const char* fileName02,
                                 const char* fileName03,
                                 const char* fileName04,
                                 const char* fileName05,
                                 const char* fileName06);
    
    //Provides a way to change the height of all images to move with the background
    static void updateLayerHeight(float layh);
    
    //Provide a public static method to get map objects
    static SWGameMap *sharedMap();
    //Get the current height of the map
    float getMapHeight();
    
    //Map down height of each move down
    void downMap(float time);
    //Map up the height of each up move
    void upMap(float time);
    
private:
    void mapInit(const char* fileName01,
                 const char* fileName02,
                 const char* fileName03,
                 const char* fileName04,
                 const char* fileName05,
                 const char* fileName06);
    
    void update(float time);
    virtual void onExit();
    
};



#endif /* defined(__Holdtail__SWGameMap__) */


 

 

//
//  SWGameMap.cpp
//  Holdtail
//
//  Created by handsome on 13-12-02.
//
//

#include "SWGameMap.h"
#include "SWGameWorld.h"
using namespace cocos2d;

static float layHeight;
static SWGameMap *sm;

SWGameMap *SWGameMap::sharedMap(){
    if(sm != NULL){
        return sm;
    }
    return  NULL;
}


//Create a map object
SWGameMap *SWGameMap::createMap(const char* fileName01,
                                const char* fileName02,
                                const char* fileName03,
                                const char* fileName04,
                                const char* fileName05,
                                const char* fileName06){
    SWGameMap *map = new SWGameMap();
    if(map && map->create()){
        layHeight = 210;
        
        map->autorelease();
        map->mapInit(fileName01,
                     fileName02,
                     fileName03,
                     fileName04,
                     fileName05,
                     fileName06);
        return map;
    }
    
    CC_SAFE_DELETE(map);
    return NULL;
}

float SWGameMap::getMapHeight(){
    return layHeight;
}

//Provides a way to change the height of all images to move with the background
void SWGameMap::updateLayerHeight(float layh){
    if(0 == layh){
        //Map up and down
        sm->unschedule(schedule_selector(SWGameMap::upMap));
        sm->schedule(schedule_selector(SWGameMap::downMap), 0.005f);
    }else if(1 == layh){
        //Map up and down
        sm->unschedule(schedule_selector(SWGameMap::downMap));
        sm->schedule(schedule_selector(SWGameMap::upMap), 0.005f);
    }
}

void SWGameMap::downMap(float time){
    float layh = -2;
    //Control the bottom height of the map
    if (layHeight >= -500) {
        
        layHeight = layHeight+layh;
        
        //Sprite *sp0101 = (Sprite *)sm->getChildByTag(tag_Img01_01);
        //sp0101->setPosition(sp0101->getPosition(), ccp(0, layh)));
        //Sprite *sp0102 = (Sprite *)sm->getChildByTag(tag_Img01_02);
        //sp0102->setPosition(sp0102->getPosition(), ccp(0, layh)));
        
        Sprite *sp0201 = (Sprite *)sm->getChildByTag(tag_Img02_01);
        sp0201->setPosition(sp0201->getPosition() + Point(0, layh));
        
        Sprite *sp0202 = (Sprite *)sm->getChildByTag(tag_Img02_02);
        sp0202->setPosition(sp0202->getPosition() + Point(0, layh));
        
        Sprite *sp0301 = (Sprite *)sm->getChildByTag(tag_Img03_01);
        sp0301->setPosition(sp0301->getPosition() + Point(0, layh));
        Sprite *sp0302 = (Sprite *)sm->getChildByTag(tag_Img03_02);
        sp0302->setPosition(sp0302->getPosition() + Point(0, layh));
        
        Sprite *sp0401 = (Sprite *)sm->getChildByTag(tag_Img04_01);
        sp0401->setPosition(sp0401->getPosition() + Point(0, layh));
        Sprite *sp0402 = (Sprite *)sm->getChildByTag(tag_Img04_02);
        sp0402->setPosition(sp0402->getPosition() + Point(0, layh));
        
        Sprite *sp0501 = (Sprite *)sm->getChildByTag(tag_Img05_01);
        sp0501->setPosition(sp0501->getPosition() + Point(0, layh));
        Sprite *sp0502 = (Sprite *)sm->getChildByTag(tag_Img05_02);
        sp0502->setPosition(sp0502->getPosition() + Point(0, layh));
        
        Sprite *sp0601 = (Sprite *)sm->getChildByTag(tag_Img06_01);
        sp0601->setPosition(sp0601->getPosition() + Point(0, layh));
        Sprite *sp0602 = (Sprite *)sm->getChildByTag(tag_Img06_02);
        sp0602->setPosition(sp0602->getPosition() + Point(0, layh));
    }else{
        //Automatic stop
        sm->unschedule(schedule_selector(SWGameMap::downMap));
    }
}


void SWGameMap::upMap(float time){
    float layh = +2;
    //Control the highest height of characters
    if (layHeight < 210) {
        
        layHeight = layHeight+layh;
        
        //Sprite *sp0101 = (Sprite *)sm->getChildByTag(tag_Img01_01);
        //sp0101->setPosition(sp0101->getPosition(), ccp(0, layh)));
        //Sprite *sp0102 = (Sprite *)sm->getChildByTag(tag_Img01_02);
        //sp0102->setPosition(sp0102->getPosition(), ccp(0, layh)));
        
        Sprite *sp0201 = (Sprite *)sm->getChildByTag(tag_Img02_01);
        sp0201->setPosition(sp0201->getPosition() + Point(0, layh));
        Sprite *sp0202 = (Sprite *)sm->getChildByTag(tag_Img02_02);
        sp0202->setPosition(sp0202->getPosition() + Point(0, layh));
        
        Sprite *sp0301 = (Sprite *)sm->getChildByTag(tag_Img03_01);
        sp0301->setPosition(sp0301->getPosition() + Point(0, layh));
        Sprite *sp0302 = (Sprite *)sm->getChildByTag(tag_Img03_02);
        sp0302->setPosition(sp0302->getPosition() + Point(0, layh));
        
        Sprite *sp0401 = (Sprite *)sm->getChildByTag(tag_Img04_01);
        sp0401->setPosition(sp0401->getPosition() + Point(0, layh));
        Sprite *sp0402 = (Sprite *)sm->getChildByTag(tag_Img04_02);
        sp0402->setPosition(sp0402->getPosition() + Point(0, layh));
        
        Sprite *sp0501 = (Sprite *)sm->getChildByTag(tag_Img05_01);
        sp0501->setPosition(sp0501->getPosition() + Point(0, layh));
        Sprite *sp0502 = (Sprite *)sm->getChildByTag(tag_Img05_02);
        sp0502->setPosition(sp0502->getPosition() + Point(0, layh));
        
        Sprite *sp0601 = (Sprite *)sm->getChildByTag(tag_Img06_01);
        sp0601->setPosition(sp0601->getPosition() + Point(0, layh));
        Sprite *sp0602 = (Sprite *)sm->getChildByTag(tag_Img06_02);
        sp0602->setPosition(sp0602->getPosition() + Point(0, layh));
    }else{
        //Automatic stop
        sm->unschedule(schedule_selector(SWGameMap::upMap));
    }
}

void SWGameMap::mapInit(const char* fileName01,
                        const char* fileName02,
                        const char* fileName03,
                        const char* fileName04,
                        const char* fileName05,
                        const char* fileName06){
    
    sm = this;
    
    //CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    //Create the first map background
    Sprite *turnImg0101 = Sprite::create(fileName01);
    turnImg0101->setPosition(Point(turnImg0101->getContentSize().width*0.5, 250));
    this->addChild(turnImg0101,0,tag_Img01_01);
    //Create the first two map backgrounds
    Sprite *turnImg0102 = Sprite::create(fileName01);
    turnImg0102->setPosition(Point(turnImg0102->getContentSize().width*1.5, 250));
    this->addChild(turnImg0102,0,tag_Img01_02);
    
    //Create a second map background
    Sprite *turnImg0201 = Sprite::create(fileName02);
    turnImg0201->setPosition(Point(turnImg0201->getContentSize().width*0.5, layHeight+700));
    this->addChild(turnImg0201,0,tag_Img02_01);
    //Create a second map background
    Sprite *turnImg0202 = Sprite::create(fileName02);
    turnImg0202->setPosition(Point(turnImg0202->getContentSize().width*1.5, layHeight+700));
    this->addChild(turnImg0202,0,tag_Img02_02);
    
    //Create a third map background
    Sprite *turnImg0301 = Sprite::create(fileName03);
    turnImg0301->setPosition(Point(turnImg0301->getContentSize().width*0.5, layHeight+500));
    this->addChild(turnImg0301,0,tag_Img03_01);
    //Create a third and second map background
    Sprite *turnImg0302 = Sprite::create(fileName03);
    turnImg0302->setPosition(Point(turnImg0302->getContentSize().width*1.5, layHeight+500));
    this->addChild(turnImg0302,0,tag_Img03_02);
    
    //Create a fourth map background
    Sprite *turnImg0401 = Sprite::create(fileName04);
    turnImg0401->setPosition(Point(turnImg0401->getContentSize().width*0.5, layHeight+300));
    this->addChild(turnImg0401,0,tag_Img04_01);
    //Create a fourth and second map background
    Sprite *turnImg0402 = Sprite::create(fileName04);
    turnImg0402->setPosition(Point(turnImg0402->getContentSize().width*1.5, layHeight+300));
    this->addChild(turnImg0402,0,tag_Img04_02);
    
    //Create the fifth map background
    Sprite *turnImg0501 = Sprite::create(fileName05);
    turnImg0501->setPosition(Point(turnImg0501->getContentSize().width*0.5, layHeight+100));
    this->addChild(turnImg0501,0,tag_Img05_01);
    //Create the fifth and second map background
    Sprite *turnImg0502 = Sprite::create(fileName05);
    turnImg0502->setPosition(Point(turnImg0502->getContentSize().width*1.5, layHeight+100));
    this->addChild(turnImg0502,0,tag_Img05_02);
    
    //Create the sixth map background
    Sprite *turnImg0601 = Sprite::create(fileName06);
    turnImg0601->setPosition(Point(turnImg0601->getContentSize().width*0.5, layHeight));
    this->addChild(turnImg0601,0,tag_Img06_01);
    //Create the sixth and second map background
    Sprite *turnImg0602 = Sprite::create(fileName06);
    turnImg0602->setPosition(Point(turnImg0602->getContentSize().width*1.5, layHeight));
    this->addChild(turnImg0602,0,tag_Img06_02);
    
    //To update
    this->scheduleUpdate();
}

void SWGameMap::update(float time){
    //CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    //First picture
    Sprite *sp0101 = (Sprite *)this->getChildByTag(tag_Img01_01);
    if(sp0101->getPositionX() <= -sp0101->getContentSize().width*0.5){
        sp0101->setPosition(Point(sp0101->getContentSize().width*1.5f-2, 250));
    }else{
        sp0101->setPosition(sp0101->getPosition() + Point(-2, 0));
    }
    Sprite *sp0102 = (Sprite *)this->getChildByTag(tag_Img01_02);
    if(sp0102->getPositionX() <= -sp0102->getContentSize().width*0.5){
        sp0102->setPosition(Point(sp0102->getContentSize().width*1.5f-2, 250));
    }else{
        sp0102->setPosition(sp0102->getPosition() + Point(-2, 0));
    }
    
    
    //Second picture
    Sprite *sp0201 = (Sprite *)this->getChildByTag(tag_Img02_01);
    if(sp0201->getPositionX() <= -sp0201->getContentSize().width*0.5){
        sp0201->setPosition(Point(sp0201->getContentSize().width*1.5f-3, layHeight+700));
    }else{
        sp0201->setPosition(sp0201->getPosition() + Point(-1.5, 0));
    }
    Sprite *sp0202 = (Sprite *)this->getChildByTag(tag_Img02_02);
    if(sp0202->getPositionX() <= -sp0202->getContentSize().width*0.5){
        sp0202->setPosition(Point(sp0202->getContentSize().width*1.5f-3, layHeight+700));
    }else{
        sp0202->setPosition(sp0202->getPosition() + Point(-1.5, 0));
    }
    
    //Third picture
    Sprite *sp0301 = (Sprite *)this->getChildByTag(tag_Img03_01);
    if(sp0301->getPositionX() <= -sp0301->getContentSize().width*0.5){
        sp0301->setPosition(Point(sp0301->getContentSize().width*1.5f-2, layHeight+500));
    }else{
        sp0301->setPosition(sp0301->getPosition() + Point(-2, 0));
    }
    Sprite *sp0302 = (Sprite *)this->getChildByTag(tag_Img03_02);
    if(sp0302->getPositionX() <= -sp0302->getContentSize().width*0.5){
        sp0302->setPosition(Point(sp0302->getContentSize().width*1.5f-2, layHeight+500));
    }else{
        sp0302->setPosition(sp0302->getPosition() + Point(-2, 0));
    }
    
    //Fourth picture
    Sprite *sp0401 = (Sprite *)this->getChildByTag(tag_Img04_01);
    if(sp0401->getPositionX() <= -sp0401->getContentSize().width*0.5){
        sp0401->setPosition(Point(sp0401->getContentSize().width*1.5f-2.5, layHeight+300));
    }else{
        sp0401->setPosition(sp0401->getPosition() + Point(-2.5, 0));
    }
    Sprite *sp0402 = (Sprite *)this->getChildByTag(tag_Img04_02);
    if(sp0402->getPositionX() <= -sp0402->getContentSize().width*0.5){
        sp0402->setPosition(Point(sp0402->getContentSize().width*1.5f-2.5, layHeight+300));
    }else{
        sp0402->setPosition(sp0402->getPosition() + Point(-2.5, 0));
    }
    
    //Fifth picture
    Sprite *sp0501 = (Sprite *)this->getChildByTag(tag_Img05_01);
    if(sp0501->getPositionX() <= -sp0501->getContentSize().width*0.5){
        sp0501->setPosition(Point(sp0501->getContentSize().width*1.5f-3, layHeight+100));
    }else{
        sp0501->setPosition(sp0501->getPosition() + Point(-3, 0));
    }
    Sprite *sp0502 = (Sprite *)this->getChildByTag(tag_Img05_02);
    if(sp0502->getPositionX() <= -sp0502->getContentSize().width*0.5){
        sp0502->setPosition(Point(sp0502->getContentSize().width*1.5f-3, layHeight+100));
    }else{
        sp0502->setPosition(sp0502->getPosition() + Point(-3, 0));
    }
    
    //Sixth picture
    Sprite *sp0601 = (Sprite *)this->getChildByTag(tag_Img06_01);
    if(sp0601->getPositionX() <= -sp0601->getContentSize().width*0.5){
        sp0601->setPosition(Point(sp0601->getContentSize().width*1.5f-4, layHeight));
    }else{
        sp0601->setPosition(sp0601->getPosition() + Point(-4, 0));
    }
    Sprite *sp0602 = (Sprite *)this->getChildByTag(tag_Img06_02);
    if(sp0602->getPositionX() <= -sp0602->getContentSize().width*0.5){
        sp0602->setPosition(Point(sp0602->getContentSize().width*1.5f-4, layHeight));
    }else{
        sp0602->setPosition(sp0602->getPosition() + Point(-4, 0));
    }
}

//Sign out
void SWGameMap::onExit(){
    this->unscheduleUpdate();
    CCLayer::onExit();
}


 


After the map class is established, the next step is to establish the game world class.

 

//
//  SWGameWorld.h
//  Holdtail
//
//  Created by handsome on 13-12-02.
//
//

#ifndef __Holdtail__SWGameWorld__
#define __Holdtail__SWGameWorld__

#include <iostream>
#include "cocos2d.h"

//Defining attributes
typedef enum {
    tag_player
}tagWorld;

class SWGameWorld:public cocos2d::Layer{
public:
    static cocos2d::Scene *scene();
    static SWGameWorld *sharedWorld();
private:
    virtual bool init();
    CREATE_FUNC(SWGameWorld);
    
};
#endif /* defined(__Holdtail__SWGameWorld__) */


 

 

//
//  SWGameWorld.cpp
//  Holdtail
//
//  Created by handsome on 13-12-02.
//
//

#include "SWGameWorld.h"
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "SWGameMap.h"

using namespace cocos2d;
using namespace CocosDenshion;

//Declare static variables
static SWGameWorld *SWGW;

SWGameWorld *SWGameWorld::sharedWorld(){
    if(SWGW != NULL){
        return SWGW;
    }
    return  NULL;
}


Scene *SWGameWorld::scene(){
    Scene *scene = Scene::create();
    SWGameWorld *layer = SWGameWorld::create();
    scene->addChild(layer);
    return scene;
}

//Create scene
bool SWGameWorld::init(){
    if( !Layer::init()){
        return false;
    }
    SWGW = this;
    
    //Map
    SWGameMap *map = SWGameMap::createMap("cloudbg.png","cloud04.png","cloud03.png","cloud02.png","cloud01.png","treesbg.png");
    addChild(map);
    
    return true;
}


 


When these two classes are established, they can be used. Click the start game button to switch to the world class, and the background can be scrolled.

 

//Implementation of various button click methods
void SWMenu::PlayPressed(Object* pSender){
    //Realization
    CCDirector::getInstance()->replaceScene(CCTransitionFade::create(0.5, SWGameWorld::scene()));
}


 

This stage of today's project package download - > Direct aircraft

 

Reproduced at: https://www.cnblogs.com/riasky/p/3455228.html

Topics: xcode simulator