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

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

时间:2017-07-30 20:07:24      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:cts   ccf   index   pat   进入   音乐   send   cclayer   包含   

转载请注明出处: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();
}


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

标签:cts   ccf   index   pat   进入   音乐   send   cclayer   包含   

原文地址:http://www.cnblogs.com/lytwajue/p/7260011.html

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