码迷,mamicode.com
首页 > 其他好文 > 详细

【梦幻连连连】源码分析(一)

时间:2014-04-30 22:34:40      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:cocos2d   连连看   

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


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

 mamicode.com,码迷

 

程序分析:

初始化该场景精灵所需的信息、背景/音效及图层UI

bool 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));
    
}


 


 

 

 

 

 

 

【梦幻连连连】源码分析(一)

标签:cocos2d   连连看   

原文地址:http://blog.csdn.net/oyangyufu/article/details/24729087

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!