[Cocos2d-x for WP8学习笔记]CCMenu,CCLayerColor,CCLayerGradient

Posted by baicai on July 13, 2013

Cocos2d-x内置了三种特殊的CCLayer

CCLayerColor

一个色块

CCLayerGradient

一个渐变色块

CCMenu

游戏菜单

将之前建立的启动界面做下修改,在init中添加菜单:

186-1

bool MainScene::init()
{
 bool bRet = false;
 do 
 {
  if ( !CCLayer::init() )
  {
   break;
  }

  CCSize size = CCDirector::sharedDirector()->getWinSize();
  
  CCSprite* back = CCSprite::create("Background/main_back.jpg");
  back->setPosition(ccp(size.width * 0.5, size.height * 0.5));
  this->addChild(back);

  CCMenuItem* start=CCMenuItemFont::create("Start",this,menu_selector(MainScene::startGame));
  CC_BREAK_IF(!start);
  start->setPosition(ccp((size.width)/2, (size.height)/2));
  
  CCMenuItem* exit=CCMenuItemFont::create("Exit",this,menu_selector(MainScene::exitGame));
  CC_BREAK_IF(!exit);
  exit->setPosition(ccp((size.width)/2, (size.height)/2-70));

  CCMenu* menu = CCMenu::create(start,exit,NULL);
  CC_BREAK_IF(!menu);
  menu->setPosition(CCPointZero);
  this->addChild(menu);

  bRet = true;
 } while (0);

 return bRet;
}

添加响应事件:

void MainScene::startGame(CCObject* send)
{
 CCDirector::sharedDirector()->replaceScene(GameScene::scene());
}
void MainScene::exitGame(CCObject* send)
{
 CCDirector::sharedDirector()->end();//这里会报异常,不知道为啥
}

在游戏主界面GameScene中利用CCLayerColor初始化界面

CCScene* GameScene::scene()
{
 CCScene * scene = NULL;
 do 
 {
  scene = CCScene::create();
  CC_BREAK_IF(! scene);

  CCLayerColor *layer = CCLayerColor::create(ccc4(255, 0, 255, 244));
  CC_BREAK_IF(! layer);

  scene->addChild(layer);
 } while (0);

 return scene;
}