cocos2d-x基本类(三)

来源:互联网 发布:北方交通大学网络教育 编辑:程序博客网 时间:2024/05/01 21:08

CCMenuA CCMenu extends CCLayerCCMenuItemCCMenuItem base class, extends CCNodeCCMenuItemAtlasFontA CCMenuItemAtlasFont Helper class that creates a MenuItemLabel class with a LabelAtlasCCMenuItemFontA CCMenuItemFont Helper class that creates a CCMenuItemLabel class with a LabelCCMenuItemImageCCMenuItemImage accepts images as itemsCCMenuItemLabelAn abstract class for "label" CCMenuItemLabel items Any CCNode that supports the CCLabelProtocol protocol can be addedCCMenuItemSpriteCCMenuItemSprite accepts CCNode<CCRGBAProtocol> objects as itemsCCMenuItemToggleA CCMenuItemToggle A simple container class that "toggles" it's inner items The inner itmes can be any MenuItem

CCMenu继承图:

CCMenu

CCMenuItem继承图:

菜单:CCMenu 和 CCMenuItem及其子类 

CCMenuItem

使用列子:

[cpp] view plaincopy
  1. CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(  
  2.                                         "CloseNormal.png",  
  3.                                         "CloseSelected.png",  
  4.                                         this,  
  5.                                         menu_selector(HelloWorld::menuCloseCallback) );  
  6.     pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );  
  7.   
  8.     // create menu, it's an autorelease object  
  9.     CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);  
  10.     pMenu->setPosition( CCPointZero );  
  11.     this->addChild(pMenu, 1);  

在 cocos2d 中判断互相重叠的 CCMenuItem:

转:http://www.cocoachina.com/cms/plus/view.php?aid=2451

cocos2d 提供的 CCMenu、CCMenuItem、CCMenuItemImage、CCMenuItemSprite 等类是构造菜单和按钮的常用工具类。不过 cocos2d 的 CCMenu 在判断互相重叠的 CCMenuItem 时存在一点小问题。

假设有三个按钮,如下图所示:

 


当玩家点击时,如果点击位置在 PLAY 按钮和另外两个按钮重叠的地方,那么总是 PLAY 按钮生效。出现这种情况的原因是 CCMenu 按照 CCMenuItem 添加的顺序来依次判断,PLAY 是第一个添加到 CCMenu 中的 CCMenuItem 对象,所以会忽略掉重叠区域的其他按钮。

要解决这个问题,需要从 CCMenu 派生一个继承类 CCMenuEx:

CCMenuEx.h

[cpp] view plaincopy
  1. #import "cocos2d.h"  
  2.   
  3. @interface CCMenuEx : CCMenu  
  4. {  
  5. }  
  6.   
  7. @end  

CCMenuEx.m

[cpp] view plaincopy
  1. #import "CCMenuEx.h"  
  2.   
  3. @implementation CCMenuEx  
  4.   
  5. -(CCMenuItem *) itemForTouch: (UITouch *) touch  
  6. {  
  7.     CGPoint touchLocation = [touch locationInView: [touch view]];  
  8.     touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];  
  9.   
  10.     CCMenuItem* item = nil;  
  11.     CCMenuItem* hitItem = nil;  
  12.     CCARRAY_FOREACH(children_, item){  
  13.         if ( [item visible] && [item isEnabled] ) {  
  14.             if (CGRectContainsPoint([item rect], touchLocation)) {  
  15.                 if (hitItem) {  
  16.                     if ([hitItem zOrder] < item.zOrder) {  
  17.                         hitItem = item;  
  18.                     }  
  19.                 } else {  
  20.                     hitItem = item;  
  21.                 }  
  22.             }  
  23.         }  
  24.     }  
  25.     return hitItem;  
  26. }  
  27.   
  28. @end  

最后在应用程序中将构造 CCMenu 对象的代码改为使用 CCMenuEx 即可。

CCMenuEx 不但判断点击位置是否在按钮上,还会判断按钮的叠放次序。在多个按钮重叠区域点击时,最上面的按钮会被触发。


原创粉丝点击