cocos2d-x的初步学习二十五之连连看一
发布日期:2022-02-08 18:03:29 浏览次数:28 分类:技术文章

本文共 11609 字,大约阅读时间需要 38 分钟。

这篇文章中,我们将简单的做一个连连看的demo,这里也会涉及到寻径算法,连连看有三种情况,一条直线的,一个拐点的,两个拐点的,

 情况一:要连接的两点在同一条直线上

           
                0 0 0 0 0 0
                0 2 0 0 0 2        * ------ *
                0 0 0 0 0 0 

       情况二:经过一个折点相连(+号代表折点)

                0 0 0 0 0 0       

                0 2 0 0 0 +        * ------ +       
                0 + 0 0 0 2        + ------ *
              (两条路都可连通)

       情况三:经过两个折点相连(针对企鹅来说,即数字2)

           
                0 + 0 0 0 +          0 0 0 0 0 0
                0 2 0 1 0 2          0 2 0 1 0 2
                0 0 0 0 0 0  或者    0 + 0 0 0 +

                由于有1这个障碍,所以需要两个折点才能连通

关于具体算法的问题,网上应该有很多,这里就不说了。。。

首页,我们先模仿某个游戏的界面,先做一个首界面。首界面我们要实现这样的效果,,白云,蓝天,船儿在航行,鸟儿在飞翔,海浪~~O(∩_∩)O

我们新建一个工程,直接在HelloWorld这个类里修改,下面直接上代码:

HelloWorldScene.h

typedef enum {        kRight,    kLeft        }boatDirection;typedef enum {        birdsRight,    birdsLeft        }birdsDirection;class HelloWorld : public cocos2d::CCLayer{public:    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)    virtual bool init();    // there's no 'id' in cpp, so we recommend to return the class instance pointer    static cocos2d::CCScene* scene();    // preprocessor macro for "static create()" constructor ( node() deprecated )    CREATE_FUNC(HelloWorld);        private:            void initUI(void);        void initData(void);        void initSounds(void);        void addBoat(void);        void resetSpritePos(void);        void wavesAnimation(void);        void wavesAnimation2(void);        void wavesAnimation3(void);        void birdsselfAnimation(void);        void birdsselfAnimation2(void);        void resetBirdsSpritePos(void);        void playBack(void);        cocos2d::CCSprite *cloudSprite;    cocos2d::CCSprite *cloud2Sprite;    cocos2d::CCSprite *boatSprite;    cocos2d::CCSprite *wavesSprite;    cocos2d::CCSprite *birdsSprite;            boatDirection _boatDirection;        birdsDirection _birdsDirection;                float boatMoveTime;    };
HelloWorldScene.cpp

