cocos2d-x的初步连连看一

这篇文章中,我们将简单的做一个连连看的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

[cpp]  view plain copy
  1. typedef enum {  
  2.       
  3.     kRight,  
  4.     kLeft  
  5.       
  6.       
  7. }boatDirection;  
  8.   
  9.   
  10. typedef enum {  
  11.       
  12.     birdsRight,  
  13.     birdsLeft  
  14.       
  15.       
  16. }birdsDirection;  
  17.   
  18. class HelloWorld : public cocos2d::CCLayer  
  19. {  
  20. public:  
  21.     // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)  
  22.     virtual bool init();  
  23.   
  24.     // there's no 'id' in cpp, so we recommend to return the class instance pointer  
  25.     static cocos2d::CCScene* scene();  
  26.   
  27.   
  28.     // preprocessor macro for "static create()" constructor ( node() deprecated )  
  29.     CREATE_FUNC(HelloWorld);  
  30.       
  31.       
  32. private:  
  33.       
  34.       
  35.     void initUI(void);  
  36.       
  37.     void initData(void);  
  38.       
  39.     void initSounds(void);  
  40.       
  41.     void addBoat(void);  
  42.       
  43.     void resetSpritePos(void);  
  44.       
  45.     void wavesAnimation(void);  
  46.       
  47.     void wavesAnimation2(void);  
  48.       
  49.     void wavesAnimation3(void);  
  50.       
  51.     void birdsselfAnimation(void);  
  52.       
  53.     void birdsselfAnimation2(void);  
  54.       
  55.     void resetBirdsSpritePos(void);  
  56.       
  57.     void playBack(void);  
  58.       
  59.     cocos2d::CCSprite *cloudSprite;  
  60.     cocos2d::CCSprite *cloud2Sprite;  
  61.     cocos2d::CCSprite *boatSprite;  
  62.     cocos2d::CCSprite *wavesSprite;  
  63.     cocos2d::CCSprite *birdsSprite;  
  64.       
  65.       
  66.     boatDirection _boatDirection;  
  67.       
  68.     birdsDirection _birdsDirection;  
  69.       
  70.       
  71.       
  72.     float boatMoveTime;  
  73.   
  74.       
  75. };  
HelloWorldScene.cpp

