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(CCTouch
*pTouch,
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",

"T04ColorLayer.h",

"T05Coordinate.h",

"T06BatchNode.h",

"T07Schedule.h",

"T08Touch.h",

"T09Memory.h",

"T10Label.h"

};

CCScene *
TMenu::scene()

{

CCScene *
scene =
CCScene::create();

TMenu *
layer =
TMenu::create();

scene->addChild(layer);

return
scene;

}

bool
TMenu::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(TMenu::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,
menu_selector(TMenu::closeCallback));

CCMenu *
menu2 =
CCMenu::create(closeItem,
NULL);

menu2->setPosition(CCPointZero);

closeItem->setPosition(ccp(winSize.width
- closeItem->getContentSize().width
/ 2,

winSize.height
- closeItem->getContentSize().height
/ 2));

addChild(menu2);

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

setTouchEnabled(true);

setTouchMode(kCCTouchesOneByOne);

return
true;

}

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

void
TMenu::menuCallback(CCObject
* sender)

{

CCMenuItem *
item = (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)

{

//通过CCDirector的sharedDirector来实现场景的切换

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

}

}

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

void
TMenu::closeCallback(CCObject
* sender)

{

exit(0);

}

bool
TMenu::ccTouchBegan(CCTouch
*pTouch,
CCEvent *pEvent)

{

return
true;

}

void
TMenu::ccTouchMoved(CCTouch
*pTouch,
CCEvent *pEvent){

CCRect
rcWin =
CCRect(0,40,winSize.width,winSize.height
- 80);

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

CCMenuItem *
lastItem = (CCMenuItem
*)menu->getChildren()->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);

}

执行结果:

3.cocos2dx它Menu,由menu为了实现场景切换

版权声明:本文博客原创文章,博客,未经同意,不得转载。

上一篇:使用Promise规定来处理ajax请求的结果


下一篇:图的邻接表存储表示(C)