bool HelloWorld::init(){    //    // 1. super init first    if ( !CCLayer::init() )    {        return false;    }                    this->initData();        this->initSounds();        this->initUI();                return true;}void HelloWorld::initData(){            CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sailing_boat-hd.plist");    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("seagull-hd.plist");        boatMoveTime=45.0f;        _boatDirection=kLeft;    _birdsDirection=birdsLeft;    }//音频文件初始化void HelloWorld::initSounds(){                }//UI界面的初始化void HelloWorld::initUI(){    CCSprite *bgSprite=NULL;            if (wSize.width==1136 && wSize.height==640)    {                        bgSprite=CCSprite::create("background_568-hd.png");                        }    else    {                 bgSprite=CCSprite::create("background-hd.png");                }        bgSprite->setPosition(ccp(wSize.width/2, wSize.height/2));    this->addChild(bgSprite, 0);                //白云1    cloudSprite=CCSprite::create("background_cloud_1-hd.png");    cloudSprite->setAnchorPoint(ccp(0, 0));    cloudSprite->setPosition(ccp(0, wSize.height-cloudSprite->getContentSize().height));    this->addChild(cloudSprite, 1);            //白云倒影    CCSprite *daoyingloudSprite=CCSprite::create("background_cloud_1-hd.png");    daoyingloudSprite->setAnchorPoint(ccp(0, 0));    //垂直翻转    daoyingloudSprite->setFlipY(true);    daoyingloudSprite->setOpacity(40);    daoyingloudSprite->setPosition(ccp(wSize.width-cloudSprite->getContentSize().width-40, wSize.height-cloudSprite->getContentSize().height-78*2));    this->addChild(daoyingloudSprite, 1);    //白云2    cloud2Sprite=CCSprite::create("background_cloud_2-hd.png");    cloud2Sprite->setAnchorPoint(ccp(0, 0));    cloud2Sprite->setPosition(ccp(cloudSprite->getPosition().x+cloudSprite->getContentSize().width, wSize.height-cloud2Sprite->getContentSize().height));    this->addChild(cloud2Sprite, 1);    //岛    CCSprite *landSprite=CCSprite::create("island-hd.png");    landSprite->setAnchorPoint(ccp(0, 0));    landSprite->setPosition(ccp(wSize.width-landSprite->getContentSize().width-20*2, wSize.height-landSprite->getContentSize().height-47*2));    this->addChild(landSprite, 1);        //倒影    CCSprite *daoyinglandSprite=CCSprite::create("island-hd.png");    daoyinglandSprite->setAnchorPoint(ccp(0, 0));    daoyinglandSprite->setFlipY(true);    daoyinglandSprite->setOpacity(40);    daoyinglandSprite->setPosition(ccp(wSize.width-landSprite->getContentSize().width-20*2, wSize.height-landSprite->getContentSize().height-78*2));    this->addChild(daoyinglandSprite, 1);    //船    boatSprite=CCSprite::createWithSpriteFrameName("sailing_boat1.png");    boatSprite->setPosition(ccp(wSize.width-boatSprite->getContentSize().width-50*2,230*2));    this->addChild(boatSprite, 1);        this->addBoat();    //沙滩    CCSprite *shatanSprite=CCSprite::create("beach_adornment-hd.png");        shatanSprite->setPosition(ccp(wSize.width/2, shatanSprite->getContentSize().height/2));        this->addChild(shatanSprite, 1);                if (wSize.width==1136 && wSize.height==640)    {                        wavesSprite=CCSprite::create("background_sea1_568-hd.png");                            }    else    {                        wavesSprite=CCSprite::create("background_sea1-hd.png");                            }    wavesSprite->setPosition(ccp(wSize.width/2, 140*2));    wavesSprite->setOpacity(0);    this->addChild(wavesSprite, 1);        this->schedule(schedule_selector(HelloWorld::wavesAnimation), 10);        //鸟    birdsSprite=CCSprite::createWithSpriteFrameName("seagull1.png");    birdsSprite->setAnchorPoint(ccp(0, 0));    birdsSprite->setPosition(ccp(200*2,270*2));    birdsSprite->setScale(0.7);    this->addChild(birdsSprite, 1);        this->schedule(schedule_selector(HelloWorld::birdsselfAnimation), 5);        this->birdsselfAnimation2();        //    //开始菜单//    CCSprite *playSprite=CCSprite::create("button_play-hd.png");//    CCSprite *playSprite_s=CCSprite::create("button_play_s-hd.png");//    //    CCMenuItemSprite *item=CCMenuItemSprite::create(playSprite, playSprite_s, this, menu_selector(HelloWorld::playBack));//    //    //    CCMenu *menu=CCMenu::create(item,NULL);//    //    item->setAnchorPoint(ccp(0.5, 0.5));//    menu->setPosition(ccp(wSize.width/2,110*2));//    //    //    this->addChild(menu, 1);            }//船自身的动画void HelloWorld::addBoat(){    boatSprite->setFlipX(false);        CCArray *array=CCArray::create();        for (int i = 1; i < 4; i++)    {                CCString *string=CCString::createWithFormat("sailing_boat%d.png");                CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());                array->addObject(frame);    }        CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.5);    CCAnimate *animate=CCAnimate::create(animation);        CCRepeatForever *ac1=CCRepeatForever::create(animate);        boatSprite->runAction(ac1);        array->removeAllObjects();        //船移动动画,到两边自动返回    CCMoveTo *ac2;        switch (_boatDirection)    {        case kLeft:                              ac2=CCMoveTo::create(boatMoveTime, CCPointMake(-boatSprite->getContentSize().width, boatSprite->getPosition().y));            _boatDirection=kRight;                        boatSprite->setFlipX(false);                        break;                    case kRight:                        ac2=CCMoveTo::create(boatMoveTime, CCPointMake(wSize.width, boatSprite->getPosition().y));            _boatDirection=kLeft;                        boatSprite->setFlipX(true);                        break;                    default:                                    break;                }       CCSequence *seq=CCSequence::create(ac2,CCCallFunc::create(this, callfunc_selector(HelloWorld::resetSpritePos)),NULL);        boatSprite->runAction(seq);        }//重新设置船的位置void HelloWorld::resetSpritePos(){    this->addBoat();}//海浪动画void HelloWorld::wavesAnimation(){    SimpleAudioEngine::sharedEngine()->playEffect("14.wav");            CCMoveTo *ac1=CCMoveTo::create(4.0f, CCPointMake(wavesSprite->getPosition().x, 100*2));        CCFadeIn *ac2=CCFadeIn::create(4.0f);        //同时进行    CCSpawn *spawn=CCSpawn::create(ac1,ac2,NULL);    //队列    CCSequence *seq=CCSequence::create(spawn,CCCallFunc::create(this, callfunc_selector(HelloWorld::wavesAnimation2)),NULL);    wavesSprite->runAction(seq);        }void HelloWorld::wavesAnimation2(){    CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage("background_sea2_568-hd.png");    wavesSprite->setTexture(aTexture);    CCMoveTo *ac1=CCMoveTo::create(4.0f, CCPointMake(wavesSprite->getPosition().x, 140*2));        CCFadeIn *ac2=CCFadeIn::create(4.0f);        //同时进行    CCSpawn *spawn=CCSpawn::create(ac1,ac2,NULL);        //队列    CCSequence *seq=CCSequence::create(spawn,CCCallFunc::create(this, callfunc_selector(HelloWorld::wavesAnimation3)),NULL);        wavesSprite->runAction(seq);    }void HelloWorld::wavesAnimation3(){    CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage("background_sea1_568-hd.png");        wavesSprite->setTexture(aTexture);        wavesSprite->setOpacity(0);}//鸟自身的动画void HelloWorld::birdsselfAnimation(){        SimpleAudioEngine::sharedEngine()->playEffect("15.wav");    CCArray *array=CCArray::create();        for (int i = 1; i < 4; i++)    {                CCString *string=CCString::createWithFormat("seagull%d.png");                CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());                array->addObject(frame);            }        CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.2);        animation->setRestoreOriginalFrame(true);        CCAnimate *animate=CCAnimate::create(animation);        int times=arc4random()%5+1;        CCRepeat *ac1=CCRepeat::create(animate, times);        birdsSprite->runAction(ac1);        array->removeAllObjects();}//鸟飞翔的动画void HelloWorld::birdsselfAnimation2(){         float yoffset=arc4random()%40+1;        CCPoint birdsPos=CCPointMake(-birdsSprite->getContentSize().width, 270*2+yoffset);        CCMoveTo *ac2;        switch (_birdsDirection) {        case birdsLeft:                        ac2=CCMoveTo::create(35.0f, CCPointMake(birdsPos.x, birdsPos.y));            _birdsDirection=birdsRight;                        birdsSprite->setFlipX(false);            break;                    case birdsRight:                        ac2=CCMoveTo::create(35.0f, CCPointMake(wSize.width, 270*2+yoffset));            _birdsDirection=birdsLeft;                        birdsSprite->setFlipX(true);            break;                    default:            break;                }            CCSpawn *spawn=CCSpawn::create(ac2,NULL);        CCSequence *seq=CCSequence::create(spawn,CCCallFunc::create(this, callfunc_selector(HelloWorld::resetBirdsSpritePos)),NULL);        birdsSprite->runAction(seq);        }void HelloWorld::resetBirdsSpritePos(){        this->birdsselfAnimation2();}
上面中,我们初始化了一些界面元素,然后做了一些简单的动画效果,整体上都是很简单的,这里就不在多说了,

~~~~~~~~

然后在函数initUI中加入初始化开始菜单

//开始菜单    CCSprite *playSprite=CCSprite::create("button_play-hd.png");    CCSprite *playSprite_s=CCSprite::create("button_play_s-hd.png");        CCMenuItemSprite *item=CCMenuItemSprite::create(playSprite, playSprite_s, this, menu_selector(HelloWorld::playBack));            CCMenu *menu=CCMenu::create(item,NULL);        item->setAnchorPoint(ccp(0.5, 0.5));    menu->setPosition(ccp(wSize.width/2,110*2));            this->addChild(menu, 1);
//开始游戏void HelloWorld::playBack(){}

OK,,开始界面就完成~~~下面就我们将完成连连看的游戏界面~~~

(*^__^*) …….....................................................................................................

转载地址:https://blog.csdn.net/kuloveyouwei/article/details/12912875 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iOS 7: 如何为iPhone 5S编译64位应用
下一篇:cocos2d-x的初步学习二十七之连连看三

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年03月15日 00时19分55秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

opencv matlab编程,在Matlab中调用OpenCV函数 | 学步园 2019-04-21
c语言文件wt,c语言,wt和rt中的t是什么意思 2019-04-21
c语言运行几进制,【C语言】求已知等式在几进制条件下成立 2019-04-21
电梯运行仿真c语言代码,电梯调度算法模拟(示例代码) 2019-04-21
android组件动态接收数据库,Android开发——fragment中数据传递与刷新UI(更改控件)... 2019-04-21
云麦小米华为体脂秤怎么样_云康宝和华为智能体脂秤对比评测,实际体验告诉你哪款更好... 2019-04-21
linux 条件判断 取非_Linux awk 系列文章之 awk 多重条件判断 2019-04-21
c语言中如何将字符串的元素一个一个取出_C语言中常用的字符串操作函数 2019-04-21
2d游戏地图编辑器_王者荣耀:新版本爆料!地图编辑器“天工”即将开测,游戏怎么玩由你定!... 2019-04-21
.net framework服务启动后停止_dos命令net图文教程,start启动系统服务stop停止服务批处理脚本... 2019-04-21
8k分辨率需要多大带宽_超乎想象!用RTX3080显卡连索尼8K电视玩游戏感受如何?... 2019-04-21
win10怎么开启aptx_Win10未来的黑科技?微软SurfaceFleet大曝光 2019-04-21
creo视图管理器使用方法_学以致用之中望3D—浅谈使用中望3D的初步感受 2019-04-21
周育如的音标口诀大全_花鸟画口诀大全,实用! 2019-04-21
心电图计算心率公式_医学常用的计算公式口诀(内外妇儿),赶快收藏! 2019-04-21
select 移动端 第一个无法选中_Python爬虫微博(移动端)评论 2019-04-21
华为云welink成像是反的_华为发布智能办公神器WeLink,可连接会议室开会,还可一键遥控报销和智能翻译... 2019-04-21
唱好铁血丹心谐音正规_趙贤典:打好“感情牌” 唱好“大合唱” 2019-04-21
aix系统vi修改命令_Linux基础知识必备:利用vi编辑器创建和编辑正文文件 2019-04-21
天涯明月刀开发_玩家被天涯明月刀手游“冷落”?六大门派角色竟不带正眼看人... 2019-04-21