[cpp]  view plain copy
  1. bool HelloWorld::init()  
  2. {  
  3.     //  
  4.     // 1. super init first  
  5.     if ( !CCLayer::init() )  
  6.     {  
  7.         return false;  
  8.     }  
  9.   
  10.       
  11.       
  12.       
  13.       
  14.     this->initData();  
  15.       
  16.     this->initSounds();  
  17.       
  18.     this->initUI();  
  19.       
  20.       
  21.   
  22.       
  23.     return true;  
  24. }  
  25.   
  26.   
  27.   
  28.   
  29. void HelloWorld::initData()  
  30. {  
  31.       
  32.       
  33.     CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sailing_boat-hd.plist");  
  34.     CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("seagull-hd.plist");  
  35.   
  36.       
  37.     boatMoveTime=45.0f;  
  38.   
  39.       
  40.     _boatDirection=kLeft;  
  41.     _birdsDirection=birdsLeft;  
  42.       
  43.   
  44. }  
  45.   
  46.   
  47. //音频文件初始化  
  48. void HelloWorld::initSounds()  
  49. {  
  50.   
  51.   
  52.       
  53.       
  54.       
  55.   
  56.       
  57.   
  58. }  
  59.   
  60.   
  61. //UI界面的初始化  
  62. void HelloWorld::initUI()  
  63. {  
  64.   
  65.     CCSprite *bgSprite=NULL;  
  66.       
  67.       
  68.     if (wSize.width==1136 && wSize.height==640)  
  69.     {  
  70.           
  71.           
  72.         bgSprite=CCSprite::create("background_568-hd.png");  
  73.       
  74.           
  75.           
  76.     }  
  77.     else  
  78.     {  
  79.       
  80.       
  81.          bgSprite=CCSprite::create("background-hd.png");  
  82.       
  83.       
  84.       
  85.     }  
  86.       
  87.     bgSprite->setPosition(ccp(wSize.width/2, wSize.height/2));  
  88.     this->addChild(bgSprite, 0);  
  89.       
  90.       
  91.       
  92.     //白云1  
  93.     cloudSprite=CCSprite::create("background_cloud_1-hd.png");  
  94.     cloudSprite->setAnchorPoint(ccp(0, 0));  
  95.     cloudSprite->setPosition(ccp(0, wSize.height-cloudSprite->getContentSize().height));  
  96.     this->addChild(cloudSprite, 1);  
  97.       
  98.       
  99.     //白云倒影  
  100.     CCSprite *daoyingloudSprite=CCSprite::create("background_cloud_1-hd.png");  
  101.     daoyingloudSprite->setAnchorPoint(ccp(0, 0));  
  102.     //垂直翻转  
  103.     daoyingloudSprite->setFlipY(true);  
  104.     daoyingloudSprite->setOpacity(40);  
  105.     daoyingloudSprite->setPosition(ccp(wSize.width-cloudSprite->getContentSize().width-40, wSize.height-cloudSprite->getContentSize().height-78*2));  
  106.     this->addChild(daoyingloudSprite, 1);  
  107.   
  108.     //白云2  
  109.     cloud2Sprite=CCSprite::create("background_cloud_2-hd.png");  
  110.     cloud2Sprite->setAnchorPoint(ccp(0, 0));  
  111.     cloud2Sprite->setPosition(ccp(cloudSprite->getPosition().x+cloudSprite->getContentSize().width, wSize.height-cloud2Sprite->getContentSize().height));  
  112.     this->addChild(cloud2Sprite, 1);  
  113.   
  114.     //岛  
  115.     CCSprite *landSprite=CCSprite::create("island-hd.png");  
  116.     landSprite->setAnchorPoint(ccp(0, 0));  
  117.     landSprite->setPosition(ccp(wSize.width-landSprite->getContentSize().width-20*2, wSize.height-landSprite->getContentSize().height-47*2));  
  118.     this->addChild(landSprite, 1);  
  119.       
  120.     //倒影  
  121.     CCSprite *daoyinglandSprite=CCSprite::create("island-hd.png");  
  122.     daoyinglandSprite->setAnchorPoint(ccp(0, 0));  
  123.     daoyinglandSprite->setFlipY(true);  
  124.     daoyinglandSprite->setOpacity(40);  
  125.     daoyinglandSprite->setPosition(ccp(wSize.width-landSprite->getContentSize().width-20*2, wSize.height-landSprite->getContentSize().height-78*2));  
  126.     this->addChild(daoyinglandSprite, 1);  
  127.   
  128.     //船  
  129.     boatSprite=CCSprite::createWithSpriteFrameName("sailing_boat1.png");  
  130.     boatSprite->setPosition(ccp(wSize.width-boatSprite->getContentSize().width-50*2,230*2));  
  131.     this->addChild(boatSprite, 1);  
  132.       
  133.     this->addBoat();  
  134.   
  135.   
  136.     //沙滩  
  137.     CCSprite *shatanSprite=CCSprite::create("beach_adornment-hd.png");  
  138.       
  139.     shatanSprite->setPosition(ccp(wSize.width/2, shatanSprite->getContentSize().height/2));  
  140.       
  141.     this->addChild(shatanSprite, 1);  
  142.       
  143.       
  144.       
  145.     if (wSize.width==1136 && wSize.height==640)  
  146.     {  
  147.           
  148.           
  149.         wavesSprite=CCSprite::create("background_sea1_568-hd.png");  
  150.           
  151.           
  152.           
  153.     }  
  154.     else  
  155.     {  
  156.           
  157.           
  158.         wavesSprite=CCSprite::create("background_sea1-hd.png");  
  159.           
  160.           
  161.           
  162.     }  
  163.     wavesSprite->setPosition(ccp(wSize.width/2, 140*2));  
  164.     wavesSprite->setOpacity(0);  
  165.     this->addChild(wavesSprite, 1);  
  166.   
  167.       
  168.     this->schedule(schedule_selector(HelloWorld::wavesAnimation), 10);  
  169.       
  170.     //鸟  
  171.     birdsSprite=CCSprite::createWithSpriteFrameName("seagull1.png");  
  172.     birdsSprite->setAnchorPoint(ccp(0, 0));  
  173.     birdsSprite->setPosition(ccp(200*2,270*2));  
  174.     birdsSprite->setScale(0.7);  
  175.     this->addChild(birdsSprite, 1);  
  176.       
  177.     this->schedule(schedule_selector(HelloWorld::birdsselfAnimation), 5);  
  178.   
  179.       
  180.     this->birdsselfAnimation2();  
  181.       
  182.       
  183. //    //开始菜单  
  184. //    CCSprite *playSprite=CCSprite::create("button_play-hd.png");  
  185. //    CCSprite *playSprite_s=CCSprite::create("button_play_s-hd.png");  
  186. //      
  187. //    CCMenuItemSprite *item=CCMenuItemSprite::create(playSprite, playSprite_s, this, menu_selector(HelloWorld::playBack));  
  188. //      
  189. //      
  190. //    CCMenu *menu=CCMenu::create(item,NULL);  
  191. //      
  192. //    item->setAnchorPoint(ccp(0.5, 0.5));  
  193. //    menu->setPosition(ccp(wSize.width/2,110*2));  
  194. //      
  195. //      
  196. //    this->addChild(menu, 1);  
  197.   
  198.       
  199.       
  200.       
  201. }  
  202.   
  203.   
  204. //船自身的动画  
  205. void HelloWorld::addBoat()  
  206. {  
  207.   
  208.   
  209.     boatSprite->setFlipX(false);  
  210.       
  211.     CCArray *array=CCArray::create();  
  212.       
  213.     for (int i = 1; i < 4; i++)  
  214.     {  
  215.           
  216.         CCString *string=CCString::createWithFormat("sailing_boat%d.png");  
  217.           
  218.         CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());  
  219.           
  220.         array->addObject(frame);  
  221.   
  222.     }  
  223.       
  224.     CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.5);  
  225.     CCAnimate *animate=CCAnimate::create(animation);  
  226.       
  227.     CCRepeatForever *ac1=CCRepeatForever::create(animate);  
  228.       
  229.     boatSprite->runAction(ac1);  
  230.       
  231.     array->removeAllObjects();  
  232.       
  233.     //船移动动画,到两边自动返回  
  234.     CCMoveTo *ac2;  
  235.       
  236.     switch (_boatDirection)  
  237.     {  
  238.         case kLeft:  
  239.               
  240.         
  241.             ac2=CCMoveTo::create(boatMoveTime, CCPointMake(-boatSprite->getContentSize().width, boatSprite->getPosition().y));  
  242.             _boatDirection=kRight;  
  243.               
  244.             boatSprite->setFlipX(false);  
  245.               
  246.             break;  
  247.               
  248.         case kRight:  
  249.               
  250.             ac2=CCMoveTo::create(boatMoveTime, CCPointMake(wSize.width, boatSprite->getPosition().y));  
  251.             _boatDirection=kLeft;  
  252.               
  253.             boatSprite->setFlipX(true);  
  254.               
  255.             break;  
  256.               
  257.         default:  
  258.               
  259.               
  260.             break;  
  261.               
  262.     }  
  263.      
  264.     CCSequence *seq=CCSequence::create(ac2,CCCallFunc::create(this, callfunc_selector(HelloWorld::resetSpritePos)),NULL);  
  265.       
  266.     boatSprite->runAction(seq);  
  267.       
  268.       
  269.   
  270.   
  271. }  
  272.   
  273. //重新设置船的位置  
  274. void HelloWorld::resetSpritePos()  
  275. {  
  276.   
  277.   
  278.   
  279.     this->addBoat();  
  280.   
  281.   
  282. }  
  283.   
  284.   
  285. //海浪动画  
  286. void HelloWorld::wavesAnimation()  
  287. {  
  288.   
  289.   
  290.     SimpleAudioEngine::sharedEngine()->playEffect("14.wav");  
  291.       
  292.       
  293.   
  294.     CCMoveTo *ac1=CCMoveTo::create(4.0f, CCPointMake(wavesSprite->getPosition().x, 100*2));  
  295.       
  296.     CCFadeIn *ac2=CCFadeIn::create(4.0f);  
  297.       
  298.     //同时进行  
  299.     CCSpawn *spawn=CCSpawn::create(ac1,ac2,NULL);  
  300.   
  301.     //队列  
  302.     CCSequence *seq=CCSequence::create(spawn,CCCallFunc::create(this, callfunc_selector(HelloWorld::wavesAnimation2)),NULL);  
  303.   
  304.     wavesSprite->runAction(seq);  
  305.   
  306.       
  307.       
  308. }  
  309.   
  310. void HelloWorld::wavesAnimation2()  
  311. {  
  312.   
  313.   
  314.     CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage("background_sea2_568-hd.png");  
  315.   
  316.     wavesSprite->setTexture(aTexture);  
  317.   
  318.   
  319.     CCMoveTo *ac1=CCMoveTo::create(4.0f, CCPointMake(wavesSprite->getPosition().x, 140*2));  
  320.       
  321.     CCFadeIn *ac2=CCFadeIn::create(4.0f);  
  322.       
  323.     //同时进行  
  324.     CCSpawn *spawn=CCSpawn::create(ac1,ac2,NULL);  
  325.       
  326.     //队列  
  327.     CCSequence *seq=CCSequence::create(spawn,CCCallFunc::create(this, callfunc_selector(HelloWorld::wavesAnimation3)),NULL);  
  328.       
  329.     wavesSprite->runAction(seq);  
  330.   
  331.       
  332. }  
  333.   
  334.   
  335. void HelloWorld::wavesAnimation3()  
  336. {  
  337.   
  338.   
  339.   
  340.   
  341.     CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage("background_sea1_568-hd.png");  
  342.       
  343.     wavesSprite->setTexture(aTexture);  
  344.       
  345.   
  346.     wavesSprite->setOpacity(0);  
  347.   
  348.   
  349. }  
  350.   
  351.   
  352. //鸟自身的动画  
  353. void HelloWorld::birdsselfAnimation()  
  354. {  
  355.   
  356.       
  357.     SimpleAudioEngine::sharedEngine()->playEffect("15.wav");  
  358.   
  359.     CCArray *array=CCArray::create();  
  360.       
  361.     for (int i = 1; i < 4; i++)  
  362.     {  
  363.           
  364.         CCString *string=CCString::createWithFormat("seagull%d.png");  
  365.           
  366.         CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());  
  367.           
  368.         array->addObject(frame);  
  369.           
  370.     }  
  371.       
  372.     CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.2);  
  373.       
  374.     animation->setRestoreOriginalFrame(true);  
  375.       
  376.     CCAnimate *animate=CCAnimate::create(animation);  
  377.       
  378.     int times=arc4random()%5+1;  
  379.       
  380.     CCRepeat *ac1=CCRepeat::create(animate, times);  
  381.       
  382.     birdsSprite->runAction(ac1);  
  383.       
  384.     array->removeAllObjects();  
  385.   
  386.   
  387.   
  388. }  
  389.   
  390. //鸟飞翔的动画  
  391. void HelloWorld::birdsselfAnimation2()  
  392. {  
  393.   
  394.       
  395.      float yoffset=arc4random()%40+1;  
  396.   
  397.       
  398.     CCPoint birdsPos=CCPointMake(-birdsSprite->getContentSize().width, 270*2+yoffset);  
  399.       
  400.     CCMoveTo *ac2;  
  401.       
  402.     switch (_birdsDirection) {  
  403.         case birdsLeft:  
  404.               
  405.             ac2=CCMoveTo::create(35.0f, CCPointMake(birdsPos.x, birdsPos.y));  
  406.             _birdsDirection=birdsRight;  
  407.               
  408.             birdsSprite->setFlipX(false);  
  409.             break;  
  410.               
  411.         case birdsRight:  
  412.               
  413.             ac2=CCMoveTo::create(35.0f, CCPointMake(wSize.width, 270*2+yoffset));  
  414.             _birdsDirection=birdsLeft;  
  415.               
  416.             birdsSprite->setFlipX(true);  
  417.   
  418.             break;  
  419.               
  420.         default:  
  421.             break;  
  422.               
  423.     }  
  424.   
  425.       
  426.       
  427.     CCSpawn *spawn=CCSpawn::create(ac2,NULL);  
  428.       
  429.     CCSequence *seq=CCSequence::create(spawn,CCCallFunc::create(this, callfunc_selector(HelloWorld::resetBirdsSpritePos)),NULL);  
  430.       
  431.     birdsSprite->runAction(seq);  
  432.   
  433.       
  434.       
  435.   
  436. }  
  437.   
  438.   
  439.   
  440. void HelloWorld::resetBirdsSpritePos()  
  441. {  
  442.   
  443.   
  444.   
  445.       
  446.     this->birdsselfAnimation2();  
  447.   
  448.   
  449.   
  450. }  

上面中,我们初始化了一些界面元素,然后做了一些简单的动画效果,整体上都是很简单的,这里就不在多说了,



~~~~~~~~

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

[cpp]  view plain copy
  1. //开始菜单  
  2.    CCSprite *playSprite=CCSprite::create("button_play-hd.png");  
  3.    CCSprite *playSprite_s=CCSprite::create("button_play_s-hd.png");  
  4.      
  5.    CCMenuItemSprite *item=CCMenuItemSprite::create(playSprite, playSprite_s, this, menu_selector(HelloWorld::playBack));  
  6.      
  7.      
  8.    CCMenu *menu=CCMenu::create(item,NULL);  
  9.      
  10.    item->setAnchorPoint(ccp(0.5, 0.5));  
  11.    menu->setPosition(ccp(wSize.width/2,110*2));  
  12.      
  13.      
  14.    this->addChild(menu, 1);  

[cpp]  view plain copy
  1. //开始游戏  
  2. void HelloWorld::playBack()  
  3. {  
  4.   
  5.   
  6.   
  7.   
  8.   
  9.   
  10. }  

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




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

这篇文章中,我们将完成剩下的部分,点击首界面进入游戏主界面,我们初始化一些界面元素,实现这样的功能,有暂停功能,背景音乐,进度条等,加上一些动画,下面直接上代码,前面都是一些界面的布局和动画等,比较简单。先新建一个GameLayer类

GameLayer.h

