zoukankan      html  css  js  c++  java
  • [置顶] cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面

    cocos2d-x 3.0游戏开发xcode5帅印博客教学 003.[HoldTail]游戏世界以及背景画面

    写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不管是简单的还是困难的现在都慢慢的一步一步克服了,其实用cocos2d-x做游戏特别的简单,大家不要被是做游戏吓到了,支持我把游戏开源的原因是因为  eoe上海  的大家的支持。加油,加油,要说明一下的是,写得不好还请大家多多包含哦。相信你们跟着我得博客,一步一步做肯定会成功的。


    昨天做了游戏的主场景的开始画面,今天要做的是整个游戏的世界和游戏的滚动的背景

    注意由于是在模拟器上门进行所以帧不高,但是如果是在真机上,则是60帧满帧运行


    效果图


    首先建立背景地图类

     

    //
    //  SWGameMap.h
    //  Holdtail
    //
    //  Created by 帅 印 on 13-12-02.
    //
    //
    
    #ifndef __Holdtail__SWGameMap__
    #define __Holdtail__SWGameMap__
    
    #include <iostream>
    #include "cocos2d.h"
    
    using namespace cocos2d;
    
    //定义图片的枚举的类型
    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);
        
        //提供一个方法修改所有图片的高度,使之随着背景移动
        static void updateLayerHeight(float layh);
        
        //提供一个公共静态的方法获取地图对象
        static SWGameMap *sharedMap();
        //获取地图现在的高度
        float getMapHeight();
        
        //地图下移   每次下移的高度
        void downMap(float time);
        //地图上移   每次上移的高度
        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 帅 印 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;
    }
    
    
    //创建一个地图对象
    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;
    }
    
    //提供一个方法修改所有图片的高度,使之随着背景移动
    void SWGameMap::updateLayerHeight(float layh){
        if(0 == layh){
            //地图的上下移动
            sm->unschedule(schedule_selector(SWGameMap::upMap));
            sm->schedule(schedule_selector(SWGameMap::downMap), 0.005f);
        }else if(1 == layh){
            //地图的上下移动
            sm->unschedule(schedule_selector(SWGameMap::downMap));
            sm->schedule(schedule_selector(SWGameMap::upMap), 0.005f);
        }
    }
    
    void SWGameMap::downMap(float time){
        float layh = -2;
        //控制地图的最底高度
        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{
            //自动停止
            sm->unschedule(schedule_selector(SWGameMap::downMap));
        }
    }
    
    
    void SWGameMap::upMap(float time){
        float layh = +2;
        //控制人物的最高高度
        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{
            //自动停止
            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();
        
        //创建第一一个地图背景
        Sprite *turnImg0101 = Sprite::create(fileName01);
        turnImg0101->setPosition(Point(turnImg0101->getContentSize().width*0.5, 250));
        this->addChild(turnImg0101,0,tag_Img01_01);
        //创建第一二个地图背景
        Sprite *turnImg0102 = Sprite::create(fileName01);
        turnImg0102->setPosition(Point(turnImg0102->getContentSize().width*1.5, 250));
        this->addChild(turnImg0102,0,tag_Img01_02);
        
        //创建第二一个地图背景
        Sprite *turnImg0201 = Sprite::create(fileName02);
        turnImg0201->setPosition(Point(turnImg0201->getContentSize().width*0.5, layHeight+700));
        this->addChild(turnImg0201,0,tag_Img02_01);
        //创建第二二个地图背景
        Sprite *turnImg0202 = Sprite::create(fileName02);
        turnImg0202->setPosition(Point(turnImg0202->getContentSize().width*1.5, layHeight+700));
        this->addChild(turnImg0202,0,tag_Img02_02);
        
        //创建第三一个地图背景
        Sprite *turnImg0301 = Sprite::create(fileName03);
        turnImg0301->setPosition(Point(turnImg0301->getContentSize().width*0.5, layHeight+500));
        this->addChild(turnImg0301,0,tag_Img03_01);
        //创建第三二个地图背景
        Sprite *turnImg0302 = Sprite::create(fileName03);
        turnImg0302->setPosition(Point(turnImg0302->getContentSize().width*1.5, layHeight+500));
        this->addChild(turnImg0302,0,tag_Img03_02);
        
        //创建第四一个地图背景
        Sprite *turnImg0401 = Sprite::create(fileName04);
        turnImg0401->setPosition(Point(turnImg0401->getContentSize().width*0.5, layHeight+300));
        this->addChild(turnImg0401,0,tag_Img04_01);
        //创建第四二个地图背景
        Sprite *turnImg0402 = Sprite::create(fileName04);
        turnImg0402->setPosition(Point(turnImg0402->getContentSize().width*1.5, layHeight+300));
        this->addChild(turnImg0402,0,tag_Img04_02);
        
        //创建第五一个地图背景
        Sprite *turnImg0501 = Sprite::create(fileName05);
        turnImg0501->setPosition(Point(turnImg0501->getContentSize().width*0.5, layHeight+100));
        this->addChild(turnImg0501,0,tag_Img05_01);
        //创建第五二个地图背景
        Sprite *turnImg0502 = Sprite::create(fileName05);
        turnImg0502->setPosition(Point(turnImg0502->getContentSize().width*1.5, layHeight+100));
        this->addChild(turnImg0502,0,tag_Img05_02);
        
        //创建第六一个地图背景
        Sprite *turnImg0601 = Sprite::create(fileName06);
        turnImg0601->setPosition(Point(turnImg0601->getContentSize().width*0.5, layHeight));
        this->addChild(turnImg0601,0,tag_Img06_01);
        //创建第六二个地图背景
        Sprite *turnImg0602 = Sprite::create(fileName06);
        turnImg0602->setPosition(Point(turnImg0602->getContentSize().width*1.5, layHeight));
        this->addChild(turnImg0602,0,tag_Img06_02);
        
        //更新
        this->scheduleUpdate();
    }
    
    void SWGameMap::update(float time){
        //CCSize size = CCDirector::sharedDirector()->getWinSize();
        
        //第一张图片
        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));
        }
        
        
        //第二张图片
        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));
        }
        
        //第三张图片
        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));
        }
        
        //第四张图片
        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));
        }
        
        //第五张图片
        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));
        }
        
        //第六张图片
        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));
        }
    }
    
    //退出
    void SWGameMap::onExit(){
        this->unscheduleUpdate();
        CCLayer::onExit();
    }



    地图类建立完成之后,接下来建立游戏世界类

     

    //
    //  SWGameWorld.h
    //  Holdtail
    //
    //  Created by 帅 印 on 13-12-02.
    //
    //
    
    #ifndef __Holdtail__SWGameWorld__
    #define __Holdtail__SWGameWorld__
    
    #include <iostream>
    #include "cocos2d.h"
    
    //定义属性
    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 帅 印 on 13-12-02.
    //
    //
    
    #include "SWGameWorld.h"
    #include "cocos2d.h"
    #include "SimpleAudioEngine.h"
    #include "SWGameMap.h"
    
    using namespace cocos2d;
    using namespace CocosDenshion;
    
    //声明静态变量
    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;
    }
    
    //创建场景
    bool SWGameWorld::init(){
        if( !Layer::init()){
            return false;
        }
        SWGW = this;
        
        //地图
        SWGameMap *map = SWGameMap::createMap("cloudbg.png","cloud04.png","cloud03.png","cloud02.png","cloud01.png","treesbg.png");
        addChild(map);
        
        return true;
    }
    



    当这两个类都建立完成后,就可以使用了,点击开始游戏按钮,切换到世界类,背景就可以滚动咯

     

    //各种按钮点击的方法实现
    void SWMenu::PlayPressed(Object* pSender){
        //实现
        CCDirector::getInstance()->replaceScene(CCTransitionFade::create(0.5, SWGameWorld::scene()));
    }


    今天的这个阶段的项目打包下载-->飞机直达

  • 相关阅读:
    Selenium2+python自动化-查看selenium API
    彻底搞懂scrapy的中间件第二章
    彻底搞懂scrapy的中间件第一章
    爬虫面试题
    爬取58同城二手房数据存储到redis数据库和mysql数据库
    Scrapy操作浏览器获取网易新闻数据
    Scrapy框架中的 UA伪装
    scrapy框架中如何使用selenuim
    基于scrapy中---全站爬取数据----CrawlSpider的使用
    基于百度AI的自然语言处理文字分类
  • 原文地址:https://www.cnblogs.com/riasky/p/3455228.html
Copyright © 2011-2022 走看看