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

前端之家收集整理的这篇文章主要介绍了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_402_2403@

猜你在找的Cocos2d-x相关文章