[cpp]  view plain copy
  1. //游戏主界面  
  2. class GameLayer : public cocos2d::CCLayerColor  
  3. {  
  4.   
  5. public:  
  6.       
  7.     virtual bool init();  
  8.   
  9.     static cocos2d::CCScene* scene();  
  10.   
  11.       
  12.     CREATE_FUNC(GameLayer);  
  13.   
  14.   
  15.       
  16. private:  
  17.       
  18.     void initData(void);  
  19.     void initUI(void);  
  20.     void initLoadingUI(void);  
  21.     void playSound1(void);  
  22.     void playSound2(void);  
  23.     void playSound3(void);  
  24.     void initDialog(void);  
  25.       
  26.     void pausePressed(void);  
  27.       
  28.     void dialogAnimaton(void);  
  29.       
  30.     void dialogAnimaton2(void);  
  31.       
  32.     void dialogAnimaton3(void);  
  33.       
  34.     void removeDialogLayer(void);  
  35.       
  36.     void loadingAnimation(void);  
  37.       
  38.     void removeLoadSprite(void);  
  39.       
  40.     void functionSpriteAnimation(cocos2d::CCPoint point1,cocos2d::CCPoint point2);  
  41.       
  42.     void functionPauseSpriteAnimation(cocos2d::CCPoint point1,cocos2d::CCPoint point2);  
  43.       
  44.     CCLayerColor *dialogLayer;  
  45.       
  46.       
  47.     cocos2d::CCString *dialogLevelString;  
  48.     cocos2d::CCString *dialogTishiString;  
  49.       
  50.     cocos2d::CCSprite *leisureSprite;  
  51.     cocos2d::CCSprite *levelSprite;  
  52.       
  53.     cocos2d::CCLabelAtlas *levelnumsAtlas;  
  54.       
  55.     cocos2d::CCString *_level;  
  56.       
  57.     cocos2d::CCSprite *pauseSprite;  
  58.       
  59.     cocos2d::CCMenu *pausemenu;  
  60.       
  61.     cocos2d::CCSprite *loadSprite;  
  62.       
  63. };  

GameLayer.cpp

