首页 > 代码库 > 横屏小游戏--萝莉快跑源码分析一

横屏小游戏--萝莉快跑源码分析一

Cpp文件功能介绍

GameAboutScene.cpp 关于页面
GameMainScene.cpp游戏主页面
GameMark.cpp分数
GameMenuScene.cpp游戏主菜单
GameObjHero.cpp主角
GameObjMap.cpp游戏地图
GameObjStar.cpp星星

 

菜单主页面:

 

主菜单页面背景

    CCSprite* bg = CCSprite::create("MainMenu.png");
    bg->setScale(0.5);
    bg->setPosition( ccp(size.width/2, size.height/2) );
    this->addChild(bg, 0,0);


菜单按钮及其效果,单击事件

CCMenuItemImage *newGameItem = CCMenuItemImage::create("newGameA.png", "newGameB.png",this,menu_selector(GameMenu::menuNewGameCallback)); //NEW GAME
    newGameItem->setScale(0.5);
    newGameItem->setPosition(ccp(size.width / 2 + 40,size.height / 2 - 20));
    newGameItem->setEnabled(false);	//暂不可见
    CCMenuItemImage *continueItem = CCMenuItemImage::create("continueA.png", "continueB.png",this,menu_selector(GameMenu::menuContinueCallback));  //CONTINUE
    continueItem->setScale(0.5);
    continueItem->setPosition(ccp(size.width / 2 + 40,size.height / 2 - 60));
    continueItem->setEnabled(false);
    CCMenuItemImage *aboutItem = CCMenuItemImage::create("aboutA.png", "aboutB.png",this,menu_selector(GameMenu::menuAboutCallback));  //About
    aboutItem->setScale(0.5);
    aboutItem->setPosition(ccp(size.width / 2 + 40,size.height / 2 - 100));
    aboutItem->setEnabled(false);
    soundItem = CCMenuItemImage::create("sound-off-A.png", "sound-off-B.png",this,menu_selector(GameMenu::menuSoundCallback)); //声音
    soundItem->setScale(0.5);
    soundItem->setEnabled(false);
    soundItem->setPosition(ccp(40,40));
    CCMenu* mainmenu = CCMenu::create(newGameItem,continueItem,aboutItem,soundItem,NULL);
    mainmenu->setPosition(ccp(0,0));
    this->addChild(mainmenu,1,3);
    issound = false;


进入该场景动画,菜单按钮由小至大缩放,并显示菜单

void GameMenu::onEnter(){
    CCLayer::onEnter();
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    CCNode* mainmenu = this->getChildByTag(3);
    mainmenu->setScale(0);
    mainmenu->runAction(CCSequence::create(CCScaleTo::create(0.5,1),CCCallFuncN::create(this, callfuncN_selector(GameMenu::menuEnter)),NULL));
}
void GameMenu::menuEnter(CCNode* pSender ){
    CCNode* mainmenu = this->getChildByTag(3);
    CCArray* temp = mainmenu->getChildren();
    for(int i = 0;i < temp->count();i ++)
        ((CCMenuItemImage *)temp->objectAtIndex(i))->setEnabled(true);
}


菜单按钮单击事件切换场景
切换游戏主页面

void GameMenu::menuNewGameCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->setDepthTest(true);
    CCDirector::sharedDirector()->replaceScene(CCTransitionPageTurn::create(0.5,GameMain::scene(), false));
}
void GameMenu::menuContinueCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->setDepthTest(true);
    CCDirector::sharedDirector()->replaceScene(CCTransitionPageTurn::create(0.5,GameMain::scene(), false));
}


切换关于页面

void GameMenu::menuAboutCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->setDepthTest(true);
    CCDirector::sharedDirector()->replaceScene(CCTransitionPageTurn::create(0.5,GameAbout::scene(), false));
}


背景音乐设置

void GameMenu::menuSoundCallback(CCObject* pSender)
{
	if(!issound)//开
	{
		soundItem->setNormalImage(CCSprite::create("sound-on-A.png"));
		soundItem->setDisabledImage(CCSprite::create("sound-on-B.png"));
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(CCFileUtils::sharedFileUtils()->fullPathForFilename("background.mp3").c_str(), true);
		issound = true;
	}
	else//关
	{
		soundItem->setNormalImage(CCSprite::create("sound-off-A.png"));
		soundItem->setDisabledImage(CCSprite::create("sound-off-B.png"));
		CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
		issound = false;
	}

}