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

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

概述 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 

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 "T04@R_301_6004@Layer.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">"T04@R_301_6004@Layer.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 = T04@R_301_6004@Layer::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_516_2403@

总结

以上是内存溢出为你收集整理的3.cocos2dx之Menu,通过menu来实现场景的切换全部内容,希望文章能够帮你解决3.cocos2dx之Menu,通过menu来实现场景的切换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1005790.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存