[cpp]  view plain copy
  1. CCScene* GameLayer::scene()  
  2. {  
  3.   
  4.     CCScene *scene = CCScene::create();  
  5.       
  6.     GameLayer *layer = GameLayer::create();  
  7.       
  8.     scene->addChild(layer);  
  9.       
  10.     return scene;  
  11.       
  12.   
  13. }  
  14.   
  15.   
  16. bool GameLayer::init()  
  17. {  
  18.       
  19.     if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)))  
  20.     {  
  21.         return false;  
  22.     }  
  23.       
  24.       
  25.       
  26.     
  27.       
  28.     this->initLoadingUI();  
  29.       
  30.     //循环播放三首背景音乐  
  31.     this->playSound1();  
  32.       
  33.       
  34.     return true;  
  35.   
  36. }  
  37.   
  38.   
  39. void GameLayer::initData()  
  40. {  
  41.   
  42.       
  43.     dialogLevelString=CCString::create("第一关");  
  44.     dialogTishiString=CCString::create("王子大人");  
  45.   //  dialogLevelString->retain();  
  46.    // dialogTishiString->retain();  
  47.     _level=CCString::create("001");  
  48.   
  49. }  
  50.   
  51. //load界面  
  52. void GameLayer::initLoadingUI()  
  53. {  
  54.   
  55.   
  56.     loadSprite=CCSprite::create("loading_logo-hd.png");  
  57.       
  58.     loadSprite->setPosition(ccp(wSize.width/2, wSize.height/2));  
  59.   
  60.   
  61.     this->addChild(loadSprite, 0);  
  62.       
  63.   
  64.     this->loadingAnimation();  
  65.   
  66. }  
  67.   
  68.   
  69. void GameLayer::initUI()  
  70. {  
  71.   
  72.     CCSprite *bgSprite=NULL;  
  73.       
  74.       
  75.     if (wSize.width==1136 && wSize.height==640)  
  76.     {  
  77.           
  78.           
  79.         bgSprite=CCSprite::create("background_568-hd.png");  
  80.           
  81.           
  82.           
  83.     }  
  84.     else  
  85.     {  
  86.           
  87.           
  88.         bgSprite=CCSprite::create("background-hd.png");  
  89.           
  90.           
  91.           
  92.     }  
  93.       
  94.     bgSprite->setPosition(ccp(wSize.width/2, wSize.height/2));  
  95.     this->addChild(bgSprite, 0);  
  96.       
  97.       
  98.       
  99.     //白云1  
  100.     CCSprite *cloudSprite=CCSprite::create("background_cloud_1-hd.png");  
  101.     cloudSprite->setAnchorPoint(ccp(0, 0));  
  102.     cloudSprite->setPosition(ccp(0, wSize.height-cloudSprite->getContentSize().height));  
  103.     this->addChild(cloudSprite, 1);  
  104.       
  105.       
  106.     //白云倒影  
  107.     CCSprite *daoyingloudSprite=CCSprite::create("background_cloud_1-hd.png");  
  108.     daoyingloudSprite->setAnchorPoint(ccp(0, 0));  
  109.     //垂直翻转  
  110.     daoyingloudSprite->setFlipY(true);  
  111.     daoyingloudSprite->setOpacity(40);  
  112.     daoyingloudSprite->setPosition(ccp(wSize.width-cloudSprite->getContentSize().width-40, wSize.height-cloudSprite->getContentSize().height-78*2));  
  113.     this->addChild(daoyingloudSprite, 1);  
  114.       
  115.     //白云2  
  116.     CCSprite *cloud2Sprite=CCSprite::create("background_cloud_2-hd.png");  
  117.     cloud2Sprite->setAnchorPoint(ccp(0, 0));  
  118.     cloud2Sprite->setPosition(ccp(cloudSprite->getPosition().x+cloudSprite->getContentSize().width, wSize.height-cloud2Sprite->getContentSize().height));  
  119.     this->addChild(cloud2Sprite, 1);  
  120.       
  121.     //岛  
  122.     CCSprite *landSprite=CCSprite::create("island-hd.png");  
  123.     landSprite->setAnchorPoint(ccp(0, 0));  
  124.     landSprite->setPosition(ccp(wSize.width-landSprite->getContentSize().width-20*2, wSize.height-landSprite->getContentSize().height-47*2));  
  125.     this->addChild(landSprite, 1);  
  126.       
  127.     //倒影  
  128.     CCSprite *daoyinglandSprite=CCSprite::create("island-hd.png");  
  129.     daoyinglandSprite->setAnchorPoint(ccp(0, 0));  
  130.     daoyinglandSprite->setFlipY(true);  
  131.     daoyinglandSprite->setOpacity(40);  
  132.     daoyinglandSprite->setPosition(ccp(wSize.width-landSprite->getContentSize().width-20*2, wSize.height-landSprite->getContentSize().height-78*2));  
  133.     this->addChild(daoyinglandSprite, 1);  
  134.   
  135.     this->initDialog();  
  136.       
  137.     //图标1  
  138.     leisureSprite=CCSprite::create("game_leisure_logo-hd.png");  
  139.     leisureSprite->setPosition(ccp(leisureSprite->getContentSize().width/2+10*2, wSize.height+leisureSprite->getContentSize().height/2));  
  140.       
  141.     this->addChild(leisureSprite, 1);  
  142.       
  143.     //等级图标  
  144.     levelSprite=CCSprite::create("orange_font_level-hd.png");  
  145.     levelSprite->setPosition(ccp(leisureSprite->getContentSize().width/2+8*2, 255*2));  
  146.       
  147.     this->addChild(levelSprite, 1);  
  148.     levelSprite->setVisible(false);  
  149.       
  150.       
  151.     //等级sprite  
  152.     levelnumsAtlas=CCLabelAtlas::create(_level->getCString(), "small_blue_number_level-hd.png",16, 20, '0');  
  153.       
  154.     levelnumsAtlas->setScale(1.2);  
  155.     levelnumsAtlas->setPosition(ccp(leisureSprite->getContentSize().width/2-6*2,levelSprite->getPosition().y-20*2));  
  156.     levelnumsAtlas->setVisible(false);  
  157.       
  158.     this->addChild(levelnumsAtlas, 1);  
  159.       
  160.     //暂停菜单  
  161.     pauseSprite=CCSprite::create("button_pause-hd.png");  
  162.       
  163.     CCSprite *pauseSprite_s=CCSprite::create("button_pause-hd.png");  
  164.       
  165.     CCMenuItemSprite *item=CCMenuItemSprite::create(pauseSprite, pauseSprite_s, this, menu_selector(GameLayer::pausePressed));  
  166.       
  167.     pausemenu=CCMenu::create(item,NULL);  
  168.       
  169.     item->setAnchorPoint(ccp(0.5, 0.5));  
  170.     pausemenu->setPosition(ccp(pauseSprite->getContentSize().width/2+10*2,-pauseSprite->getContentSize().height/2));  
  171.   
  172.       
  173.     this->addChild(pausemenu, 1);  
  174.       
  175.     this->dialogAnimaton();  
  176.       
  177.       
  178. }  
  179.   
  180.   
  181. //初始化弹出框  
  182. void GameLayer::initDialog()  
  183. {  
  184.   
  185.     //弹出框  
  186.     dialogLayer=CCLayerColor::create(ccc4(0, 0, 0, 0), 333*2, 235*2);  
  187.       
  188.     dialogLayer->setPosition(ccp(wSize.width/2-dialogLayer->getContentSize().width/2, wSize.height/2-dialogLayer->getContentSize().height/2));  
  189.       
  190.     dialogLayer->setScale(0.9);  
  191.     this->addChild(dialogLayer, 1);  
  192.       
  193.       
  194.     CCSprite *dialogSprite=CCSprite::create("game_dialog-hd.png");  
  195.     dialogSprite->setAnchorPoint(ccp(0, 0));  
  196.     dialogSprite->setPosition(ccp(0, 0));  
  197.       
  198.     dialogLayer->addChild(dialogSprite, 1);  
  199.       
  200.     //标题1  
  201.     CCLabelTTF *dialoglevel=CCLabelTTF::create(dialogLevelString->getCString(), "Marker Felt", 35);  
  202.     dialoglevel->setAnchorPoint(ccp(0, 0));  
  203.     dialoglevel->setPosition(ccp(dialogLayer->getContentSize().width/2-dialoglevel->getContentSize().width/2, dialogLayer->getContentSize().height/2-dialoglevel->getContentSize().height/2+30*2));  
  204.     dialoglevel->setColor(ccWHITE);  
  205.     dialogLayer->addChild(dialoglevel, 1, 310);  
  206.   
  207.     //标题2  
  208.     CCLabelTTF *dialogtishi=CCLabelTTF::create(dialogTishiString->getCString(), "Marker Felt", 35);  
  209.     dialogtishi->setAnchorPoint(ccp(0, 0));  
  210.     dialogtishi->setPosition(ccp(dialogLayer->getContentSize().width/2-dialogtishi->getContentSize().width/2, dialogLayer->getContentSize().height/2-dialogtishi->getContentSize().height/2-15*2));  
  211.     dialogtishi->setColor(ccWHITE);  
  212.     dialogLayer->addChild(dialogtishi, 1, 311);  
  213.   
  214.       
  215.       
  216.   
  217. }  
  218.   
  219.   
  220. //load界面动画  
  221. void GameLayer::loadingAnimation()  
  222. {  
  223.       
  224.     CCFadeOut *ac1=CCFadeOut::create(3.0f);  
  225.       
  226.     CCSequence *seq=CCSequence::create(ac1,CCCallFunc::create(this, callfunc_selector(GameLayer::removeLoadSprite)),NULL);  
  227.   
  228.       
  229.     loadSprite->runAction(seq);  
  230.   
  231.   
  232.   
  233. }  
  234.   
  235. //移除load界面  
  236. void GameLayer::removeLoadSprite()  
  237. {  
  238.   
  239.     loadSprite->removeFromParentAndCleanup(true);  
  240.       
  241.     this->initData();  
  242.     this->initUI();  
  243.   
  244.   
  245. }  
  246.   
  247.   
  248. void GameLayer::playSound1()  
  249. {  
  250.   
  251.     SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();  
  252.       
  253.     SimpleAudioEngine::sharedEngine()->playBackgroundMusic("Back2new.mp3"false);  
  254.       
  255.   
  256.     this->scheduleOnce(schedule_selector(GameLayer::playSound2), 60);  
  257.   
  258.   
  259.       
  260. }  
  261.   
  262.   
  263. void GameLayer::playSound2()  
  264. {  
  265.   
  266.   
  267.     SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();  
  268.       
  269.     SimpleAudioEngine::sharedEngine()->playBackgroundMusic("Back3new.mp3"false);  
  270.   
  271.   
  272.     this->scheduleOnce(schedule_selector(GameLayer::playSound3), 62);  
  273.       
  274.   
  275.       
  276.   
  277. }  
  278.   
  279. void GameLayer::playSound3()  
  280. {  
  281.       
  282.       
  283.     SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();  
  284.       
  285.     SimpleAudioEngine::sharedEngine()->playBackgroundMusic("MainMenu.mp3"false);  
  286.       
  287.       
  288.     this->scheduleOnce(schedule_selector(GameLayer::playSound1), 65);  
  289.       
  290.       
  291.       
  292. }  
  293.   
  294.   
  295. //暂停  
  296. void GameLayer::pausePressed()  
  297. {  
  298.   
  299.       
  300.       
  301.       
  302.       
  303.       
  304.   
  305.   
  306. }  
  307.   
  308.   
  309. //弹出框动画  
  310. void GameLayer::dialogAnimaton()  
  311. {  
  312.   
  313.   
  314.     SimpleAudioEngine::sharedEngine()->playEffect("19.wav");  
  315.       
  316.     CCShow *ac1=CCShow::create();  
  317.     CCScaleTo *ac2=CCScaleTo::create(0.1f, 1.0);  
  318.     CCSpawn *spwan=CCSpawn::create(ac1,ac2,NULL);  
  319.     CCSequence *seq=CCSequence::create(spwan,CCCallFunc::create(this, callfunc_selector(GameLayer::dialogAnimaton2)),NULL);  
  320.   
  321.       
  322.     dialogLayer->runAction(seq);  
  323.       
  324.   
  325. }  
  326.   
  327. void GameLayer::dialogAnimaton2()  
  328. {  
  329.   
  330.       
  331.     CCScaleTo *ac1=CCScaleTo::create(0.1f, 0.9);  
  332.   
  333.       
  334.     CCSequence *seq=CCSequence::create(ac1,CCCallFunc::create(this, callfunc_selector(GameLayer::dialogAnimaton3)),NULL);  
  335.   
  336.   
  337.     dialogLayer->runAction(seq);  
  338.   
  339.   
  340. }  
  341.   
  342. void GameLayer::dialogAnimaton3()  
  343. {  
  344.       
  345.   
  346.     CCScaleTo *ac1=CCScaleTo::create(0.1f, 1.0);  
  347.     CCScaleTo *ac2=CCScaleTo::create(0.2f, 1.2);  
  348.     CCFadeTo *ac3=CCFadeTo::create(0.2f, 0);  
  349.     CCSpawn *spwan=CCSpawn::create(ac2,ac3,NULL);  
  350.       
  351.       
  352.     CCSequence *seq=CCSequence::create(ac1,CCDelayTime::create(1.5f),spwan,CCCallFunc::create(this, callfunc_selector(GameLayer::removeDialogLayer)),NULL);  
  353.       
  354.       
  355.     dialogLayer->runAction(seq);  
  356.       
  357.       
  358.       
  359. }  
  360.   
  361.   
  362. //移除弹出框  
  363. void GameLayer::removeDialogLayer()  
  364. {  
  365.   
  366.   
  367.     SimpleAudioEngine::sharedEngine()->playEffect("16.wav");  
  368.   
  369.   
  370.     dialogLayer->removeFromParentAndCleanup(true);  
  371.       
  372.     levelSprite->setVisible(true);  
  373.     levelnumsAtlas->setVisible(true);  
  374.       
  375.       
  376.       
  377.     this->functionSpriteAnimation(CCPoint(leisureSprite->getPosition().x, wSize.height-leisureSprite->getContentSize().height/2-15*2) ,CCPoint(leisureSprite->getPosition().x, wSize.height-leisureSprite->getContentSize().height/2-10*2));  
  378.       
  379.     this->functionPauseSpriteAnimation(CCPoint(pausemenu->getPosition().x, 34*2), CCPoint(pausemenu->getPosition().x, 29*2));  
  380.       
  381.       
  382.       
  383.       
  384. }  
  385.   
  386.   
  387. //移动动画  
  388. void GameLayer::functionSpriteAnimation(cocos2d::CCPoint point1, cocos2d::CCPoint point2)  
  389. {  
  390.   
  391.   
  392.     CCMoveTo *ac1=CCMoveTo::create(0.15, point1);  
  393.     CCMoveTo *ac2=CCMoveTo::create(0.15, point2);  
  394.       
  395.   
  396.     CCSequence *seq=CCSequence::create(ac1,ac2,NULL);  
  397.       
  398.     leisureSprite->runAction(seq);  
  399.   
  400. }  
  401.   
  402. 移动动画  
  403. void GameLayer::functionPauseSpriteAnimation(cocos2d::CCPoint point1, cocos2d::CCPoint point2)  
  404. {  
  405.   
  406.   
  407.   
  408.     CCMoveTo *ac1=CCMoveTo::create(0.15, point1);  
  409.     CCMoveTo *ac2=CCMoveTo::create(0.15, point2);  
  410.       
  411.       
  412.     CCSequence *seq=CCSequence::create(ac1,ac2,NULL);  
  413.       
  414.       
  415.     pausemenu->runAction(seq);  
  416.   
  417. }  


场景切换动画

[cpp]  view plain copy
  1. //开始游戏  
  2. void HelloWorld::playBack()  
  3. {  
  4.   
  5.     SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();  
  6.       
  7.     CCScene *scene=GameLayer::scene();  
  8.       
  9.     //旋转动画  
  10.     CCDirector::sharedDirector()->replaceScene(CCTransitionRotoZoom::create(1.2f, scene));  
  11.       
  12.   
  13.   
  14. }  



以前就是游戏主界面的一些元素布局,跳转进入游戏界面,先出现一个loading界面,加个渐隐的动画,消失后弹出一个关卡提示框,。。。。

效果如下:







~~~~~~~~~接下来,我们加入一个时间进度条,O(∩_∩)O~~~~~~~

在函数initUI()中加入

