雷电源代码分析-- 进入游戏開始界面

Posted lytwajue

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了雷电源代码分析-- 进入游戏開始界面相关的知识,希望对你有一定的参考价值。

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

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

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

技术分享

源代码分析:

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

设置界面效果:

技术分享

源代码分析:

Options设置场景初始化背景图片、背景音乐、设置菜单
bool Options::init()
{
    if (!CCLayer::init()) {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    
    CCSprite *sp = CCSprite::create(s_loading);//背景图片
    sp->setAnchorPoint(CCPointZero);
    addChild(sp, 0, 1);
    
	//依据纹理创建OPTION
    CCTexture2D *imageCache = CCTextureCache::sharedTextureCache()->addImage(s_menuTitle);
    CCSprite *title = CCSprite::createWithTexture(imageCache, CCRectMake(0, 0, 134, 34));
    title->setPosition(ccp(winSize.width/2, winSize.height - 60));
    addChild(title);

	//创建设置开关、上一步菜单button,并注冊响应事件
    CCMenuItemFont *menuTitle = CCMenuItemFont::create("Sound");
    menuTitle->setFontName("Arial");
    menuTitle->setFontSize(18);
    menuTitle->setEnabled(false);
    
    CCMenuItemFont::setFontName("Arial");
    CCMenuItemFont::setFontSize(26);
    CCMenuItemToggle *toggle = CCMenuItemToggle::createWithTarget(this, menu_selector(Options::setOptions), CCMenuItemFont::create("On"),CCMenuItemFont::create("Off"), NULL);//创建点击之后on/off状态切换
    
	//设置音频状态,默觉得On
    int selectId = Config::sharedConfig()->getAudioState()? 0 : 1;
    toggle->setSelectedIndex(selectId);
    
	//创建Go Back按键并注冊事件
    CCLabelBMFont *backLb = CCLabelBMFont::create("Go Back", s_font);//渲染字体
    CCMenuItemLabel *goBack = CCMenuItemLabel::create(backLb, this, menu_selector(Options::goBack));//按键事件
    goBack->setScale(0.6);
	//字体动画。弱隐弱现
    CCActionInterval *fadeIn = CCFadeTo::create(1, 80);
    CCActionInterval *fadeOut = CCFadeTo::create(1, 255);
    CCEaseSineInOut *ease1 = CCEaseSineInOut::create(fadeIn);
    CCEaseSineInOut *ease2 = CCEaseSineInOut::create(fadeOut);
    CCFiniteTimeAction *seq = CCSequence::create(ease1, ease2, NULL);
    goBack->runAction(CCRepeatForever::create((CCActionInterval*)seq));
    
    CCMenu *menu = CCMenu::create( menuTitle, toggle, goBack, NULL);
    menu->alignItemsInColumns(2,1);//菜单布局
    addChild(menu);

    
    CCPoint pos =  goBack->getPosition();
    pos.y -= 50;
    goBack->setPosition(pos);
    
    
    return true;
}

音频设置开/关
void Options::setOptions(CCObject *pObject)
{
    bool tmpSound = Config::sharedConfig()->getAudioState();
    Config::sharedConfig()->updateAudioState(!tmpSound);
    
    if (Config::sharedConfig()->getAudioState()) {
        SimpleAudioEngine::sharedEngine()->resumeAllEffects();
        SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
    }else{
        SimpleAudioEngine::sharedEngine()->pauseAllEffects();
        SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
    }
    
}

上一步
void Options::goBack(cocos2d::CCObject *pSender)
{
    CCScene *scene = StartMenu::scene();
    CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2, scene));

}

关于界面效果:

技术分享

程序分析:

