转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/24729087

源码下载:http://download.csdn.net/detail/oyangyufu/7272177

进入游戏连连第一个场景,如下:

程序分析:

初始化该场景精灵所需的信息、背景/音效及图层UIbool HelloWorld::init()
{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()
{SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("class.mp3");//预加载音频文件SimpleAudioEngine::sharedEngine()->playBackgroundMusic("class.mp3", true); //循环播放音乐}UI界面的初始化
void HelloWorld::initUI()
{CCSprite *bgSprite=NULL;//添加背景图片                    if (wSize.width==1024 && wSize.height==768){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(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);}实现船移动的动画,用array保存船的3种图片状态,并执行动画 , 到两边自动返回 , 就看见船来回的开着:
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", i);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);}过10秒来个海浪的动画:
void HelloWorld::wavesAnimation(float dt)
{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);}鸟的动画,添加鸟叫声,array保存鸟的状态图片,并随机执行动画
void HelloWorld::birdsselfAnimation(float dt)
{SimpleAudioEngine::sharedEngine()->playEffect("15.wav");CCArray *array=CCArray::create();for (int i = 1; i < 4; i++){CCString *string=CCString::createWithFormat("seagull%d.png", i);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=rand()%5+1;CCRepeat *ac1=CCRepeat::create(animate, times);  //随机间隔飞翔birdsSprite->runAction(ac1);array->removeAllObjects();
}鸟移动的动画 ,飞到了左右边自动往回飞
void HelloWorld::birdsselfAnimation2()
{float yoffset=rand()%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::playBack(CCObject* pSender)
{SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();CCScene *scene=GameLayer::scene();//旋转动画CCDirector::sharedDirector()->replaceScene(CCTransitionRotoZoom::create(1.2f, scene));//CCDirector::sharedDirector()->replaceScene(CCTransitionShrinkGrow::create(1.2f, scene));}

【梦幻连连连】源码分析(一)相关推荐

  1. 【梦幻连连连】源码分析(二)

    转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/24736711 源码下载:http://download.csdn.net/detail ...

  2. 【Golang源码分析】Go Web常用程序包gorilla/mux的使用与源码简析

    目录[阅读时间:约10分钟] 一.概述 二.对比: gorilla/mux与net/http DefaultServeMux 三.简单使用 四.源码简析 1.NewRouter函数 2.HandleF ...

  3. SpringBoot-web开发(四): SpringMVC的拓展、接管(源码分析)

    [SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) SpringBoot-web开发(二): 页面和图标定制(源码分析) SpringBo ...

  4. SpringBoot-web开发(二): 页面和图标定制(源码分析)

    [SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) 目录 一.首页 1. 源码分析 2. 访问首页测试 二.动态页面 1. 动态资源目录t ...

  5. SpringBoot-web开发(一): 静态资源的导入(源码分析)

    目录 方式一:通过WebJars 1. 什么是webjars? 2. webjars的使用 3. webjars结构 4. 解析源码 5. 测试访问 方式二:放入静态资源目录 1. 源码分析 2. 测 ...

  6. Yolov3Yolov4网络结构与源码分析

    Yolov3&Yolov4网络结构与源码分析 从2018年Yolov3年提出的两年后,在原作者声名放弃更新Yolo算法后,俄罗斯的Alexey大神扛起了Yolov4的大旗. 文章目录 论文汇总 ...

  7. ViewGroup的Touch事件分发(源码分析)

    Android中Touch事件的分发又分为View和ViewGroup的事件分发,View的touch事件分发相对比较简单,可参考 View的Touch事件分发(一.初步了解) View的Touch事 ...

  8. View的Touch事件分发(二.源码分析)

    Android中Touch事件的分发又分为View和ViewGroup的事件分发,先来看简单的View的touch事件分发. 主要分析View的dispatchTouchEvent()方法和onTou ...

  9. MyBatis原理分析之四:一次SQL查询的源码分析

    上回我们讲到Mybatis加载相关的配置文件进行初始化,这回我们讲一下一次SQL查询怎么进行的. 准备工作 Mybatis完成一次SQL查询需要使用的代码如下: Java代码   String res ...

  10. [转]slf4j + log4j原理实现及源码分析

    slf4j + log4j原理实现及源码分析 转载于:https://www.cnblogs.com/jasonzeng888/p/6051080.html

最新文章

  1. 配置一台计算机所需的东西,攒一台计算机需要的配置
  2. Xamarin提示Build-tools版本过老
  3. 网站优化之如何筛选更正确高质量的关键词?
  4. ftm模块linux驱动,飞思卡尔k系列_ftm模块详解.doc
  5. Java算法之最大子序和
  6. Tensorflow:estimator训练
  7. Android屏幕尺寸(来自网络整理)
  8. asp小偷转html,ASP中实现小偷程序的原理和简单示例
  9. MySql常用SQL语句
  10. arcgis制作遥感影像标签(分割分类目标提取方向)
  11. 毕业设计——基于SSM的在线抽奖系统(构思)
  12. Java学生档案管理系统的设计与实现
  13. 大数据面试题——用shell打印200以内的质数
  14. Unity酱~ 卡通渲染技术分析(一)
  15. 神经网络中Epoch、Iteration、Batchsize相关理解和说明
  16. 回望2019,互联网产品发展只剩焦虑?
  17. 线性代数学习笔记4-1:线性方程组的数学和几何意义、零空间/解空间/核
  18. 民航飞机因何频繁遭遇乱流?
  19. lv双肩包尺寸对照表_最值得入手的10款LV双肩包包书包图片大全
  20. 来与大家分享一些我们珍藏的、有力量的语句

热门文章

  1. 使用Ranch搭建自己的TCP连接池
  2. 从键盘任意输入一个整型表示的月份值,用指针数组编程输出该月份的英文表示,若输入的月份值不在1~12之间,则输出“Illegal month”。
  3. 四个准备助力绩效面谈成功
  4. 第八周coreldraw课总结
  5. Word Xml格式 标签说明
  6. java 如何判断一个数是否为2的整数次幂以及是2的多少次方(整数幂)?
  7. Python入门 —— 02基础语法
  8. 基于S3C2440的linux-3.6.6移植——LED驱动
  9. 使用dom4j时出现的问题
  10. 网易闪电邮收信显示连接服务器,网易闪电邮IMAP协议同步网页邮箱收发邮件的使用技巧...