[cpp]  view plain copy
  1. //进度条背景  
  2.     progressbgSprite=CCSprite::create("time_slot-hd.png");  
  3.       
  4.     progressbgSprite->setAnchorPoint(ccp(0, 0));  
  5.     progressbgSprite->setPosition(ccp(130, wSize.height-55));  
  6.       
  7.     this->addChild(progressbgSprite, 1);  
  8.       
  9.     progressbgSprite->setVisible(false);  
  10.       
  11.       
  12.     CCSprite *progressSprite=CCSprite::create("time_bars-hd.png");  
  13.     //进度条  
  14.     progress=CCProgressTimer::create(progressSprite);  
  15.     progress->setAnchorPoint(ccp(0, 0));  
  16.     progress->setType(kCCProgressTimerTypeBar);  
  17.       
  18.     progress->setPosition(ccp(130, wSize.height-55));  
  19.       
  20.     //进度动画运动方向,可以多试几个值,看看效果  
  21.     progress->setMidpoint(ccp(0, 0));  
  22.       
  23.     //进度条宽高变化  
  24.     progress->setBarChangeRate(ccp(1, 0));  
  25.       
  26.     progress->setPercentage(100);  
  27.       
  28.     this->addChild(progress, 1);  
  29.       
  30.     progress->setVisible(false);  
  31.       
  32.       
  33.     numsTTF=CCLabelTTF::create("100""Thonburi", 24);  
  34.       
  35.     numsTTF->setAnchorPoint(ccp(0, 0));  
  36.     numsTTF->setPosition(ccp(400, wSize.height-55));  
  37.     numsTTF->setColor(ccBLACK);  
  38.       
  39.     this->addChild(numsTTF, 1);  
  40.       
  41.     numsTTF->setVisible(false);  

定义刷新函数

[cpp]  view plain copy
  1. this->schedule(schedule_selector(GameLayer::update), 1);  

[cpp]  view plain copy
  1. //刷新  
  2. void GameLayer::update()  
  3. {  
  4.   
  5.     int cu=progress->getPercentage();  
  6.       
  7.     cu=cu-1.0f;  
  8.       
  9.     progress->setPercentage(cu);  
  10.       
  11.     CCString *str = CCString::createWithFormat("%d",cu);  
  12.       
  13.     numsTTF->setString(str->getCString());  
  14.   
  15.       
  16. }  

一秒钟刷新一次

效果如下



注意,在我现在用的cocos2dx版本中,在ios下,CCLabelTTF没显示出来,如果你升级到2.1.4以上,就没问题了,如果你不想升级,那么网上的解决方法为修改底层源码:

修改CCImage.mm的

static bool _initWithString(constchar * pText, cocos2d::CCImage::ETextAlign eAlign,const char * pFontName,intnSize, tImageInfo* pInfo)


[cpp]  view plain copy
  1. const int _width  = dim.width;  //Add    
  2. const int _height = dim.height;  //Add    
  3.     
  4. unsigned char* data = new unsigned char[(int)(dim.width * dim.height * 4)];    
  5. memset(data, 0, (int)(_width * _height * 4));   //Modify    
  6.     
  7. // draw text    
  8. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();        
  9. CGContextRef context = CGBitmapContextCreate(data, _width, _height, 8, _width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);    
  10. CGColorSpaceRelease(colorSpace);                //Modify    
  11.     
  12. if (! context)    
  13. {    
  14.     delete[] data;    
  15.     break;    
  16. }    

~~~~~~~~~~~~~~~~接下来我们加入一个暂停的界面,并加入一些动画效果

我们新建一个暂停界面的类,PauseLayer,

PauseLayer.h

[cpp]  view plain copy
  1. class PauseLayer :public cocos2d::CCLayerColor  
  2. {  
  3.   
  4.   
  5.       
  6. public:  
  7.       
  8.     virtual bool init();  
  9.       
  10.       
  11.     CREATE_FUNC(PauseLayer);  
  12.       
  13.     cocos2d::CCSprite *pausebgSprite;  
  14.   
  15.     cocos2d::CCMenuItemSprite *backitem;  
  16.     cocos2d::CCMenuItemSprite *startitem;  
  17.       
  18.       
  19.       
  20.     void restartPressed(void);  
  21.   
  22.     void helpPressed(void);  
  23.   
  24.     void musicPressed(void);  
  25.       
  26.       
  27.     void soundPressed(void);  
  28.   
  29.       
  30. private:  
  31.       
  32.     void initUI(void);  
  33.       
  34.   
  35.       
  36.   
  37.   
  38.   
  39. };  

PauseLayer.cpp

[cpp]  view plain copy
  1. bool PauseLayer::init()  
  2. {  
  3.       
  4.     if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 0),343,343))  
  5.     {  
  6.         return false;  
  7.     }  
  8.       
  9.       
  10.       
  11.     this->initUI();  
  12.   
  13.       
  14.       
  15.     return true;  
  16.       
  17. }  
  18.   
  19.   
  20. //UI界面初始化  
  21. void PauseLayer::initUI()  
  22. {  
  23.   
  24.     pausebgSprite=CCSprite::create("game_menu_background-hd.png");  
  25.       
  26.     pausebgSprite->setPosition(ccp(pausebgSprite->getContentSize().width/2, pausebgSprite->getContentSize().height/2));  
  27.       
  28.       
  29.     this->addChild(pausebgSprite, 1);  
  30.       
  31.       
  32.     //返回  
  33.     CCSprite *backSprite=CCSprite::create("button_back-hd.png");  
  34.       
  35.     CCSprite *backSprite_s=CCSprite::create("button_back-hd.png");  
  36.       
  37.     backitem=CCMenuItemSprite::create(backSprite, backSprite_s);  
  38.       
  39.     CCMenu *backmenu=CCMenu::create(backitem,NULL);  
  40.       
  41.     backitem->setAnchorPoint(ccp(0, 0));  
  42.       
  43.     backmenu->setPosition(ccp(71*2,3*2));  
  44.       
  45.     this->addChild(backmenu, 1);  
  46.       
  47.       
  48.     //继续  
  49.     CCSprite *startSprite=CCSprite::create("button_start.png");  
  50.       
  51.     CCSprite *startSprite_s=CCSprite::create("button_start.png");  
  52.       
  53.     startitem=CCMenuItemSprite::create(startSprite, startSprite_s);  
  54.       
  55.     CCMenu *startmenu=CCMenu::create(startitem,NULL);  
  56.       
  57.     startmenu->setPosition(ccp(pausebgSprite->getContentSize().width/2, pausebgSprite->getContentSize().height/2));  
  58.       
  59.     this->addChild(startmenu, 1);  
  60.   
  61.       
  62.       
  63.     //重新开始  
  64.     CCSprite *restartSprite=CCSprite::create("button_restart.png");  
  65.       
  66.     CCSprite *restartSprite_s=CCSprite::create("button_restart.png");  
  67.       
  68.     CCMenuItemSprite *restartitem=CCMenuItemSprite::create(restartSprite, restartSprite_s, this, menu_selector(PauseLayer::restartPressed));  
  69.       
  70.     CCMenu *restartmenu=CCMenu::create(restartitem,NULL);  
  71.       
  72.     restartitem->setAnchorPoint(ccp(0, 0));  
  73.       
  74.     restartmenu->setPosition(ccp(71*2,129*2));  
  75.       
  76.     this->addChild(restartmenu, 1);  
  77.   
  78.     //帮助  
  79.     CCSprite *helpSprite=CCSprite::create("button_help-hd.png");  
  80.       
  81.     CCSprite *helpSprite_s=CCSprite::create("button_help-hd.png");  
  82.       
  83.     CCMenuItemSprite *helpitem=CCMenuItemSprite::create(helpSprite, helpSprite_s, this, menu_selector(PauseLayer::helpPressed));  
  84.       
  85.     CCMenu *helpmenu=CCMenu::create(helpitem,NULL);  
  86.       
  87.     helpitem->setAnchorPoint(ccp(0, 0));  
  88.       
  89.     helpmenu->setPosition(ccp(114*2,21*2));  
  90.       
  91.     this->addChild(helpmenu, 1);  
  92.   
  93.       
  94.     //音乐  
  95.     CCSprite *musicSprite=CCSprite::create("button_music-hd.png");  
  96.       
  97.     CCSprite *musicSprite_s=CCSprite::create("button_music-hd.png");  
  98.       
  99.     CCMenuItemSprite *musicitem=CCMenuItemSprite::create(musicSprite, musicSprite_s, this, menu_selector(PauseLayer::musicPressed));  
  100.       
  101.     CCMenu *musicmenu=CCMenu::create(musicitem,NULL);  
  102.       
  103.     musicitem->setAnchorPoint(ccp(0, 0));  
  104.       
  105.     musicmenu->setPosition(ccp(127*2,66*2));  
  106.       
  107.     this->addChild(musicmenu, 1);  
  108.   
  109.   
  110.     //音效  
  111.     CCSprite *soundSprite=CCSprite::create("button_sound-hd.png");  
  112.       
  113.     CCSprite *soundSprite_s=CCSprite::create("button_sound-hd.png");  
  114.       
  115.     CCMenuItemSprite *sounditem=CCMenuItemSprite::create(soundSprite, soundSprite_s, this, menu_selector(PauseLayer::soundPressed));  
  116.     CCMenu *soundmenu=CCMenu::create(sounditem,NULL);  
  117.       
  118.     sounditem->setAnchorPoint(ccp(0, 0));  
  119.       
  120.     soundmenu->setPosition(ccp(114*2,111*2));  
  121.       
  122.     this->addChild(soundmenu, 1);  
  123.   
  124.   
  125. }  
  126.   
  127.   
  128.   
  129.   
  130.   
  131. void PauseLayer::restartPressed()  
  132. {  
  133.   
  134.   
  135.   
  136.   
  137.   
  138.   
  139. }  
  140.   
  141. void PauseLayer::helpPressed()  
  142. {  
  143.   
  144.   
  145.   
  146.   
  147.   
  148.   
  149. }  
  150.   
  151. void PauseLayer::musicPressed()  
  152. {  
  153.   
  154.   
  155.   
  156.   
  157.   
  158. }  
  159.   
  160.   
  161. void PauseLayer::soundPressed()  
  162. {  
  163.   
  164.   
  165.   
  166.   
  167.   
  168.   
  169. }  

