首页 > 代码库 > 3.cocos2dx之Menu,通过menu来实现场景的切换
3.cocos2dx之Menu,通过menu来实现场景的切换
1头文件
TMenu.h |
#ifndef__TMENU_H__ #define__TMENU_H__
#include"cocos2d.h" USING_NS_CC;
classTMenu :publicCCLayer { public: staticCCScene *scene(); CREATE_FUNC(TMenu); boolinit();
CCMenu *menu;
voidmenuCallback(CCObject * sender); voidcloseCallback(CCObject * sender);
virtualboolccTouchBegan(CCTouch *pTouch,CCEvent *pEvent); virtualvoidccTouchMoved(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)求出数组中的每个元素的大小 staticchar *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); returnscene; }
boolTMenu::init() { CCLayer::init();
menu =CCMenu::create(); //通过下面的方式实现将菜单项添加到menu中 for (inti = 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; inti = 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);
returntrue; }
//当点击到菜单之后跳转到指定的CCScene中去 voidTMenu::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); } }
//当点击关闭的时候游戏退出 voidTMenu::closeCallback(CCObject * sender) { exit(0); }
boolTMenu::ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent) { returntrue; }
voidTMenu::ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent){ CCRectrcWin =CCRect(0,40,winSize.width,winSize.height - 80); CCMenuItem *firstItem = (CCMenuItem *)menu->getChildren()->objectAtIndex(0); CCMenuItem *lastItem = (CCMenuItem *)menu->getChildren()->lastObject();
CCPointfirstInWorld =menu->convertToWorldSpace(firstItem->getPosition()); CCPointlastInWorld =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来实现场景的切换