这篇文章中,我们将简单的做一个连连看的demo,这里也会涉及到寻径算法,连连看有三种情况,一条直线的,一个拐点的,两个拐点的,
情况一:要连接的两点在同一条直线上
首页,我们先模仿某个游戏的界面,先做一个首界面。首界面我们要实现这样的效果,,白云,蓝天,船儿在航行,鸟儿在飞翔,海浪~~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;
- };
- 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,,开始界面就完成~~~下面就我们将完成连连看的游戏界面~~~
(*^__^*) …….....................................................................................................
这篇文章中,我们将完成剩下的部分,点击首界面进入游戏主界面,我们初始化一些界面元素,实现这样的功能,有暂停功能,背景音乐,进度条等,加上一些动画,下面直接上代码,前面都是一些界面的布局和动画等,比较简单。先新建一个GameLayer类
GameLayer.h
- //游戏主界面
- class GameLayer : public cocos2d::CCLayerColor
- {
- public:
- virtual bool init();
- static cocos2d::CCScene* scene();
- CREATE_FUNC(GameLayer);
- private:
- void initData(void);
- void initUI(void);
- void initLoadingUI(void);
- void playSound1(void);
- void playSound2(void);
- void playSound3(void);
- void initDialog(void);
- void pausePressed(void);
- void dialogAnimaton(void);
- void dialogAnimaton2(void);
- void dialogAnimaton3(void);
- void removeDialogLayer(void);
- void loadingAnimation(void);
- void removeLoadSprite(void);
- void functionSpriteAnimation(cocos2d::CCPoint point1,cocos2d::CCPoint point2);
- void functionPauseSpriteAnimation(cocos2d::CCPoint point1,cocos2d::CCPoint point2);
- CCLayerColor *dialogLayer;
- cocos2d::CCString *dialogLevelString;
- cocos2d::CCString *dialogTishiString;
- cocos2d::CCSprite *leisureSprite;
- cocos2d::CCSprite *levelSprite;
- cocos2d::CCLabelAtlas *levelnumsAtlas;
- cocos2d::CCString *_level;
- cocos2d::CCSprite *pauseSprite;
- cocos2d::CCMenu *pausemenu;
- cocos2d::CCSprite *loadSprite;
- };
GameLayer.cpp
- CCScene* GameLayer::scene()
- {
- CCScene *scene = CCScene::create();
- GameLayer *layer = GameLayer::create();
- scene->addChild(layer);
- return scene;
- }
- bool GameLayer::init()
- {
- if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)))
- {
- return false;
- }
- this->initLoadingUI();
- //循环播放三首背景音乐
- this->playSound1();
- return true;
- }
- void GameLayer::initData()
- {
- dialogLevelString=CCString::create("第一关");
- dialogTishiString=CCString::create("王子大人");
- // dialogLevelString->retain();
- // dialogTishiString->retain();
- _level=CCString::create("001");
- }
- //load界面
- void GameLayer::initLoadingUI()
- {
- loadSprite=CCSprite::create("loading_logo-hd.png");
- loadSprite->setPosition(ccp(wSize.width/2, wSize.height/2));
- this->addChild(loadSprite, 0);
- this->loadingAnimation();
- }
- void GameLayer::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
- CCSprite *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
- CCSprite *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);
- this->initDialog();
- //图标1
- leisureSprite=CCSprite::create("game_leisure_logo-hd.png");
- leisureSprite->setPosition(ccp(leisureSprite->getContentSize().width/2+10*2, wSize.height+leisureSprite->getContentSize().height/2));
- this->addChild(leisureSprite, 1);
- //等级图标
- levelSprite=CCSprite::create("orange_font_level-hd.png");
- levelSprite->setPosition(ccp(leisureSprite->getContentSize().width/2+8*2, 255*2));
- this->addChild(levelSprite, 1);
- levelSprite->setVisible(false);
- //等级sprite
- levelnumsAtlas=CCLabelAtlas::create(_level->getCString(), "small_blue_number_level-hd.png",16, 20, '0');
- levelnumsAtlas->setScale(1.2);
- levelnumsAtlas->setPosition(ccp(leisureSprite->getContentSize().width/2-6*2,levelSprite->getPosition().y-20*2));
- levelnumsAtlas->setVisible(false);
- this->addChild(levelnumsAtlas, 1);
- //暂停菜单
- pauseSprite=CCSprite::create("button_pause-hd.png");
- CCSprite *pauseSprite_s=CCSprite::create("button_pause-hd.png");
- CCMenuItemSprite *item=CCMenuItemSprite::create(pauseSprite, pauseSprite_s, this, menu_selector(GameLayer::pausePressed));
- pausemenu=CCMenu::create(item,NULL);
- item->setAnchorPoint(ccp(0.5, 0.5));
- pausemenu->setPosition(ccp(pauseSprite->getContentSize().width/2+10*2,-pauseSprite->getContentSize().height/2));
- this->addChild(pausemenu, 1);
- this->dialogAnimaton();
- }
- //初始化弹出框
- void GameLayer::initDialog()
- {
- //弹出框
- dialogLayer=CCLayerColor::create(ccc4(0, 0, 0, 0), 333*2, 235*2);
- dialogLayer->setPosition(ccp(wSize.width/2-dialogLayer->getContentSize().width/2, wSize.height/2-dialogLayer->getContentSize().height/2));
- dialogLayer->setScale(0.9);
- this->addChild(dialogLayer, 1);
- CCSprite *dialogSprite=CCSprite::create("game_dialog-hd.png");
- dialogSprite->setAnchorPoint(ccp(0, 0));
- dialogSprite->setPosition(ccp(0, 0));
- dialogLayer->addChild(dialogSprite, 1);
- //标题1
- CCLabelTTF *dialoglevel=CCLabelTTF::create(dialogLevelString->getCString(), "Marker Felt", 35);
- dialoglevel->setAnchorPoint(ccp(0, 0));
- dialoglevel->setPosition(ccp(dialogLayer->getContentSize().width/2-dialoglevel->getContentSize().width/2, dialogLayer->getContentSize().height/2-dialoglevel->getContentSize().height/2+30*2));
- dialoglevel->setColor(ccWHITE);
- dialogLayer->addChild(dialoglevel, 1, 310);
- //标题2
- CCLabelTTF *dialogtishi=CCLabelTTF::create(dialogTishiString->getCString(), "Marker Felt", 35);
- dialogtishi->setAnchorPoint(ccp(0, 0));
- dialogtishi->setPosition(ccp(dialogLayer->getContentSize().width/2-dialogtishi->getContentSize().width/2, dialogLayer->getContentSize().height/2-dialogtishi->getContentSize().height/2-15*2));
- dialogtishi->setColor(ccWHITE);
- dialogLayer->addChild(dialogtishi, 1, 311);
- }
- //load界面动画
- void GameLayer::loadingAnimation()
- {
- CCFadeOut *ac1=CCFadeOut::create(3.0f);
- CCSequence *seq=CCSequence::create(ac1,CCCallFunc::create(this, callfunc_selector(GameLayer::removeLoadSprite)),NULL);
- loadSprite->runAction(seq);
- }
- //移除load界面
- void GameLayer::removeLoadSprite()
- {
- loadSprite->removeFromParentAndCleanup(true);
- this->initData();
- this->initUI();
- }
- void GameLayer::playSound1()
- {
- SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
- SimpleAudioEngine::sharedEngine()->playBackgroundMusic("Back2new.mp3", false);
- this->scheduleOnce(schedule_selector(GameLayer::playSound2), 60);
- }
- void GameLayer::playSound2()
- {
- SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
- SimpleAudioEngine::sharedEngine()->playBackgroundMusic("Back3new.mp3", false);
- this->scheduleOnce(schedule_selector(GameLayer::playSound3), 62);
- }
- void GameLayer::playSound3()
- {
- SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
- SimpleAudioEngine::sharedEngine()->playBackgroundMusic("MainMenu.mp3", false);
- this->scheduleOnce(schedule_selector(GameLayer::playSound1), 65);
- }
- //暂停
- void GameLayer::pausePressed()
- {
- }
- //弹出框动画
- void GameLayer::dialogAnimaton()
- {
- SimpleAudioEngine::sharedEngine()->playEffect("19.wav");
- CCShow *ac1=CCShow::create();
- CCScaleTo *ac2=CCScaleTo::create(0.1f, 1.0);
- CCSpawn *spwan=CCSpawn::create(ac1,ac2,NULL);
- CCSequence *seq=CCSequence::create(spwan,CCCallFunc::create(this, callfunc_selector(GameLayer::dialogAnimaton2)),NULL);
- dialogLayer->runAction(seq);
- }
- void GameLayer::dialogAnimaton2()
- {
- CCScaleTo *ac1=CCScaleTo::create(0.1f, 0.9);
- CCSequence *seq=CCSequence::create(ac1,CCCallFunc::create(this, callfunc_selector(GameLayer::dialogAnimaton3)),NULL);
- dialogLayer->runAction(seq);
- }
- void GameLayer::dialogAnimaton3()
- {
- CCScaleTo *ac1=CCScaleTo::create(0.1f, 1.0);
- CCScaleTo *ac2=CCScaleTo::create(0.2f, 1.2);
- CCFadeTo *ac3=CCFadeTo::create(0.2f, 0);
- CCSpawn *spwan=CCSpawn::create(ac2,ac3,NULL);
- CCSequence *seq=CCSequence::create(ac1,CCDelayTime::create(1.5f),spwan,CCCallFunc::create(this, callfunc_selector(GameLayer::removeDialogLayer)),NULL);
- dialogLayer->runAction(seq);
- }
- //移除弹出框
- void GameLayer::removeDialogLayer()
- {
- SimpleAudioEngine::sharedEngine()->playEffect("16.wav");
- dialogLayer->removeFromParentAndCleanup(true);
- levelSprite->setVisible(true);
- levelnumsAtlas->setVisible(true);
- 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));
- this->functionPauseSpriteAnimation(CCPoint(pausemenu->getPosition().x, 34*2), CCPoint(pausemenu->getPosition().x, 29*2));
- }
- //移动动画
- void GameLayer::functionSpriteAnimation(cocos2d::CCPoint point1, cocos2d::CCPoint point2)
- {
- CCMoveTo *ac1=CCMoveTo::create(0.15, point1);
- CCMoveTo *ac2=CCMoveTo::create(0.15, point2);
- CCSequence *seq=CCSequence::create(ac1,ac2,NULL);
- leisureSprite->runAction(seq);
- }
- 移动动画
- void GameLayer::functionPauseSpriteAnimation(cocos2d::CCPoint point1, cocos2d::CCPoint point2)
- {
- CCMoveTo *ac1=CCMoveTo::create(0.15, point1);
- CCMoveTo *ac2=CCMoveTo::create(0.15, point2);
- CCSequence *seq=CCSequence::create(ac1,ac2,NULL);
- pausemenu->runAction(seq);
- }
场景切换动画
- //开始游戏
- void HelloWorld::playBack()
- {
- SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
- CCScene *scene=GameLayer::scene();
- //旋转动画
- CCDirector::sharedDirector()->replaceScene(CCTransitionRotoZoom::create(1.2f, scene));
- }
以前就是游戏主界面的一些元素布局,跳转进入游戏界面,先出现一个loading界面,加个渐隐的动画,消失后弹出一个关卡提示框,。。。。
效果如下:
~~~~~~~~~接下来,我们加入一个时间进度条,O(∩_∩)O~~~~~~~
在函数initUI()中加入
- //进度条背景
- progressbgSprite=CCSprite::create("time_slot-hd.png");
- progressbgSprite->setAnchorPoint(ccp(0, 0));
- progressbgSprite->setPosition(ccp(130, wSize.height-55));
- this->addChild(progressbgSprite, 1);
- progressbgSprite->setVisible(false);
- CCSprite *progressSprite=CCSprite::create("time_bars-hd.png");
- //进度条
- progress=CCProgressTimer::create(progressSprite);
- progress->setAnchorPoint(ccp(0, 0));
- progress->setType(kCCProgressTimerTypeBar);
- progress->setPosition(ccp(130, wSize.height-55));
- //进度动画运动方向,可以多试几个值,看看效果
- progress->setMidpoint(ccp(0, 0));
- //进度条宽高变化
- progress->setBarChangeRate(ccp(1, 0));
- progress->setPercentage(100);
- this->addChild(progress, 1);
- progress->setVisible(false);
- numsTTF=CCLabelTTF::create("100", "Thonburi", 24);
- numsTTF->setAnchorPoint(ccp(0, 0));
- numsTTF->setPosition(ccp(400, wSize.height-55));
- numsTTF->setColor(ccBLACK);
- this->addChild(numsTTF, 1);
- numsTTF->setVisible(false);
定义刷新函数
- this->schedule(schedule_selector(GameLayer::update), 1);
- //刷新
- void GameLayer::update()
- {
- int cu=progress->getPercentage();
- cu=cu-1.0f;
- progress->setPercentage(cu);
- CCString *str = CCString::createWithFormat("%d",cu);
- numsTTF->setString(str->getCString());
- }
一秒钟刷新一次
效果如下
注意,在我现在用的cocos2dx版本中,在ios下,CCLabelTTF没显示出来,如果你升级到2.1.4以上,就没问题了,如果你不想升级,那么网上的解决方法为修改底层源码:
修改CCImage.mm的
static bool _initWithString(constchar * pText, cocos2d::CCImage::ETextAlign eAlign,const char * pFontName,intnSize, tImageInfo* pInfo)
- const int _width = dim.width; //Add
- const int _height = dim.height; //Add
- unsigned char* data = new unsigned char[(int)(dim.width * dim.height * 4)];
- memset(data, 0, (int)(_width * _height * 4)); //Modify
- // draw text
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- CGContextRef context = CGBitmapContextCreate(data, _width, _height, 8, _width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
- CGColorSpaceRelease(colorSpace); //Modify
- if (! context)
- {
- delete[] data;
- break;
- }
~~~~~~~~~~~~~~~~接下来我们加入一个暂停的界面,并加入一些动画效果
我们新建一个暂停界面的类,PauseLayer,
PauseLayer.h
- class PauseLayer :public cocos2d::CCLayerColor
- {
- public:
- virtual bool init();
- CREATE_FUNC(PauseLayer);
- cocos2d::CCSprite *pausebgSprite;
- cocos2d::CCMenuItemSprite *backitem;
- cocos2d::CCMenuItemSprite *startitem;
- void restartPressed(void);
- void helpPressed(void);
- void musicPressed(void);
- void soundPressed(void);
- private:
- void initUI(void);
- };
PauseLayer.cpp
- bool PauseLayer::init()
- {
- if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 0),343,343))
- {
- return false;
- }
- this->initUI();
- return true;
- }
- //UI界面初始化
- void PauseLayer::initUI()
- {
- pausebgSprite=CCSprite::create("game_menu_background-hd.png");
- pausebgSprite->setPosition(ccp(pausebgSprite->getContentSize().width/2, pausebgSprite->getContentSize().height/2));
- this->addChild(pausebgSprite, 1);
- //返回
- CCSprite *backSprite=CCSprite::create("button_back-hd.png");
- CCSprite *backSprite_s=CCSprite::create("button_back-hd.png");
- backitem=CCMenuItemSprite::create(backSprite, backSprite_s);
- CCMenu *backmenu=CCMenu::create(backitem,NULL);
- backitem->setAnchorPoint(ccp(0, 0));
- backmenu->setPosition(ccp(71*2,3*2));
- this->addChild(backmenu, 1);
- //继续
- CCSprite *startSprite=CCSprite::create("button_start.png");
- CCSprite *startSprite_s=CCSprite::create("button_start.png");
- startitem=CCMenuItemSprite::create(startSprite, startSprite_s);
- CCMenu *startmenu=CCMenu::create(startitem,NULL);
- startmenu->setPosition(ccp(pausebgSprite->getContentSize().width/2, pausebgSprite->getContentSize().height/2));
- this->addChild(startmenu, 1);
- //重新开始
- CCSprite *restartSprite=CCSprite::create("button_restart.png");
- CCSprite *restartSprite_s=CCSprite::create("button_restart.png");
- CCMenuItemSprite *restartitem=CCMenuItemSprite::create(restartSprite, restartSprite_s, this, menu_selector(PauseLayer::restartPressed));
- CCMenu *restartmenu=CCMenu::create(restartitem,NULL);
- restartitem->setAnchorPoint(ccp(0, 0));
- restartmenu->setPosition(ccp(71*2,129*2));
- this->addChild(restartmenu, 1);
- //帮助
- CCSprite *helpSprite=CCSprite::create("button_help-hd.png");
- CCSprite *helpSprite_s=CCSprite::create("button_help-hd.png");
- CCMenuItemSprite *helpitem=CCMenuItemSprite::create(helpSprite, helpSprite_s, this, menu_selector(PauseLayer::helpPressed));
- CCMenu *helpmenu=CCMenu::create(helpitem,NULL);
- helpitem->setAnchorPoint(ccp(0, 0));
- helpmenu->setPosition(ccp(114*2,21*2));
- this->addChild(helpmenu, 1);
- //音乐
- CCSprite *musicSprite=CCSprite::create("button_music-hd.png");
- CCSprite *musicSprite_s=CCSprite::create("button_music-hd.png");
- CCMenuItemSprite *musicitem=CCMenuItemSprite::create(musicSprite, musicSprite_s, this, menu_selector(PauseLayer::musicPressed));
- CCMenu *musicmenu=CCMenu::create(musicitem,NULL);
- musicitem->setAnchorPoint(ccp(0, 0));
- musicmenu->setPosition(ccp(127*2,66*2));
- this->addChild(musicmenu, 1);
- //音效
- CCSprite *soundSprite=CCSprite::create("button_sound-hd.png");
- CCSprite *soundSprite_s=CCSprite::create("button_sound-hd.png");
- CCMenuItemSprite *sounditem=CCMenuItemSprite::create(soundSprite, soundSprite_s, this, menu_selector(PauseLayer::soundPressed));
- CCMenu *soundmenu=CCMenu::create(sounditem,NULL);
- sounditem->setAnchorPoint(ccp(0, 0));
- soundmenu->setPosition(ccp(114*2,111*2));
- this->addChild(soundmenu, 1);
- }
- void PauseLayer::restartPressed()
- {
- }
- void PauseLayer::helpPressed()
- {
- }
- void PauseLayer::musicPressed()
- {
- }
- void PauseLayer::soundPressed()
- {
- }
然后在GameLayer类中加入我们的暂停界面,
GameLayer.h
- void backPressed(void);
- void startPressed(void);
- void gamePause(void);
- void gameRestart(void);
- PauseLayer *pauseLayer;
GameLayer.cpp
在函数initUI中加入
- //暂停界面
- pauseLayer=PauseLayer::create();
- pauseLayer->setPosition(ccp(-pauseLayer->getContentSize().width, wSize.height/2-pauseLayer->getContentSize().height/2));
- this->addChild(pauseLayer, 1);
- //设置监听事件
- pauseLayer->backitem->setTarget(this, menu_selector(GameLayer::backPressed));
- pauseLayer->startitem->setTarget(this, menu_selector(GameLayer::startPressed));
- //暂停
- void GameLayer::pausePressed()
- {
- // CCDirector::sharedDirector()->pause();
- SimpleAudioEngine::sharedEngine()->playEffect("3.wav");
- 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));
- this->functionPauseSpriteAnimation(CCPoint(pausemenu->getPosition().x, -pauseSprite->getContentSize().height/2-5*2), CCPoint(pausemenu->getPosition().x, -pauseSprite->getContentSize().height/2));
- levelSprite->setVisible(false);
- levelnumsAtlas->setVisible(false);
- progressbgSprite->setVisible(false);
- progress->setVisible(false);
- numsTTF->setVisible(false);
- CCMoveTo *ac1=CCMoveTo::create(0.15, CCPoint(-44*2, pauseLayer->getPosition().y));
- CCMoveTo *ac2=CCMoveTo::create(0.15, CCPoint(-100, pauseLayer->getPosition().y));
- CCSequence *seq=CCSequence::create(ac1,ac2,CCCallFunc::create(this, callfunc_selector(GameLayer::gamePause)),NULL);
- pauseLayer->runAction(seq);
- }
- //游戏暂停
- void GameLayer::gamePause()
- {
- CCDirector::sharedDirector()->pause();
- }
- //游戏恢复
- void GameLayer::gameRestart()
- {
- CCDirector::sharedDirector()->resume();
- }
点击返回按钮,返回到首界面
- //返回
- void GameLayer::backPressed()
- {
- this->gameRestart();
- SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
- SimpleAudioEngine::sharedEngine()->playEffect("2.wav");
- CCScene *scene=HelloWorld::scene();
- //旋转动画
- CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::create(1.2f, scene));
- }
- //继续
- void GameLayer::startPressed()
- {
- this->gameRestart();
- CCMoveTo *ac1=CCMoveTo::create(0.15, CCPoint(-100+4, wSize.height/2-pauseLayer->getContentSize().height/2));
- CCMoveTo *ac2=CCMoveTo::create(0.15, CCPoint(-pauseLayer->getContentSize().width, wSize.height/2-pauseLayer->getContentSize().height/2));
- CCSequence *seq=CCSequence::create(ac1,ac2,NULL);
- http://digi.tech.qq.com/zt2013/newipad/live.htm
- pauseLayer->runAction(seq);
- levelSprite->setVisible(true);
- levelnumsAtlas->setVisible(true);
- progressbgSprite->setVisible(true);
- progress->setVisible(true);
- numsTTF->setVisible(true);
- 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));
- this->functionPauseSpriteAnimation(CCPoint(pausemenu->getPosition().x, 34*2), CCPoint(pausemenu->getPosition().x, 29*2));
- }
效果图如下:
@@@@@@@@@@@@
下篇文章会将重点讲游戏逻辑。。。。。。。
接下来,我们将完成剩下的游戏界面部分。连连看我们可以看做是一个m行乘以n列的一个矩行,其中我们规定0为空的,其余数字可以看作是图片的引用id,通过这个id,我们可以直接找到这个图片,有了图片,我们就可以初始化出这个对应的精灵。然后通过touch事件判断出是否选中或是没选中这个精灵,然后通过相应的算法,算出所选两个精灵对象是否是一样的,并且符合我们的游戏规则,符合就消掉,不符合再重新选取。。。。。。
00001000200003
10000300000002
00040000040000
OK,,我们先新建一个类,叫MapLayer,这个类主要是游戏地图层的部分
- //地图层
- class MapLayer :public cocos2d::CCLayerColor
- {
- public:
- virtual bool init();
- CREATE_FUNC(MapLayer);
- void initUI(void);
- void initData(void);
- ~MapLayer();
- private:
- };
这里,我们把数据存到一个plist文件里,plist可以看作是一个xml,我们新建一个plist,叫levelinfo.plist
我们定义第一关001,里面有total_x,列数,total_y,行数,也就是上面我们定义了一个5*9的一个矩形,array里面存了图片对应的id,0表示无,没有。
OK。。接下来,我们来解析这个xml,,代码如下
- //读取plist数据文件
- const char *plistPath=CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile("levelinfo.plist","levelinfo.plist");
- CCDictionary *plistDic=CCDictionary::createWithContentsOfFile(plistPath);
- CCDictionary *levelDic=dynamic_cast<CCDictionary *>(plistDic->objectForKey("001"));
- CCString *xstring=dynamic_cast<CCString *>(levelDic->objectForKey("total_x"));
- total_x=xstring->intValue();
- CCString *ystring=dynamic_cast<CCString *>(levelDic->objectForKey("total_y"));
- total_y=ystring->intValue();
- CCArray *array=CCArray::create();
- array=dynamic_cast<CCArray *>(levelDic->objectForKey("imageidarr"));
这样,我们就读取到plist里相应的数据,接下来,我们定义一个类,叫MapNode类,它就提供两个属性,order跟id,代码如下
- class MapNode :public cocos2d::CCObject
- {
- public:
- //序号
- int order;
- //图片的id
- int imgid;
- };
接下来,我们对把plist的array里的数据取出来,然后赋值给MapNode的imgid属性,代码如下
- mapArray=CCArray::create();
- mapArray->retain();
- CCArray *array2=CCArray::create();
- //种子
- srandom((unsigned int)time(NULL));
- for (int i=0; i<(total_x-1)*(total_y-1); i++)
- {
- MapNode *node=new MapNode();
- node->autorelease();
- //产生唯一的orderid
- node->order=(int)(CCRANDOM_0_1()*INT_MAX)%(int)(CCRANDOM_0_1()*INT_MAX);
- CCString *idString=(CCString *)(array->objectAtIndex(i));
- node->imgid=idString->intValue();
- array2->addObject(node);
- }
- //排序
- qsort(array2->data->arr, array2->data->num, sizeof(long),compare);
- for (int x=0; x<total_x; x++)
- {
- for (int y=0; y<total_y; y++)
- {
- if (x==0 || y==0)
- {
- MapNode *node=new MapNode();
- node->autorelease();
- node->order=0;
- node->imgid=0;
- mapArray->addObject(node);
- }else
- {
- int i = (y - 1) * (total_x - 1) + x - 1;
- mapArray->addObject(array2->objectAtIndex(i));
- }
- }
- }
void qsort(void *, size_t, size_t,int (*)(const void *, const void *));第一个参数是数组头部首地址,第二个参数数组长度,第三个是数组每个元素大小size,第四个是一个int型函数指针,
- //排序比较
- int compare(const void *a,const void *b)
- {
- MapNode *node1=(MapNode *)a;
- MapNode *node2=(MapNode *)b;
- if (node1->order>node2->order)
- {
- return 1;
- }
- else if (node1->order==node2->order)
- {
- return 0;
- }
- else
- {
- return -1;
- }
- }
- //布局sprite
- for (int y = 0; y < total_y; ++y)
- {
- for (int x = 0; x < total_x; ++x)
- {
- int index=y*total_x+x;
- if (this->imageFilename(index))
- {
- CCSprite *sprite=CCSprite::createWithSpriteFrameName(this->imageFilename(index)->getCString());
- sprite->setScale(1.0);
- sprite->setPosition(ccp(OFFSET_X * x + (SIZE_W / 2) + SIZE_W * x, OFFSET_Y * y + (SIZE_H / 2) + SIZE_H * y));
- this->addChild(sprite, 1, TAG_START_SPRITE+index);
- }
- }
- }
- //获取图片
- CCString* MapLayer::imageFilename(int index)
- {
- int n=((MapNode *)(mapArray->objectAtIndex(index)))->imgid;
- // CCLOG("n------:%d",index);
- if (n>=1 && n<=20)
- {
- return CCString::createWithFormat("%d.png",n);
- }
- else
- {
- return NULL;
- }
- }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
接下来,我们加入触摸事件,代码如下
在
MapLayer.h中加入
- virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
- virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
MapLayer.cpp中加入
- //添加touch监听
- CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, true);
- //
- bool MapLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- return true;
- }
- //
- void MapLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- }
- //
- bool MapLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- CCPoint location = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
- //在这个层区域内返回true
- if (this->boundingBox().containsPoint(location))
- {
- return true;
- }
- //否则返回false,阻止接下来的touch函数
- return false;
- }
- //
- void MapLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- CCPoint location = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
- location = this->convertToNodeSpace(location);
- //location=CCPointMake(location.x, location.y-40);
- location=this->pointOfView(location);
- // CCLOG("x:%f",location.x);
- // CCLOG("y:%f",location.y);
- }
- //屏幕坐标转换成地图坐标
- CCPoint MapLayer::pointOfView(cocos2d::CCPoint point)
- {
- <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>
- }
OK。。。。接下来,我们会把剩下的一些算法,判断函数全部写下来,大家直接看代码吧
- //是否在有效范围内
- bool MapLayer::isValiableNode(cocos2d::CCPoint point)
- {
- return point.x >= 0 && point.x < total_x && point.y >= 0 && point.y < total_y;
- }
- //是否是空的坐标点
- bool MapLayer::isEmptyNode(cocos2d::CCPoint point)
- {
- int index=this->indexFromPoint(point);
- MapNode *node=(MapNode *)mapArray->objectAtIndex(index);
- return (node->imgid==0);
- }
- //每个sprite的index
- int MapLayer::indexFromPoint(cocos2d::CCPoint point)
- {
- return point.y * total_x + point.x;
- }
- //是否是相同的点
- bool MapLayer::isSamePoints(cocos2d::CCPoint p1, cocos2d::CCPoint p2)
- {
- return (p1.x == p2.x && p1.y == p2.y);
- }
- //清除
- void MapLayer::clearNode(cocos2d::CCPoint point)
- {
- int index=this->indexFromPoint(point);
- MapNode *node=(MapNode *)mapArray->objectAtIndex(index);
- node->imgid=0;
- }
- //判断两个是否可以消除
- bool MapLayer::canClearTwo(cocos2d::CCPoint pointpre, cocos2d::CCPoint pointcurrent)
- {
- bool bMatch = false;
- int pre = this->indexFromPoint(pointpre);
- int current = this->indexFromPoint(pointcurrent);
- int p = ((MapNode *)(mapArray->objectAtIndex(pre)))->imgid;
- int c = ((MapNode *)(mapArray->objectAtIndex(current)))->imgid;
- if (p == c && this->match(pointcurrent, pointpre)) {
- bMatch = true;
- }
- return bMatch;
- }
- //放大缩小动画
- void MapLayer::scaleAnimation(cocos2d::CCSprite* sprite)
- {
- CCScaleTo *ac1=CCScaleTo::create(0.07f, 1.0);
- CCScaleTo *ac2=CCScaleTo::create(0.07f, 0.9);
- CCScaleTo *ac3=CCScaleTo::create(0.07f, 1.0);
- CCSequence *seq=CCSequence::create(ac1,ac2,ac3,NULL);
- sprite->runAction(seq);
- }
- //三种匹配算法
- //一直线
- bool MapLayer::match_direct(cocos2d::CCPoint a, cocos2d::CCPoint b)
- {
- if (!(a.x == b.x || a.y == b.y)) {
- return false;
- }
- int i;
- bool match_x = false;
- if(a.x == b.x) {
- match_x = true;
- if(a.y > b.y) {
- for(i = a.y - 1; i > b.y; --i) {
- CCPoint point = CCPointMake(a.x, i);
- if(!this->isValiableNode(point) || !this->isEmptyNode(point)){
- match_x = false;
- }
- }
- }
- if(b.y > a.y) {
- for(i = b.y - 1; i > a.y; --i) {
- CCPoint point = CCPointMake(a.x, i);
- if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {
- match_x = false;
- }
- }
- }
- }
- bool match_y = false;
- if(a.y == b.y) {
- match_y = true;
- if(a.x > b.x) {
- for(i = a.x - 1; i > b.x; --i) {
- CCPoint point = CCPointMake(i, a.y);
- if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {
- match_y = false;
- }
- }
- }
- if(b.x > a.x) {
- for(i = b.x - 1; i > a.x; --i) {
- CCPoint point = CCPointMake(i, a.y);
- if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {
- match_y = false;
- }
- }
- }
- }
- return match_x || match_y;
- }
- //一个拐点的
- bool MapLayer::match_one_corner(cocos2d::CCPoint a, cocos2d::CCPoint b)
- {
- CCPoint point = CCPointMake(b.x, a.y);
- if( this->isValiableNode(point) && this->isEmptyNode(point) && this->match_direct(a, point) && this->match_direct(b, point)){
- return true;
- }
- point = CCPointMake(a.x, b.y);
- if( this->isValiableNode(point) && this->isEmptyNode(point) && this->match_direct(a, point) && this->match_direct(b, point)){
- return true;
- }
- return false;
- }
- //两个拐点的
- bool MapLayer::match_two_corner(cocos2d::CCPoint a, cocos2d::CCPoint b)
- {
- for(int i = a.x - 1; i >= 0; --i) {
- CCPoint point = CCPointMake(i, a.y);
- if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {
- break;
- } else {
- if (this->match_one_corner(point, b)) {
- return true;
- }
- }
- }
- for(int i = a.x + 1; i < total_x; ++i) {
- CCPoint point = CCPointMake(i, a.y);
- if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {
- break;
- } else {
- if (this->match_one_corner(point, b)) {
- return true;
- }
- }
- }
- for(int i = a.y - 1; i >= 0; --i) {
- CCPoint point = CCPointMake(a.x ,i);
- if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {
- break;
- } else {
- if (this->match_one_corner(point, b)) {
- return true;
- }
- }
- }
- for(int i = a.y + 1; i < total_y; ++i) {
- CCPoint point = CCPointMake(a.x ,i);
- if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {
- break;
- } else {
- if (this->match_one_corner(point, b)) {
- return true;
- }
- }
- }
- return false;
- }
- bool MapLayer::match(cocos2d::CCPoint a,cocos2d::CCPoint b)
- {
- if (this->match_direct(a, b)) {
- return true;
- }
- if (this->match_one_corner(a, b)) {
- return true;
- }
- if (this->match_two_corner(a, b)) {
- return true;
- }
- return false;
- }
重新写touch事件
- //
- bool MapLayer::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- CCPoint location = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
- //在这个层区域内返回true
- if (this->boundingBox().containsPoint(location))
- {
- return true;
- }
- //否则返回false,阻止接下来的touch函数
- return false;
- }
- //
- void MapLayer::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
- {
- CCPoint location = CCDirector::sharedDirector()->convertToGL(pTouch->getLocationInView());
- location = this->convertToNodeSpace(location);
- //location=CCPointMake(location.x, location.y-40);
- location=this->pointOfView(location);
- CCLOG("x:%f",location.x);
- CCLOG("y:%f",location.y);
- if (this->isValiableNode(location)==false)
- {
- return;
- }
- if (this->isEmptyNode(location))
- {
- return;
- }
- SimpleAudioEngine::sharedEngine()->playEffect("12.wav");
- if (this->isSamePoints(location, prePoint))
- {
- return;
- }
- //点击当前精灵
- CCSprite *spritecurrent=(CCSprite *)this->getChildByTag(TAG_START_SPRITE+this->indexFromPoint(location));
- spritecurrent->setScale(1.3);
- CCLOG("%d",this->indexFromPoint(location));
- if (this->isValiableNode(prePoint))
- {
- //前一个
- CCSprite *spritepre=(CCSprite *)this->getChildByTag(TAG_START_SPRITE+this->indexFromPoint(prePoint));
- if (this->canClearTwo(prePoint, location))
- {
- SimpleAudioEngine::sharedEngine()->playEffect("4.wav");
- this->clearNode(location);
- this->clearNode(prePoint);
- spritecurrent->setVisible(false);
- spritepre->setVisible(false);
- }
- else
- {
- spritepre->setScale(0.9);
- this->scaleAnimation(spritepre);
- }
- }
- prePoint=location;
- }
运行,效果如下
~~~~~这里时间有限,下一篇的文章将探讨一些自动消除,还有用粒子效果来实现消除时的动画等,还有一些其他问题~~~