About场景创建背景图片、关于文本、上一步button
bool About::init()
{
    if (!CCLayer::init()) {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    
    CCSprite *sp = CCSprite::create(s_loading);//创建背景图片
    sp->setAnchorPoint(ccp(0, 0));
    addChild(sp, 0, 1);

    //依据纹理创建About
    CCTexture2D *imageCache = CCTextureCache::sharedTextureCache()->addImage(s_menuTitle);
    CCSprite *title = CCSprite::createWithTexture(imageCache, CCRectMake(0, 36, 100, 34));
    title->setPosition(ccp(winSize.width/2, winSize.height - 60));
    addChild(title);
    
	//创建关于文本。向左对齐
    CCLabelTTF *about = CCLabelTTF::create("   I recode this game according to the logic of MoonWarriors-html5,almost all of the code  keep unanimous with the original. \n     This showcase utilizes many features from Cocos2d-x engine, including: Parallax background, tilemap, actions, ease, frame animation, schedule, Labels, keyboard Dispatcher, Scene Transition", "Arial", 18, CCSizeMake(winSize.width * 0.85, 320), kCCTextAlignmentLeft);
    about->setPosition(ccp(winSize.width / 2, winSize.height / 2 - 20));
    about->setAnchorPoint(ccp(0.5, 0.5));
    addChild(about);
    
    //创建Go Back按键并注冊事件
    CCLabelBMFont *backLb = CCLabelBMFont::create("Go Back", s_font);
    CCMenuItemLabel *goBack = CCMenuItemLabel::create(backLb, this, menu_selector(About::goBack));
    goBack->setScale(0.6);
	//字体动画,弱隐弱现
    CCActionInterval *fadeIn = CCFadeTo::create(1, 80);
    CCActionInterval *fadeOut = CCFadeTo::create(1, 255);
    CCEaseSineInOut *ease1 = CCEaseSineInOut::create(fadeIn);
    CCEaseSineInOut *ease2 = CCEaseSineInOut::create(fadeOut);
    CCFiniteTimeAction *seq = CCSequence::create(ease1, ease2, NULL);
    
    goBack->runAction(CCRepeatForever::create((CCActionInterval*)seq));
    
    CCMenu *menu = CCMenu::create(goBack, NULL);
    menu->setPosition(winSize.width / 2, 50);
    addChild(menu);
    
    return  true;
}

上一步
void About::goBack(CCObject *pObject)
{
    CCScene *scene = StartMenu::scene();
    CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2, scene));
}

光晕效果图:

技术分享

程序分析:

创建光晕动画
void Effect::flareEffect(CCNode *parent, CCCallFunc *callback)
{
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
  
    CCSprite *flare = CCSprite::create(s_flare);//光晕图片
    
    ccBlendFunc cbl = {GL_SRC_ALPHA, GL_ONE};// 设置混合模式
    flare->setBlendFunc(cbl);
    parent->addChild(flare, 10);
    
    flare->setOpacity(0);//设置透明度
    
    flare->setPosition(ccp(-30, winSize.height - 130));//设置位置

    flare->setRotation(-120);//设置角度
    
    flare->setScale(0.2);
    
    // 透明度渐变
    CCActionInterval *opacityAnim = CCFadeTo::create(0.5, 255);
    CCActionInterval *opacDim = CCFadeTo::create(1, 0);
    
    // 大小渐变
    CCActionInterval *bigAnim = CCScaleBy::create(0.7, 1.2, 1.2);
    
    // 渐变速度
    CCEaseSineOut *biggerEase = CCEaseSineOut::create(bigAnim);
    CCActionInterval *moveAnim = CCMoveBy::create(0.5, ccp(328, 0));
    CCEaseSineOut *moveEase = CCEaseSineOut::create(moveAnim);
    
    // 角度旋转
    CCActionInterval *roteAnim = CCRotateBy::create(2.5, 90);
    
    // 角度旋转速度,指数变化
    CCEaseExponentialOut * rotateEase = CCEaseExponentialOut::create(roteAnim);
    
    // 放大到原大小
    CCScaleTo *bigger = CCScaleTo::create(0.5, 1);
    
    // 动画完毕后回调函数,清除精灵
    CCCallFuncN *removeFunc =  CCCallFuncN::create(flare, callfuncN_selector(Effect::killSprite));
    
    // 运行动画和回调函数切换GameLayer场景
    flare->runAction(CCSequence::create(opacityAnim, biggerEase, opacDim, removeFunc, callback,  NULL));
    flare->runAction(moveEase);
    flare->runAction(rotateEase);
    flare->runAction(bigger);

}

void Effect::killSprite(CCNode *pSender)
{
    pSender->removeFromParent();
}











以上是关于雷电源代码分析-- 进入游戏開始界面的主要内容,如果未能解决你的问题,请参考以下文章

基于java的雷电游戏

如何用雷电模拟器玩taptap

雷电模拟器改定位有用吗

cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第一步---開始界面&amp;关卡选择

微信小程序代码片段

Cocos2d-x《雷电大战》-子弹无限发射