然后在GameLayer类中加入我们的暂停界面,

GameLayer.h

[cpp]  view plain copy
  1. void backPressed(void);  
  2.       
  3.     void startPressed(void);  
  4.       
  5.     void gamePause(void);  
  6.   
  7.     void gameRestart(void);  
  8.       
  9.     PauseLayer *pauseLayer;  

GameLayer.cpp

在函数initUI中加入

[cpp]  view plain copy
  1. //暂停界面  
  2.   pauseLayer=PauseLayer::create();  
  3.     
  4.   pauseLayer->setPosition(ccp(-pauseLayer->getContentSize().width, wSize.height/2-pauseLayer->getContentSize().height/2));  
  5.   this->addChild(pauseLayer, 1);  
  6.     
  7.   //设置监听事件  
  8.   pauseLayer->backitem->setTarget(this, menu_selector(GameLayer::backPressed));  
  9.   pauseLayer->startitem->setTarget(this, menu_selector(GameLayer::startPressed));  
点击暂停按钮
[cpp]  view plain copy
  1. //暂停  
  2. void GameLayer::pausePressed()  
  3. {  
  4.   
  5.       
  6.       
  7.    // CCDirector::sharedDirector()->pause();  
  8.       
  9.     SimpleAudioEngine::sharedEngine()->playEffect("3.wav");  
  10.       
  11.     this->functionSpriteAnimation(CCPoint(leisureSprite->getContentSize().width/2+10*2, wSize.height+leisureSprite->getContentSize().height/2+5*2) ,CCPoint(leisureSprite->getContentSize().width/2+10*2, wSize.height+leisureSprite->getContentSize().height/2));  
  12.       
  13.     this->functionPauseSpriteAnimation(CCPoint(pausemenu->getPosition().x, -pauseSprite->getContentSize().height/2-5*2), CCPoint(pausemenu->getPosition().x, -pauseSprite->getContentSize().height/2));  
  14.       
  15.     levelSprite->setVisible(false);  
  16.     levelnumsAtlas->setVisible(false);  
  17.     progressbgSprite->setVisible(false);  
  18.     progress->setVisible(false);  
  19.     numsTTF->setVisible(false);  
  20.   
  21.     CCMoveTo *ac1=CCMoveTo::create(0.15, CCPoint(-44*2, pauseLayer->getPosition().y));  
  22.     CCMoveTo *ac2=CCMoveTo::create(0.15, CCPoint(-100, pauseLayer->getPosition().y));  
  23.       
  24.       
  25.     CCSequence *seq=CCSequence::create(ac1,ac2,CCCallFunc::create(this, callfunc_selector(GameLayer::gamePause)),NULL);  
  26.       
  27.       
  28.     pauseLayer->runAction(seq);  
  29.   
  30.       
  31.       
  32.       
  33. }  
  34.   
  35.   
  36. //游戏暂停  
  37. void GameLayer::gamePause()  
  38. {  
  39.   
  40.   
  41.     CCDirector::sharedDirector()->pause();  
  42.   
  43.   
  44.   
  45. }  
  46.   
  47. //游戏恢复  
  48. void GameLayer::gameRestart()  
  49. {  
  50.   
  51.     CCDirector::sharedDirector()->resume();  
  52.       
  53.   
  54.   
  55. }  

点击返回按钮,返回到首界面

[cpp]  view plain copy
  1. //返回  
  2. void GameLayer::backPressed()  
  3. {  
  4.   
  5.     this->gameRestart();  
  6.   
  7.     SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();  
  8.     SimpleAudioEngine::sharedEngine()->playEffect("2.wav");  
  9.       
  10.     CCScene *scene=HelloWorld::scene();  
  11.       
  12.     //旋转动画  
  13.     CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::create(1.2f, scene));  
  14.   
  15.   
  16. }  
  17.   
  18.   
  19. //继续  
  20. void GameLayer::startPressed()  
  21. {  
  22.       
  23.       
  24.     this->gameRestart();  
  25.       
  26.       
  27.     CCMoveTo *ac1=CCMoveTo::create(0.15, CCPoint(-100+4, wSize.height/2-pauseLayer->getContentSize().height/2));  
  28.     CCMoveTo *ac2=CCMoveTo::create(0.15, CCPoint(-pauseLayer->getContentSize().width, wSize.height/2-pauseLayer->getContentSize().height/2));  
  29.       
  30.       
  31.     CCSequence *seq=CCSequence::create(ac1,ac2,NULL);  
  32.     http://digi.tech.qq.com/zt2013/newipad/live.htm  
  33.       
  34.     pauseLayer->runAction(seq);  
  35.       
  36.     levelSprite->setVisible(true);  
  37.     levelnumsAtlas->setVisible(true);  
  38.       
  39.     progressbgSprite->setVisible(true);  
  40.     progress->setVisible(true);  
  41.     numsTTF->setVisible(true);  
  42.       
  43.       
  44.     this->functionSpriteAnimation(CCPoint(leisureSprite->getPosition().x, wSize.height-leisureSprite->getContentSize().height/2-15*2) ,CCPoint(leisureSprite->getPosition().x, wSize.height-leisureSprite->getContentSize().height/2-10*2));  
  45.       
  46.     this->functionPauseSpriteAnimation(CCPoint(pausemenu->getPosition().x, 34*2), CCPoint(pausemenu->getPosition().x, 29*2));  
  47.       
  48.       
  49.       
  50. }  


OK。。。。。。。代码不是很复杂,都是一些简单的逻辑,所以不具体细说,大家可以看代码

效果图如下:




@@@@@@@@@@@@

下篇文章会将重点讲游戏逻辑。。。。。。。

接下来,我们将完成剩下的游戏界面部分。连连看我们可以看做是一个m行乘以n列的一个矩行,其中我们规定0为空的,其余数字可以看作是图片的引用id,通过这个id,我们可以直接找到这个图片,有了图片,我们就可以初始化出这个对应的精灵。然后通过touch事件判断出是否选中或是没选中这个精灵,然后通过相应的算法,算出所选两个精灵对象是否是一样的,并且符合我们的游戏规则,符合就消掉,不符合再重新选取。。。。。。

00001000200003

10000300000002

00040000040000

OK,,我们先新建一个类,叫MapLayer,这个类主要是游戏地图层的部分

[cpp]  view plain copy
  1. //地图层  
  2. class MapLayer :public cocos2d::CCLayerColor  
  3. {  
  4.   
  5.   
  6.       
  7. public:  
  8.       
  9.     virtual bool init();  
  10.       
  11.       
  12.     CREATE_FUNC(MapLayer);  
  13.   
  14.   
  15.     void initUI(void);  
  16.       
  17.     void initData(void);  
  18.       
  19.       
  20.     ~MapLayer();  
  21.   
  22.       
  23.       
  24. private:  
  25.       
  26.       
  27.   
  28. };  
上面部分就是新建一个类,定义一些初始化的函数。对于数据部分,你可以存储在一个一维数组里,当然也可以存储到文件中,只要你能获取到数据,那你怎么存都OK的~~~

这里,我们把数据存到一个plist文件里,plist可以看作是一个xml,我们新建一个plist,叫levelinfo.plist


我们定义第一关001,里面有total_x,列数,total_y,行数,也就是上面我们定义了一个5*9的一个矩形,array里面存了图片对应的id,0表示无,没有。

OK。。接下来,我们来解析这个xml,,代码如下

[cpp]  view plain copy
  1. //读取plist数据文件  
  2.    const char *plistPath=CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile("levelinfo.plist","levelinfo.plist");  
  3.   
  4.      
  5.    CCDictionary *plistDic=CCDictionary::createWithContentsOfFile(plistPath);  
  6.      
  7.      
  8.    CCDictionary *levelDic=dynamic_cast<CCDictionary *>(plistDic->objectForKey("001"));  
  9.      
  10.   
  11.    CCString *xstring=dynamic_cast<CCString *>(levelDic->objectForKey("total_x"));  
  12.   
  13.    total_x=xstring->intValue();  
  14.      
  15.    CCString *ystring=dynamic_cast<CCString *>(levelDic->objectForKey("total_y"));  
  16.      
  17.    total_y=ystring->intValue();  
  18.   
  19.    CCArray *array=CCArray::create();  
  20.   
  21.    array=dynamic_cast<CCArray *>(levelDic->objectForKey("imageidarr"));  

这样,我们就读取到plist里相应的数据,接下来,我们定义一个类,叫MapNode类,它就提供两个属性,order跟id,代码如下

[cpp]  view plain copy
  1. class MapNode :public cocos2d::CCObject  
  2. {  
  3.   
  4.   
  5. public:  
  6.       
  7.     //序号  
  8.     int order;  
  9.     //图片的id  
  10.     int imgid;  
  11.   
  12. };  

接下来,我们对把plist的array里的数据取出来,然后赋值给MapNode的imgid属性,代码如下

