首页 > 代码库 > 【梦幻连连连】源码分析(二)

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

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

GameLayer场景界面效果:

 

 

源码分析:

 

GameLayer场景初始化,主要是初始化加载界面及背景音乐
bool GameLayer::init()
{
 float dt=0.0f;
    
    if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)))
    {
        return false;
    }
    
    this->initLoadingUI();
    
    //循环播放三首背景音乐
    this->playSound1(dt);
        
    return true;
}
加载界面
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();  //加载时动作
}
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();  //初始化本场景UI
}
初始关卡数据、等级数值
void GameLayer::initData()
{
 char st[10];
    
    //dialogLevelString=CCString::create(UTEXT("第一关"));
    dialogLevelString=CCString::create("第一关");
 ++_level_num;
 CCLOG("_level_num: %d", _level_num);
 sprintf(st, "%d", _level_num) ;
    _level=CCString::create(st);
}
本场景UI初始化,基本跟上一个场景差不多
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(); //关卡对话框
    
    //添加杯子图标
    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);
    
    
    //添加等级精灵
    levelnumsAtlas=CCLabelAtlas::create(_level->getCString(), "small_blue_number_level-hd.png",16, 20, ‘0‘);
    
    levelnumsAtlas->setScale(1.2f);
    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));//菜单2种状态转换
    
    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();
    
    
    //添加进度条背景
    progressbgSprite=CCSprite::create("time_slot-hd.png");
    
    progressbgSprite->setAnchorPoint(ccp(0, 0));
    progressbgSprite->setPosition(ccp(120, 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(120, wSize.height-55));
    
    //添加效果
    progress->setMidpoint(ccp(0, 0));
    
    //添加进度条宽高变化
    progress->setBarChangeRate(ccp(1, 0));
    
    progress->setPercentage(100);
    
    this->addChild(progress, 1);
    
    progress->setVisible(false);
    
    //添加进度条数值100
    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);
    
    
    //添加暂停界面
    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)); //继续菜单
	pauseLayer->restartitem->setTarget(this,menu_selector(GameLayer::restartPressed)); //重启菜单
   
    //布局连连看地图
    mapLayer=MapLayer::create();
    
    mapLayer->setPosition(120,20);
    
    this->addChild(mapLayer, 1);
    
    
    mapLayer->setVisible(false);   
    
}
初始化关卡弹出框
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", 50);
    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);
}

暂停对话框及各按键的事件处理:


源码分析:

暂停菜单
void GameLayer::pausePressed(CCObject* pSender)
{  
   // CCLOG("111111111111");
   // 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::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);
    
    progressbgSprite->setVisible(true);
    progress->setVisible(true);
    numsTTF->setVisible(true);
    
    mapLayer->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));
    
    
    //系统刷新
    //this->scheduleUpdate();
        
    this->schedule(schedule_selector(GameLayer::update), 1);
    
    
}																	
返回键,切换上一场景
void GameLayer::backPressed(CCObject* pSender)
{

    this->gameRestart();

    SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
    SimpleAudioEngine::sharedEngine()->playEffect("2.wav");
    
    CCScene *scene=HelloWorld::scene();
    
    //旋转动画
    CCDirector::sharedDirector()->replaceScene(CCTransitionFlipX::create(1.2f, scene));

    
}
进度表倒计时
void GameLayer::update(float dt)
{
	int cu=progress->getPercentage(); 
	if (cu>0)//倒记数
	{
		cu=cu-1.0f;
		progress->setPercentage(cu); 
		CCString *str = CCString::createWithFormat("%d",cu);
		numsTTF->setString(str->getCString());
	}
	if (cu==0)//闯关失败
	{
		this->unschedule(schedule_selector(GameLayer::update));
		CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("lose.mp3");


		//CCLabelTTF *label = CCLabelTTF::create(UTEXT("闯关失败"), "Arial", 36.0);
		CCLabelTTF *label = CCLabelTTF::create("闯关失败", "Arial", 50);


		CCSize s = CCDirector::sharedDirector()->getWinSize();
		label->setPosition(ccp(s.width/2, s.height/2));
		this->addChild(label);
		this->scheduleOnce(schedule_selector(GameLayer::exitGame), 1.0);
	}


    
}
void GameLayer::exitGame(float dt)
{
	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(CCObject* pSender)
{
    
    
    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);
    
    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));

}
重新开始键
void GameLayer::restartPressed(CCObject* pSender)
{
	this->gameRestart();
	SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
	SimpleAudioEngine::sharedEngine()->playEffect("2.wav");


	CCScene *scene=GameLayer::scene();


	//旋转动画
	CCDirector::sharedDirector()->replaceScene(CCTransitionRotoZoom::create(1.2f, scene));


}

关闭背景音乐
void PauseLayer::musicPressed(CCObject* pSender)
{
	if (SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
	{


		SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();


		CCSprite *musicSprite=CCSprite::create("button_music_off-hd.png");


		CCSprite *musicSprite_s=CCSprite::create("button_music_off-hd.png");


		CCMenuItemSprite *musicitem=CCMenuItemSprite::create(musicSprite, musicSprite_s, this, menu_selector(PauseLayer::musicPressed2));


		CCMenu *musicmenu=CCMenu::create(musicitem,NULL);


		musicitem->setAnchorPoint(ccp(0, 0));


		musicmenu->setPosition(ccp(127*2,66*2));


		this->addChild(musicmenu, 1);
	}


}


void PauseLayer::musicPressed2(CCObject* pSender)
{
	
	SimpleAudioEngine::sharedEngine()->rewindBackgroundMusic();
	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);
}