Cocos2d-x 3.2 大富翁游戏项目开发-第三部分 菜单场景


上一部分介绍到片头动画介绍后进入到菜单场景,场景效果如图




MenuScene.h头文件如下:


class MenuScene : public LayerColor  
{
public:

    static Scene* createScene();
  
    virtual bool init();        
 
    CREATE_FUNC(MenuScene);

private:
	Size visibleSize; //窗口尺寸
	LabelTTF* settingsGameTTF; //场景文字
	void menuTouchDown (Object* pSender,Control::EventType event);// Menu点击回调方法
	void addBackgroundSprite(); //添加场景背景方法
	void addMenuSprites();//添加Menu方法
	 
};

#endif


MenuScene.cpp部分代码如下:



bool MenuScene::init()
{
	if ( !LayerColor::initWithColor(Color4B(255, 255, 255, 255)) )
    {
        return false;
    }
	visibleSize = Director::getInstance()->getVisibleSize();
                Vec2 origin = Director::getInstance()->getVisibleOrigin();

	addBackgroundSprite();//添加场景背景方法

	addMenuSprites(); //添加Menu方法


    return true;
}


void MenuScene::addBackgroundSprite()
{
                 //添加大富翁背景图片,居中显示
	Sprite* menuSpriteLogo= Sprite::create(MENU_LOGO);
	menuSpriteLogo->setPosition(ccp(visibleSize.width/2,visibleSize.height));
	menuSpriteLogo->setAnchorPoint(ccp(0.5,1));
	menuSpriteLogo->setScale(0.6f);
	addChild(menuSpriteLogo);


             //添加左边彩虹图片
	Sprite* rainBowSprite= Sprite::create(RAINBOW);
	rainBowSprite->setPosition(ccp(5,visibleSize.height-20));
	rainBowSprite->setAnchorPoint(ccp(0,1));
	rainBowSprite->setScale(0.3f);
	addChild(rainBowSprite);

               //让彩虹图片左右移动
	MoveBy* rainBowMove = MoveBy::create(1,ccp(8,0));
	MoveBy* rainBowMoveReverse = rainBowMove->reverse();
	Sequence* rainBowAction = Sequence::create(rainBowMove,rainBowMoveReverse,NULL);
	rainBowSprite->runAction(RepeatForever::create(rainBowAction));
}


void MenuScene:: addMenuSprites()
{
              //添加单机游戏Menu  
	Scale9Sprite* btnNormal = Scale9Sprite::create(NORMAL_MENU); //设置菜单normal图片
	Scale9Sprite* btnPress = Scale9Sprite::create(PRESS_MENU);// 设置菜单press图片

	LabelTTF* singleGameTTF = LabelTTF::create(SINGLE_GAME ,FONT_MENU,Btn_FontSize);//创建菜单所需的Label对象
	ControlButton* singleGameBtn = ControlButton::create(singleGameTTF,btnNormal);//创建controlButton
	singleGameBtn->setBackgroundSpriteForState(btnPress, Control::State::SELECTED);//添加singleButton菜单的press效果图片

	singleGameBtn->setPosition(ccp(visibleSize.width/2,visibleSize.height-200));//设置位置
	singleGameBtn->setPreferredSize(Size(Btn_Width,Btn_Height));//设置大小
	singleGameBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(MenuScene::menuTouchDown),Control::EventType::TOUCH_DOWN);//设置点击回调方法
	singleGameBtn->setTag(Btn_Single_Game_TAG);//设置Tag
	addChild(singleGameBtn);//添加menu

             //其他menu添加方法,同上类似,不再重复.
	………………………..

               //settings菜单不大一样,该菜单点击都有打开或关闭音效。
	Scale9Sprite* btnNormal3 = Scale9Sprite::create(NORMAL_MENU);
	Scale9Sprite* btnPress3 = Scale9Sprite::create(PRESS_MENU);

	bool music_on = UserDefault::getInstance()->getBoolForKey(MUSIC_ON_KEY,true);//获取音效设置
LabelTTF* settingsGameTTF;
//如果音效开,则menu显示on,如果关,显示off
	if(music_on)
	{
		settingsGameTTF = LabelTTF::create(MUSIC_ON,FONT_MENU,Btn_FontSize);
	}else
	{
                              settingsGameTTF = LabelTTF::create(MUSIC_OFF,FONT_MENU,Btn_FontSize);
	}
	
	ControlButton* settingsGameBtn = ControlButton::create(settingsGameTTF,btnNormal3);
	settingsGameBtn->setBackgroundSpriteForState(btnPress3, Control::State::SELECTED);

	settingsGameBtn->setPosition(ccp(visibleSize.width/2,visibleSize.height-320));
	settingsGameBtn->setPreferredSize(Size(Btn_Width,Btn_Height));
	settingsGameBtn->addTargetWithActionForControlEvents(this,cccontrol_selector(MenuScene:: menuTouchDown),Control::EventType::TOUCH_DOWN);//添加setting回调
	settingsGameBtn->setTag(Btn_Music_TAG);
	addChild(settingsGameBtn);
                ………………………
}


Menu点击的回调方法,根据点击对象tag调用相应方法,此处主要的是case语句需要添加 { } 括号,否则编译报错


void MenuScene:: menuTouchDown(Object* pSender,Control::EventType event)
{
	log("single touched");
	ControlButton* button = (ControlButton*)pSender;
	int tag = button->getTag();
switch(tag)
	{
		case Btn_Single_Game_TAG:
			{
				log("single game");
				//Director::getInstance()->pushScene(MapChooseScene::createScene());
			}

		case Btn_Multi_Game_TAG:
			{
				log("multi game");
				break;
			}

		case Btn_Music_TAG:
			{
				bool music_on = UserDefault::getInstance()->getBoolForKey(MUSIC_ON_KEY,true);

				if(music_on)
				{
					UserDefault::getInstance()->setBoolForKey(MUSIC_ON_KEY,false);//设置音效关
					button->setTitleForState(MUSIC_OFF,Control::State::NORMAL);//让menu文字显示off
				}else
				{
					UserDefault::getInstance()->setBoolForKey(MUSIC_ON_KEY,true);//设置音效开
					button->setTitleForState(MUSIC_ON,Control::State::NORMAL);// 让menu文字显示on
				}
		
				break;
			}
		case Btn_Quit_Game_TAG:
			{
				log("quit game");
				//popupLayer();弹出退出对话框,代码较多,后续编写
				break;
			}
	   default:  
		   break;
	}

代码比较简单  


未完待续...............


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值