[cpp]  view plain copy
  1. mapArray=CCArray::create();  
  2.       
  3.     mapArray->retain();  
  4.       
  5.       
  6.     CCArray *array2=CCArray::create();  
  7.       
  8.     //种子  
  9.     srandom((unsigned int)time(NULL));  
  10.       
  11.       
  12.     for (int i=0; i<(total_x-1)*(total_y-1); i++)  
  13.     {  
  14.           
  15.         MapNode *node=new MapNode();  
  16.         node->autorelease();  
  17.         //产生唯一的orderid  
  18.         node->order=(int)(CCRANDOM_0_1()*INT_MAX)%(int)(CCRANDOM_0_1()*INT_MAX);  
  19.           
  20.         CCString *idString=(CCString *)(array->objectAtIndex(i));  
  21.           
  22.         node->imgid=idString->intValue();  
  23.   
  24.         array2->addObject(node);  
  25.           
  26.     }  
  27.       
  28.     //排序  
  29.     qsort(array2->data->arr, array2->data->num, sizeof(long),compare);  
  30.   
  31.       
  32.       
  33.     for (int x=0; x<total_x; x++)  
  34.     {  
  35.         for (int y=0; y<total_y; y++)  
  36.         {  
  37.               
  38.             if (x==0 || y==0)  
  39.             {  
  40.                   
  41.                 MapNode *node=new MapNode();  
  42.                 node->autorelease();  
  43.                   
  44.                 node->order=0;  
  45.                 node->imgid=0;  
  46.                   
  47.                 mapArray->addObject(node);  
  48.                   
  49.                   
  50.             }else  
  51.             {  
  52.               
  53.                 int i = (y - 1) * (total_x - 1) + x - 1;  
  54.               
  55.                 mapArray->addObject(array2->objectAtIndex(i));  
  56.                   
  57.                  
  58.               
  59.             }  
  60.               
  61.               
  62.         }  
  63.           
  64.           
  65.     }  
mapArray里存的都是经过排序后的mapnode,c排序函数

void qsort(void *, size_tsize_t,int (*)(const void *, const void *));第一个参数是数组头部首地址,第二个参数数组长度,第三个是数组每个元素大小size,第四个是一个int型函数指针,

[cpp]  view plain copy
  1. //排序比较  
  2. int compare(const void *a,const void *b)  
  3. {  
  4.   
  5.     MapNode *node1=(MapNode *)a;  
  6.       
  7.     MapNode *node2=(MapNode *)b;  
  8.   
  9.   
  10.     if (node1->order>node2->order)  
  11.     {  
  12.           
  13.         return 1;  
  14.           
  15.     }  
  16.     else if (node1->order==node2->order)  
  17.     {  
  18.       
  19.       
  20.         return 0;  
  21.       
  22.       
  23.     }  
  24.     else  
  25.     {  
  26.   
  27.         return -1;  
  28.   
  29.       
  30.     }  
  31.   
  32.   
  33.   
  34. }  
OK,接下来,我们来布局我们的图片sprite,代码如下

[cpp]  view plain copy
  1. //布局sprite  
  2.    for (int y = 0; y < total_y; ++y)  
  3.    {  
  4.          
  5.     for (int x = 0; x < total_x; ++x)  
  6.        {  
  7.              
  8.            int index=y*total_x+x;  
  9.              
  10.            if (this->imageFilename(index))  
  11.            {  
  12.                  
  13.                CCSprite *sprite=CCSprite::createWithSpriteFrameName(this->imageFilename(index)->getCString());  
  14.                  
  15.                sprite->setScale(1.0);  
  16.                sprite->setPosition(ccp(OFFSET_X * x + (SIZE_W / 2) + SIZE_W * x, OFFSET_Y * y + (SIZE_H / 2) + SIZE_H * y));  
  17.                  
  18.                this->addChild(sprite, 1, TAG_START_SPRITE+index);  
  19.                  
  20.                  
  21.            }  
  22.              
  23.              
  24.        }  
  25.          
  26.          
  27.    }  

[cpp]  view plain copy
  1. //获取图片  
  2. CCString* MapLayer::imageFilename(int index)  
  3. {  
  4.   
  5.   
  6.     int n=((MapNode *)(mapArray->objectAtIndex(index)))->imgid;  
  7.   
  8.       
  9.    // CCLOG("n------:%d",index);  
  10.   
  11.     if (n>=1 && n<=20)  
  12.     {  
  13.           
  14.           
  15.         return CCString::createWithFormat("%d.png",n);  
  16.           
  17.           
  18.     }  
  19.   
  20.     else  
  21.     {  
  22.       
  23.       
  24.       
  25.         return NULL;  
  26.       
  27.     }  
  28.   
  29.   
  30.       
  31. }  
我们运行下,看下效果


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

接下来,我们加入触摸事件,代码如下

MapLayer.h中加入

[cpp]  view plain copy
  1. virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);  
  2.     virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);  

MapLayer.cpp中加入

[cpp]  view plain copy
  1. //添加touch监听  
  2.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, true);  

[cpp]  view plain copy
  1. //  
  2. bool MapLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  3. {  
  4.   
  5.       
  6.     return true;  
  7.   
  8.   
  9. }  
  10.   
  11.   
  12. //  
  13. void MapLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  14. {  
  15.   
  16.   
  17.   
  18.       
  19.   
  20.   
  21. }  
我们将在touchend里处理事件,我们获取手指触摸点的坐标,然后把它转换成本地坐标,不然获取的坐标是整个下层的坐标,再把本地坐标转换成地图坐标,就像坦克大战里的,比如(1,2)之类的,看代码

[cpp]  view plain copy
  1. //  
  2. bool MapLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  3. {  
  4.   
  5.     CCPoint location = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());  
  6.   
  7.     //在这个层区域内返回true  
  8.     if (this->boundingBox().containsPoint(location))  
  9.     {  
  10.           
  11.           
  12.         return true;  
  13.           
  14.           
  15.     }  
  16.       
  17.     //否则返回false,阻止接下来的touch函数  
  18.     return false;  
  19.   
  20.   
  21. }  
  22.   
  23.   
  24. //  
  25. void MapLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  26. {  
  27.   
  28.   
  29.     CCPoint location = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());  
  30.     location = this->convertToNodeSpace(location);  
  31.       
  32.     //location=CCPointMake(location.x, location.y-40);  
  33.       
  34.     location=this->pointOfView(location);  
  35.       
  36.       
  37.     //  CCLOG("x:%f",location.x);  
  38.     //  CCLOG("y:%f",location.y);  
  39.   
  40.       
  41.       
  42. }  

[cpp]  view plain copy
  1. //屏幕坐标转换成地图坐标  
  2. CCPoint MapLayer::pointOfView(cocos2d::CCPoint point)  
  3. {  
  4.   
  5.     <p class="p1"><span class="s1">int</span> x = -<span class="s2">1</span>;</p><p class="p1"><span>   </span><span class="s1">int</span> y = -<span class="s2">1</span>;</p><p class="p1"><span>  </span><span class="s1">if</span> (point.<span class="s3">x</span> > <span class="s2">0</span> && point.<span class="s3">x</span> < <span class="s3">total_x</span> * <span class="s4">SIZE_W</span> + <span class="s4">OFFSET_X</span> * (<span class="s3">total_x</span>-<span class="s2">1</span>))</p><p class="p1"><span>    </span><span>   </span>x = (point.<span class="s3">x</span> ) / (<span class="s4">SIZE_W</span>+<span class="s4">OFFSET_X</span>);</p><p class="p1"><span>  </span><span class="s1">if</span> (point.<span class="s3">y</span> > <span class="s2">0</span> && point.<span class="s3">y</span> < (<span class="s3">total_y</span>-<span class="s2">1</span>)* <span class="s4">OFFSET_Y</span> + <span class="s3">total_y</span> * <span class="s4">SIZE_H</span> )</p><p class="p1"><span>    </span><span>   </span>y = (point.<span class="s3">y</span>) / (<span class="s4">SIZE_H</span>+<span class="s4">OFFSET_Y</span>);</p><p class="p2">    </p><p class="p1"><span>  </span><span class="s1">return</span> <span class="s3">CCPoint</span>(x, y);</p>  
  6.   
  7. }  

OK。。。。接下来,我们会把剩下的一些算法,判断函数全部写下来,大家直接看代码吧

