首页 > 代码库 > 【雷电】源码分析(一)-- 进入游戏开始界面

【雷电】源码分析(一)-- 进入游戏开始界面

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

进入游戏开始界面效果图:


源码分析:

StartMenu场景--进入游戏开始界面
初始化场景,包括初始Android按键,添加背景图片,添加logo图片,创建游戏菜单开始/设置/关于并添加其响应事件,
初始化飞船漂移动画,背景音乐
程序分析:
bool StartMenu::init()
{


    if ( !CCLayer::init() )
    {
        return false;
    }
    
    // 添加Android的按键处理,这里加了全局代理
    this->setKeypadEnabled(true);
    CostomeKeyPadDelegae *keyDelegate = new CostomeKeyPadDelegae();
    keyDelegate->autorelease();
    CCDirector::sharedDirector()->getKeypadDispatcher()->addDelegate(keyDelegate);
    
    winSize = CCDirector::sharedDirector()->getWinSize();
    
	//添加背景图片
    CCSprite* sl = CCSprite::create(s_loading);
    sl->setAnchorPoint(ccp(0, 0));
    this->addChild(sl,0,1);


    //添加logo图片
    CCSprite* logo = CCSprite::create(s_logo);
    logo->setAnchorPoint(ccp(0.5, 1));
    logo->setPosition(ccp(winSize.width/2, winSize.height-50));
    this->addChild(logo, 10, 1);
    
	//创建游戏菜单开始/设置/关于并添加其响应事件
    CCSprite* newGameNormal = CCSprite::create(s_menu, CCRectMake(0, 0, 126, 33));
    CCSprite* newGameSelected = CCSprite::create(s_menu, CCRectMake(0, 33, 126, 33));
    CCSprite* newGameDisabled = CCSprite::create(s_menu, CCRectMake(0, 33*2, 126, 33));
    
    CCSprite* gameSettingNormal = CCSprite::create(s_menu, CCRectMake(126, 0, 126, 33));
    CCSprite* gameSettingNSelected = CCSprite::create(s_menu, CCRectMake(126, 33, 126, 33));
    CCSprite* gameSettingDesabled = CCSprite::create(s_menu, CCRectMake(126, 33*2, 126, 33));
    
    CCSprite* aboutNormal = CCSprite::create(s_menu, CCRectMake(252, 0, 126, 33));
    CCSprite* aboutSelected = CCSprite::create(s_menu, CCRectMake(252, 33, 126, 33));
    CCSprite* aboutDesabled = CCSprite::create(s_menu, CCRectMake(252, 33*2, 126, 33));
    
    
    CCMenuItemSprite* newGame = CCMenuItemSprite::create(newGameNormal, newGameSelected, newGameDisabled, this, menu_selector(StartMenu::flareEffect));


    CCMenuItemSprite* gameSetting = CCMenuItemSprite::create(gameSettingNormal, gameSettingNSelected, gameSettingDesabled, this, menu_selector(StartMenu::menuCallback));


    gameSetting->setTag(20);
    CCMenuItemSprite* about = CCMenuItemSprite::create(aboutNormal, aboutSelected, aboutDesabled, this, menu_selector(StartMenu::menuCallback));
    about->setTag(21);


    // 最后一个参数要是NULL
    CCMenu* menu = CCMenu::create(newGame, gameSetting, about, NULL);
    menu->alignItemsVerticallyWithPadding(20);//垂直间隔排列
    this->addChild(menu, 1, 2);
    menu->setPosition(ccp(winSize.width / 2, winSize.height / 2 - 80));
    this->schedule(schedule_selector(StartMenu::update), 0.1);//添加定时器,0.1‘一次
    
    // ships ,飞船漂移动画 
    CCTexture2D *textCache = CCTextureCache::sharedTextureCache()->addImage(s_ship01);
    m_ship = CCSprite::createWithTexture(textCache, CCRectMake(0, 45, 60, 38));
    this->addChild(m_ship, 0, 4);
    CCPoint position = ccp(CCRANDOM_0_1() * winSize.width, 0);
    m_ship->setPosition(position);
    m_ship->runAction(CCMoveBy::create(2, ccp(CCRANDOM_0_1() * winSize.width, position.y + winSize.height + 100)));


    //背景音乐
    if (Config::sharedConfig()->getAudioState()) {
        SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.7);
        SimpleAudioEngine::sharedEngine()->playBackgroundMusic(s_mainMainMusic, true);


    }
    
    return true;
}


游戏菜单开始键响应事件,先显示光晕效果然后切换GameLayer游戏场景
//光晕效果
void StartMenu::flareEffect(CCObject *pObject)
{
    onButtonEffect();
    Effect* flareEffect = Effect::create();
    CCCallFunc *callback =  CCCallFunc::create(this, callfunc_selector(StartMenu::newGame));
    flareEffect->flareEffect(this, callback);
}
//切换GameLayer场景
void StartMenu::newGame()
{


    CCScene *scene = CCScene::create();
    scene->addChild(GameLayer::create());
    CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2, scene));


}
游戏菜单设置、关于键响应事件,根据获取tag值,切换相应场景
void StartMenu::menuCallback(CCObject* pSender)
{
    onButtonEffect();
    int tag = dynamic_cast<CCNode*>(pSender)->getTag();//获取tag
    if (tag == 20) {//设置事件
        CCScene *scene = Options::scene();
        CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2, scene));


    }
    else if(tag == 21) //声音事件
    {
        CCScene *scene = About::scene();
        CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2, scene));
    }
    
}


0.1‘一次的定时器,执行飞船从底部随机向上底部移动
void StartMenu::update(float dt)
{
    if (m_ship->getPosition().y > winSize.height) {
        CCPoint pos = ccp(CCRANDOM_0_1() * winSize.width, 10);
        m_ship->setPosition(pos);
        m_ship->runAction(CCMoveBy::create(floor(5 * CCRANDOM_0_1()), ccp(CCRANDOM_0_1() * winSize.width, pos.y + winSize.height)));
        
    }
}