秘籍场景:
这一部分的主要内容也是三点:•Menu家族及其成员构成•Menu及各个成员的特点•主开始菜单场景的分析和实现下面是菜单Menu和菜单项MenuItem类图:
他们的关系就如名字一样,一个是容器Menu,一个是内容Item。下面引用一段话:
StartLayer.h
#ifndef __StartLayer_H__
#define __StartLayer_H__
#include "cocos2d.h"
#include "cocos-ext.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
USING_NS_CC_EXT;
class StartLayer : public Layer
{
public:
virtual bool init();
static Scene* createScene();
CREATE_FUNC(StartLayer);
private:
//开始主界面的按钮控件监控事件
void touchSet(Ref* pSender);
void touchLib(Ref* pSender);
void touchMiJi(Ref* pSender);
void touchCG(Ref* pSender);
void touchTZ(Ref* pSender);
void touchClose(Ref* pSender);
void touchHelp(Ref* pSender);
//图片精灵
Sprite* title;
Sprite* bgPic;
};
#endif
StartLayer.cpp
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
#include "GlobalDefine.h"
#include "StartLayer.h"
#include "HelloWorldScene.h"
using namespace cocos2d;
using namespace CocosDenshion;
Scene* StartLayer::createScene()
{
Scene* startScene = Scene::create();
StartLayer* layer = StartLayer::create();
startScene->addChild(layer);
return startScene;
}
bool StartLayer::init()
{
if(!Layer::init())
{
return false;
}
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("pnglist/galleryLayer.plist");
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("pnglist/monster.plist");
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("pnglist/resultLayer.plist");
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("pnglist/mapBg.plist");
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("pnglist/mapMid.plist");
if(getBoolFromXML(MUSIC_KEY))
{
float music = getFloatFromXML(MUSICVOL)*100.0f;
aduioEngine->setBackgroundMusicVolume(getFloatFromXML(MUSICVOL));
if(SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())
{
aduioEngine->pauseBackgroundMusic();
aduioEngine->playBackgroundMusic("Sound/startBGM.mp3",true);
}
else
aduioEngine->playBackgroundMusic("Sound/startBGM.mp3",true);
}
else
aduioEngine->pauseBackgroundMusic();
// 精灵初始化及位置设定
title = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("Title.png"));
title->setPosition(WINSIZE.width / 2 - 222, WINSIZE.height / 2 + 186);
bgPic = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("MainMenuBackground.png"));
bgPic->setPosition(WINSIZE.width / 2, WINSIZE.height / 2);
this->addChild(bgPic);
this->addChild(title);
// 按钮初始化以及时间绑定
auto helpItem = MenuItemSprite::create(
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("HelpNormal.png")),
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("HelpSelected.png")),
CC_CALLBACK_1(StartLayer::touchHelp, this)); // 帮助
auto tujiItem = MenuItemSprite::create(
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("PhotoGalleryNormal.png")),
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("PhotoGallerySelected.png")),
CC_CALLBACK_1(StartLayer::touchLib, this)); // 图籍
auto setItem = MenuItemSprite::create(
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("SetNormal.png")),
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("SetSelected.png")),
CC_CALLBACK_1(StartLayer::touchSet, this)); // 设置
auto mijiItem = MenuItemSprite::create(
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("CheatsNormal.png")),
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("CheatsSelected.png")),
CC_CALLBACK_1(StartLayer::touchMiJi, this)); // 秘籍
auto chuangguanItem = MenuItemSprite::create(
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("EmigratedNormal.png")),
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("EmigratedSelected.png")),
CC_CALLBACK_1(StartLayer::touchCG, this)); // 闯关
auto tiaozhanItem = MenuItemSprite::create(
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("ChallengeNormal.png")),
Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("ChallengeSelected.png")),
CC_CALLBACK_1(StartLayer::touchTZ, this)); // 挑战
tujiItem->setPosition(WINSIZE.width - 62, WINSIZE.height - 73);
mijiItem->setPosition(WINSIZE.width - 62, WINSIZE.height - 209);
setItem->setPosition(WINSIZE.width - 62, WINSIZE.height - 346);
helpItem->setPosition(WINSIZE.width - 62, WINSIZE.height - 473);
chuangguanItem->setPosition(WINSIZE.width / 2 - 240, WINSIZE.height / 2 - 86);
tiaozhanItem->setPosition(WINSIZE.width / 2 - 240, WINSIZE.height / 2 - 250);
auto menu = Menu::create(tujiItem,mijiItem, setItem, helpItem, chuangguanItem, tiaozhanItem, NULL);
menu->setPosition(Point::ZERO);
this->addChild(menu, 2);
return true;
}
// 按钮事件实现
void StartLayer::touchSet(Ref* pSender)
{
PLAYEFFECT;
//Director::getInstance()->replaceScene(SetLayer::createScene());
}
void StartLayer::touchLib(Ref* pSender)
{
PLAYEFFECT;
//Director::getInstance()->replaceScene(TujiLayer::createScene());
}
void StartLayer::touchMiJi(Ref* pSender)
{
PLAYEFFECT;
//Director::getInstance()->replaceScene(MijiLayer::createScene());
}
void StartLayer::touchCG(Ref* pSender)
{
if (getBoolFromXML(SOUND_KEY))
{
aduioEngine->setEffectsVolume(getFloatFromXML(SOUNDVOL));
aduioEngine->playEffect("Sound/button.mp3");
}
Director::getInstance()->replaceScene(HelloWorld::createScene());
}
void StartLayer::touchTZ(Ref* pSender)
{
PLAYEFFECT;
//Director::getInstance()->replaceScene(GateMapLayer::createScene());
}
void StartLayer::touchHelp(Ref* pSender)
{
PLAYEFFECT;
//Director::getInstance()->replaceScene(HelpLayer::createScene());
}
*图片来源见水印