本文共 11609 字,大约阅读时间需要 38 分钟。
这篇文章中,我们将简单的做一个连连看的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; };HelloWorldScene.cpp
bool HelloWorld::init(){ // // 1. super init first if ( !CCLayer::init() ) { return false; } this->initData(); this->initSounds(); this->initUI(); return true;}void HelloWorld::initData(){ CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sailing_boat-hd.plist"); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("seagull-hd.plist"); boatMoveTime=45.0f; _boatDirection=kLeft; _birdsDirection=birdsLeft; }//音频文件初始化void HelloWorld::initSounds(){ }//UI界面的初始化void HelloWorld::initUI(){ CCSprite *bgSprite=NULL; if (wSize.width==1136 && wSize.height==640) { bgSprite=CCSprite::create("background_568-hd.png"); } else { bgSprite=CCSprite::create("background-hd.png"); } bgSprite->setPosition(ccp(wSize.width/2, wSize.height/2)); this->addChild(bgSprite, 0); //白云1 cloudSprite=CCSprite::create("background_cloud_1-hd.png"); cloudSprite->setAnchorPoint(ccp(0, 0)); cloudSprite->setPosition(ccp(0, wSize.height-cloudSprite->getContentSize().height)); this->addChild(cloudSprite, 1); //白云倒影 CCSprite *daoyingloudSprite=CCSprite::create("background_cloud_1-hd.png"); daoyingloudSprite->setAnchorPoint(ccp(0, 0)); //垂直翻转 daoyingloudSprite->setFlipY(true); daoyingloudSprite->setOpacity(40); daoyingloudSprite->setPosition(ccp(wSize.width-cloudSprite->getContentSize().width-40, wSize.height-cloudSprite->getContentSize().height-78*2)); this->addChild(daoyingloudSprite, 1); //白云2 cloud2Sprite=CCSprite::create("background_cloud_2-hd.png"); cloud2Sprite->setAnchorPoint(ccp(0, 0)); cloud2Sprite->setPosition(ccp(cloudSprite->getPosition().x+cloudSprite->getContentSize().width, wSize.height-cloud2Sprite->getContentSize().height)); this->addChild(cloud2Sprite, 1); //岛 CCSprite *landSprite=CCSprite::create("island-hd.png"); landSprite->setAnchorPoint(ccp(0, 0)); landSprite->setPosition(ccp(wSize.width-landSprite->getContentSize().width-20*2, wSize.height-landSprite->getContentSize().height-47*2)); this->addChild(landSprite, 1); //倒影 CCSprite *daoyinglandSprite=CCSprite::create("island-hd.png"); daoyinglandSprite->setAnchorPoint(ccp(0, 0)); daoyinglandSprite->setFlipY(true); daoyinglandSprite->setOpacity(40); daoyinglandSprite->setPosition(ccp(wSize.width-landSprite->getContentSize().width-20*2, wSize.height-landSprite->getContentSize().height-78*2)); this->addChild(daoyinglandSprite, 1); //船 boatSprite=CCSprite::createWithSpriteFrameName("sailing_boat1.png"); boatSprite->setPosition(ccp(wSize.width-boatSprite->getContentSize().width-50*2,230*2)); this->addChild(boatSprite, 1); this->addBoat(); //沙滩 CCSprite *shatanSprite=CCSprite::create("beach_adornment-hd.png"); shatanSprite->setPosition(ccp(wSize.width/2, shatanSprite->getContentSize().height/2)); this->addChild(shatanSprite, 1); if (wSize.width==1136 && wSize.height==640) { wavesSprite=CCSprite::create("background_sea1_568-hd.png"); } else { wavesSprite=CCSprite::create("background_sea1-hd.png"); } wavesSprite->setPosition(ccp(wSize.width/2, 140*2)); wavesSprite->setOpacity(0); this->addChild(wavesSprite, 1); this->schedule(schedule_selector(HelloWorld::wavesAnimation), 10); //鸟 birdsSprite=CCSprite::createWithSpriteFrameName("seagull1.png"); birdsSprite->setAnchorPoint(ccp(0, 0)); birdsSprite->setPosition(ccp(200*2,270*2)); birdsSprite->setScale(0.7); this->addChild(birdsSprite, 1); this->schedule(schedule_selector(HelloWorld::birdsselfAnimation), 5); this->birdsselfAnimation2(); // //开始菜单// CCSprite *playSprite=CCSprite::create("button_play-hd.png");// CCSprite *playSprite_s=CCSprite::create("button_play_s-hd.png");// // CCMenuItemSprite *item=CCMenuItemSprite::create(playSprite, playSprite_s, this, menu_selector(HelloWorld::playBack));// // // CCMenu *menu=CCMenu::create(item,NULL);// // item->setAnchorPoint(ccp(0.5, 0.5));// menu->setPosition(ccp(wSize.width/2,110*2));// // // this->addChild(menu, 1); }//船自身的动画void HelloWorld::addBoat(){ boatSprite->setFlipX(false); CCArray *array=CCArray::create(); for (int i = 1; i < 4; i++) { CCString *string=CCString::createWithFormat("sailing_boat%d.png"); CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString()); array->addObject(frame); } CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.5); CCAnimate *animate=CCAnimate::create(animation); CCRepeatForever *ac1=CCRepeatForever::create(animate); boatSprite->runAction(ac1); array->removeAllObjects(); //船移动动画,到两边自动返回 CCMoveTo *ac2; switch (_boatDirection) { case kLeft: ac2=CCMoveTo::create(boatMoveTime, CCPointMake(-boatSprite->getContentSize().width, boatSprite->getPosition().y)); _boatDirection=kRight; boatSprite->setFlipX(false); break; case kRight: ac2=CCMoveTo::create(boatMoveTime, CCPointMake(wSize.width, boatSprite->getPosition().y)); _boatDirection=kLeft; boatSprite->setFlipX(true); break; default: break; } CCSequence *seq=CCSequence::create(ac2,CCCallFunc::create(this, callfunc_selector(HelloWorld::resetSpritePos)),NULL); boatSprite->runAction(seq); }//重新设置船的位置void HelloWorld::resetSpritePos(){ this->addBoat();}//海浪动画void HelloWorld::wavesAnimation(){ SimpleAudioEngine::sharedEngine()->playEffect("14.wav"); CCMoveTo *ac1=CCMoveTo::create(4.0f, CCPointMake(wavesSprite->getPosition().x, 100*2)); CCFadeIn *ac2=CCFadeIn::create(4.0f); //同时进行 CCSpawn *spawn=CCSpawn::create(ac1,ac2,NULL); //队列 CCSequence *seq=CCSequence::create(spawn,CCCallFunc::create(this, callfunc_selector(HelloWorld::wavesAnimation2)),NULL); wavesSprite->runAction(seq); }void HelloWorld::wavesAnimation2(){ CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage("background_sea2_568-hd.png"); wavesSprite->setTexture(aTexture); CCMoveTo *ac1=CCMoveTo::create(4.0f, CCPointMake(wavesSprite->getPosition().x, 140*2)); CCFadeIn *ac2=CCFadeIn::create(4.0f); //同时进行 CCSpawn *spawn=CCSpawn::create(ac1,ac2,NULL); //队列 CCSequence *seq=CCSequence::create(spawn,CCCallFunc::create(this, callfunc_selector(HelloWorld::wavesAnimation3)),NULL); wavesSprite->runAction(seq); }void HelloWorld::wavesAnimation3(){ CCTexture2D *aTexture=CCTextureCache::sharedTextureCache()->addImage("background_sea1_568-hd.png"); wavesSprite->setTexture(aTexture); wavesSprite->setOpacity(0);}//鸟自身的动画void HelloWorld::birdsselfAnimation(){ SimpleAudioEngine::sharedEngine()->playEffect("15.wav"); CCArray *array=CCArray::create(); for (int i = 1; i < 4; i++) { CCString *string=CCString::createWithFormat("seagull%d.png"); CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString()); array->addObject(frame); } CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.2); animation->setRestoreOriginalFrame(true); CCAnimate *animate=CCAnimate::create(animation); int times=arc4random()%5+1; CCRepeat *ac1=CCRepeat::create(animate, times); birdsSprite->runAction(ac1); array->removeAllObjects();}//鸟飞翔的动画void HelloWorld::birdsselfAnimation2(){ float yoffset=arc4random()%40+1; CCPoint birdsPos=CCPointMake(-birdsSprite->getContentSize().width, 270*2+yoffset); CCMoveTo *ac2; switch (_birdsDirection) { case birdsLeft: ac2=CCMoveTo::create(35.0f, CCPointMake(birdsPos.x, birdsPos.y)); _birdsDirection=birdsRight; birdsSprite->setFlipX(false); break; case birdsRight: ac2=CCMoveTo::create(35.0f, CCPointMake(wSize.width, 270*2+yoffset)); _birdsDirection=birdsLeft; birdsSprite->setFlipX(true); break; default: break; } CCSpawn *spawn=CCSpawn::create(ac2,NULL); CCSequence *seq=CCSequence::create(spawn,CCCallFunc::create(this, callfunc_selector(HelloWorld::resetBirdsSpritePos)),NULL); birdsSprite->runAction(seq); }void HelloWorld::resetBirdsSpritePos(){ this->birdsselfAnimation2();}上面中,我们初始化了一些界面元素,然后做了一些简单的动画效果,整体上都是很简单的,这里就不在多说了,
~~~~~~~~
然后在函数initUI中加入初始化开始菜单
//开始菜单 CCSprite *playSprite=CCSprite::create("button_play-hd.png"); CCSprite *playSprite_s=CCSprite::create("button_play_s-hd.png"); CCMenuItemSprite *item=CCMenuItemSprite::create(playSprite, playSprite_s, this, menu_selector(HelloWorld::playBack)); CCMenu *menu=CCMenu::create(item,NULL); item->setAnchorPoint(ccp(0.5, 0.5)); menu->setPosition(ccp(wSize.width/2,110*2)); this->addChild(menu, 1);
//开始游戏void HelloWorld::playBack(){}
OK,,开始界面就完成~~~下面就我们将完成连连看的游戏界面~~~
(*^__^*) …….....................................................................................................
转载地址:https://blog.csdn.net/kuloveyouwei/article/details/12912875 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!