3.cocos2dx之Menu,通过menu来实现场景的切换



1 文件

TMenu.h

#ifndef __TMENU_H__

#define __TMENU_H__

#include "cocos2d.h"

USING_NS_CC;

class TMenu :public CCLayer

{

public:

static CCScene * scene();

CREATE_FUNC(TMenu);

bool init();

CCMenu * menu;

void menuCallback(CCObject * sender);

void closeCallback(CCObject * sender);

virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);

virtual void ccTouchMoved(CCEvent *pEvent);

};

#endif

TMenu.cpp

#include "TMenu.h"

#include "AppMacros.h"

#include "T02LayerSprite.h"

#include "T03LayerZorder.h"

#include "T04ColorLayer.h"

#include "T05Coordinate.h"

#include "T06BatchNode.h"

#include "T07Schedule.h"

#include "T08Touch.h"

#include "T09Memory.h"

#include "T10Label.h"

//通过sizeof(menu_array)求出数组占用的总空间大小

//通过sizeof(*menu_array)求出数组中的每个元素的大小

static char * menu_array[]

{

"T02LayerSprite.h",

"T03LayerZorder.h",21); font-family:新宋体; font-size:9.5pt">"T04ColorLayer.h",21); font-family:新宋体; font-size:9.5pt">"T05Coordinate.h",21); font-family:新宋体; font-size:9.5pt">"T06BatchNode.h",21); font-family:新宋体; font-size:9.5pt">"T07Schedule.h",21); font-family:新宋体; font-size:9.5pt">"T08Touch.h",21); font-family:新宋体; font-size:9.5pt">"T09Memory.h",21); font-family:新宋体; font-size:9.5pt">"T10Label.h"

};

CCScene * TMenu::scene()

{

scene = CCScene::create();

TMenu * layer = create();

scene->addChild(layer);

return scene;

}

bool init()

{

CCLayer::init();

menu = CCMenu::create();

//通过下面的方式实现将菜单添加menu

for (int i = 0; i < sizeof(menu_array) / sizeof(*menu_array); i++)

{

CCMenuItem * item = CCMenuItemFont::create(menu_array[i],

this,

menu_selector(menuCallback));

menu->addChild(item);

}

//设置菜单显示方向为垂直方向

menu->alignItemsVertically();

addChild(menu);

CCArray * array = menu->getChildren();

CCObject * obj;

int i = 0;

CCARRAY_FOREACH(array,obj)

{

CCMenuItem * item = (CCMenuItem*)obj;

item->setTag(i + 1 + 10000);

i++;

}

CCMenuItem * closeItem = CCMenuItemImage::create("CloseNormal.png","CloseSelected.png");

closeItem->setTarget(this,closeCallback));

CCMenu * menu2 = create(closeItem,138); font-family:新宋体; font-size:9.5pt">NULL);

menu2->setPosition(CCPointZero);

closeItem->setPosition(ccp(winSize.width - closeItem->getContentSize().width / 2,138); font-family:新宋体; font-size:9.5pt">winSize.height - closeItem->getContentSize().height / 2));

addChild(menu2);

//设置触摸事件,为可touch

setTouchEnabled(true);

setTouchMode(kCCTouchesOneByOne);

return true;

}

//当点击到菜单之后跳转到指定的CCScene中去

void CCObject * sender)

{

CCMenuItem *)sender;

CCScene * pScene = NULL;

switch (item->getTag() - 10000)

{

case 1:

pScene = T02LayerSprite::scene();

break;

case 2:

pScene = T03LayerZorder::scene();

break;

case 3:

pScene = T04ColorLayer::scene();

break;

case 4:

pScene = T05Coordinate::scene();

break;

case 5:

pScene = T06BatchNode::scene();

break;

case 6:

pScene = T07Schedule::scene();

break;

case 7:

pScene = T08Touch::scene();

break;

case 8:

pScene = T09Memory::scene();

break;

case 9:

pScene = T10Label::scene();

break;

}

if (pScene != NULL)

{

//通过CCDirectorsharedDirector来实现场景的切换

CCDirector::sharedDirector()->replaceScene(pScene);

}

}

//当点击关闭的时候游戏退出

void CCObject * sender)

{

exit(0);

}

bool CCEvent *pEvent)

{

return true;

}

void CCEvent *pEvent){

CCRect rcWin = CCRect(0,40,138); font-family:新宋体; font-size:9.5pt">winSize.width,138); font-family:新宋体; font-size:9.5pt">winSize.height - 80);

CCMenuItem * firstItem = (CCMenuItem *)menu->getChildren()->objectAtIndex(0);

CCMenuItem * lastItem = (lastObject();

CCPoint firstInWorld = menu->convertToWorldSpace(firstItem->getPosition());

CCPoint lastInWorld = menu->convertToWorldSpace(lastItem->getPosition());

//其中Delta的坐标表示的是开始坐标的减去最后坐标的差值

if (rcWin.containsPoint(firstInWorld) && pTouch->getDelta().y<0)

return;

if (rcWin.containsPoint(lastInWorld) && pTouch->getDelta().y>0)

return;

menu->setPositionY(menu->getPositionY() + pTouch->getDelta().y);

}

运行结果:

@H_543_2403@

相关文章

操作步骤 1、创建cocos2d-x工程 2、新建 Scene1.cpp Scene1.h Scene1.h代码 #ifndef __SCENE1_H__#defi...
开发环境:OS(WINDOWS 8.1 X64 企业版) cocos2d-x 2.2.1 vs2010 想给vs安装上cocos的模版,执行Install...
把创建项目做成一个批处理,当创建项目时可以省时省力很多。 操作步骤 1、在 E:cocos2d-x-2.2.1toolspr...
https://www.cnblogs.com/JiaoQing/p/3906780.html 四个响应函数 1 EventListenerPhysicsContact* evC...
转载于 http://www.cnblogs.com/kenkofox/p/3926797.html 熟悉js的dom事件或者flash事件的,基本都能立...
ScrollView(滚动容器)加载大量item时会导致游戏界面的卡顿,严重时整个界面会出现卡死的情况。最近项...