[cpp]  view plain copy
  1. //是否在有效范围内  
  2. bool MapLayer::isValiableNode(cocos2d::CCPoint point)  
  3. {  
  4.   
  5.   
  6.     return point.x >= 0 && point.x < total_x && point.y >= 0 && point.y < total_y;  
  7.   
  8.   
  9. }  
  10.   
  11. //是否是空的坐标点  
  12. bool MapLayer::isEmptyNode(cocos2d::CCPoint point)  
  13. {  
  14.   
  15.   
  16.     int index=this->indexFromPoint(point);  
  17.       
  18.       
  19.     MapNode *node=(MapNode *)mapArray->objectAtIndex(index);  
  20.       
  21.       
  22.     return (node->imgid==0);  
  23.       
  24.   
  25.   
  26. }  
  27.   
  28. //每个sprite的index  
  29. int MapLayer::indexFromPoint(cocos2d::CCPoint point)  
  30. {  
  31.   
  32.   
  33.     return point.y * total_x + point.x;  
  34.   
  35.       
  36.   
  37. }  
  38.   
  39.   
  40. //是否是相同的点  
  41. bool MapLayer::isSamePoints(cocos2d::CCPoint p1, cocos2d::CCPoint p2)  
  42. {  
  43.   
  44.   
  45.     return (p1.x == p2.x && p1.y == p2.y);  
  46.   
  47.   
  48. }  
  49.   
  50.   
  51. //清除  
  52. void MapLayer::clearNode(cocos2d::CCPoint point)  
  53. {  
  54.   
  55.   
  56.     int index=this->indexFromPoint(point);  
  57.       
  58.       
  59.     MapNode *node=(MapNode *)mapArray->objectAtIndex(index);  
  60.   
  61.     node->imgid=0;  
  62.   
  63. }  
  64.   
  65. //判断两个是否可以消除  
  66. bool MapLayer::canClearTwo(cocos2d::CCPoint pointpre, cocos2d::CCPoint pointcurrent)  
  67. {  
  68.     bool bMatch = false;  
  69.     int pre = this->indexFromPoint(pointpre);  
  70.     int current = this->indexFromPoint(pointcurrent);  
  71.     int p = ((MapNode *)(mapArray->objectAtIndex(pre)))->imgid;  
  72.     int c = ((MapNode *)(mapArray->objectAtIndex(current)))->imgid;  
  73.       
  74.     if (p == c && this->match(pointcurrent, pointpre)) {  
  75.         bMatch = true;  
  76.     }  
  77.       
  78.     return bMatch;  
  79. }  
  80.   
  81.   
  82. //放大缩小动画  
  83. void MapLayer::scaleAnimation(cocos2d::CCSprite* sprite)  
  84. {  
  85.   
  86.       
  87.   
  88.     CCScaleTo *ac1=CCScaleTo::create(0.07f, 1.0);  
  89.     CCScaleTo *ac2=CCScaleTo::create(0.07f, 0.9);  
  90.     CCScaleTo *ac3=CCScaleTo::create(0.07f, 1.0);  
  91.   
  92.     CCSequence *seq=CCSequence::create(ac1,ac2,ac3,NULL);  
  93.   
  94.   
  95.     sprite->runAction(seq);  
  96.       
  97.       
  98.   
  99. }  

[cpp]  view plain copy
  1. //三种匹配算法  
  2. //一直线  
  3. bool MapLayer::match_direct(cocos2d::CCPoint a, cocos2d::CCPoint b)  
  4. {  
  5.     if (!(a.x == b.x || a.y == b.y)) {  
  6.         return false;  
  7.     }  
  8.       
  9.     int i;  
  10.     bool match_x = false;  
  11.     if(a.x == b.x) {  
  12.         match_x = true;  
  13.         if(a.y > b.y) {  
  14.             for(i = a.y - 1; i > b.y; --i) {  
  15.                 CCPoint point = CCPointMake(a.x, i);  
  16.                 if(!this->isValiableNode(point) || !this->isEmptyNode(point)){  
  17.                     match_x = false;  
  18.                 }  
  19.             }  
  20.         }  
  21.         if(b.y > a.y) {  
  22.             for(i = b.y - 1; i > a.y; --i) {  
  23.                 CCPoint point = CCPointMake(a.x, i);  
  24.                 if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {  
  25.                     match_x = false;  
  26.                 }  
  27.             }  
  28.         }  
  29.     }  
  30.       
  31.     bool match_y = false;  
  32.     if(a.y == b.y) {  
  33.         match_y = true;  
  34.         if(a.x > b.x) {  
  35.             for(i = a.x - 1; i > b.x; --i) {  
  36.                 CCPoint point = CCPointMake(i, a.y);  
  37.                 if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {  
  38.                     match_y = false;  
  39.                 }  
  40.             }  
  41.         }  
  42.         if(b.x > a.x) {  
  43.             for(i = b.x - 1; i > a.x; --i) {  
  44.                 CCPoint point = CCPointMake(i, a.y);  
  45.                 if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {  
  46.                     match_y = false;  
  47.                 }  
  48.             }  
  49.         }  
  50.     }  
  51.       
  52.     return match_x || match_y;  
  53. }  
  54.   
  55. //一个拐点的  
  56. bool MapLayer::match_one_corner(cocos2d::CCPoint a, cocos2d::CCPoint b)  
  57. {  
  58.     CCPoint point = CCPointMake(b.x, a.y);  
  59.       
  60.     ifthis->isValiableNode(point) && this->isEmptyNode(point) && this->match_direct(a, point) && this->match_direct(b, point)){  
  61.         return true;  
  62.     }  
  63.       
  64.     point = CCPointMake(a.x, b.y);  
  65.     ifthis->isValiableNode(point) && this->isEmptyNode(point) && this->match_direct(a, point) && this->match_direct(b, point)){  
  66.         return true;  
  67.     }  
  68.       
  69.     return false;  
  70. }  
  71.   
  72. //两个拐点的  
  73. bool MapLayer::match_two_corner(cocos2d::CCPoint a, cocos2d::CCPoint b)  
  74. {  
  75.     for(int i = a.x - 1; i >= 0; --i) {  
  76.         CCPoint point = CCPointMake(i, a.y);  
  77.         if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {  
  78.             break;  
  79.         } else {  
  80.             if (this->match_one_corner(point, b)) {  
  81.                 return true;  
  82.             }  
  83.         }  
  84.     }  
  85.       
  86.     for(int i = a.x + 1; i < total_x; ++i) {  
  87.         CCPoint point = CCPointMake(i, a.y);  
  88.         if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {  
  89.             break;  
  90.         } else {  
  91.             if (this->match_one_corner(point, b)) {  
  92.                 return true;  
  93.             }  
  94.         }  
  95.     }  
  96.       
  97.     for(int i = a.y - 1; i >= 0; --i) {  
  98.         CCPoint point = CCPointMake(a.x ,i);  
  99.         if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {  
  100.             break;  
  101.         } else {  
  102.             if (this->match_one_corner(point, b)) {  
  103.                 return true;  
  104.             }  
  105.         }  
  106.     }  
  107.       
  108.     for(int i = a.y + 1; i < total_y; ++i) {  
  109.         CCPoint point = CCPointMake(a.x ,i);  
  110.         if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {  
  111.             break;  
  112.         } else {  
  113.             if (this->match_one_corner(point, b)) {  
  114.                 return true;  
  115.             }  
  116.         }  
  117.     }  
  118.       
  119.     return false;  
  120. }  
  121.   
  122.   
  123. bool MapLayer::match(cocos2d::CCPoint a,cocos2d::CCPoint b)  
  124. {  
  125.     if (this->match_direct(a, b)) {  
  126.         return true;  
  127.     }  
  128.     if (this->match_one_corner(a, b)) {  
  129.         return true;  
  130.     }  
  131.     if (this->match_two_corner(a, b)) {  
  132.         return true;  
  133.     }  
  134.       
  135.     return false;  
  136. }  

重新写touch事件

[cpp]  view plain copy
  1. //  
  2. bool MapLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  3. {  
  4.   
  5.     CCPoint location = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());  
  6.   
  7.     //在这个层区域内返回true  
  8.     if (this->boundingBox().containsPoint(location))  
  9.     {  
  10.           
  11.           
  12.         return true;  
  13.           
  14.           
  15.     }  
  16.       
  17.     //否则返回false,阻止接下来的touch函数  
  18.     return false;  
  19.   
  20.   
  21. }  
  22.   
  23.   
  24. //  
  25. void MapLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  26. {  
  27.   
  28.   
  29.     CCPoint location = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());  
  30.     location = this->convertToNodeSpace(location);  
  31.       
  32.     //location=CCPointMake(location.x, location.y-40);  
  33.       
  34.     location=this->pointOfView(location);  
  35.       
  36.       
  37.       CCLOG("x:%f",location.x);  
  38.       CCLOG("y:%f",location.y);  
  39.   
  40.     if (this->isValiableNode(location)==false)  
  41.     {  
  42.         return;  
  43.     }  
  44.       
  45.     if (this->isEmptyNode(location))  
  46.     {  
  47.         return;  
  48.     }  
  49.   
  50.       
  51.   
  52.     SimpleAudioEngine::sharedEngine()->playEffect("12.wav");  
  53.       
  54.       
  55.       
  56.     if (this->isSamePoints(location, prePoint))  
  57.     {  
  58.         return;  
  59.     }  
  60.       
  61.     //点击当前精灵  
  62.     CCSprite *spritecurrent=(CCSprite *)this->getChildByTag(TAG_START_SPRITE+this->indexFromPoint(location));  
  63.     spritecurrent->setScale(1.3);  
  64.   
  65.     CCLOG("%d",this->indexFromPoint(location));  
  66.       
  67.     if (this->isValiableNode(prePoint))  
  68.     {  
  69.       
  70.         //前一个  
  71.          CCSprite *spritepre=(CCSprite *)this->getChildByTag(TAG_START_SPRITE+this->indexFromPoint(prePoint));  
  72.           
  73.         if (this->canClearTwo(prePoint, location))  
  74.         {  
  75.               
  76.             SimpleAudioEngine::sharedEngine()->playEffect("4.wav");  
  77.   
  78.             this->clearNode(location);  
  79.             this->clearNode(prePoint);  
  80.               
  81.               
  82.             spritecurrent->setVisible(false);  
  83.             spritepre->setVisible(false);  
  84.               
  85.               
  86.         }  
  87.         else  
  88.         {  
  89.           
  90.           
  91.             spritepre->setScale(0.9);  
  92.               
  93.           
  94.             this->scaleAnimation(spritepre);  
  95.           
  96.         }  
  97.           
  98.           
  99.           
  100.     }  
  101.       
  102.       
  103.       
  104.       
  105.     prePoint=location;  
  106.   
  107.       
  108.       
  109. }  

运行,效果如下


选中的时候,放大精灵~~~~


~~~~~这里时间有限,下一篇的文章将探讨一些自动消除,还有用粒子效果来实现消除时的动画等,还有一些其他问题~~~



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值