cocos2D(八)---- CCMenu && CCMenuItem,cocos2d----ccmenu

來源:互聯網
上載者:User

cocos2D(八)---- CCMenu && CCMenuItem,cocos2d----ccmenu

遊戲中經常會提供一些功能表項目讓使用者開始遊戲、暫停\繼續遊戲、開啟\關閉音樂或者是返回到上一個介面,比如下面兩張圖中用紅色線框標記的功能表項目

   

我們可以使用CCMenu和CCMenuItem實現上述的菜單功能,CCMenu繼承自CCLayer,只能添加CCMenuItem為子節點。一個CCMenuItem就可以表示一個功能表項目。

CCMenuItem繼承自CCNode,來看下它的繼承結構

下面直接上代碼

[java] view plaincopy
  1. // 第一個功能表項目:CCMenuItemFont  
  2. // 設定文字大小  
  3. [CCMenuItemFont setFontSize:50];  
  4. CCMenuItemFont *item1 = [CCMenuItemFont itemFromString:@"文字菜單" block:^(id sender) {  
  5.     NSLog(@"點擊了文字菜單");  
  6. }];  
  7. // 文字為黃色  
  8. item1.color = ccc3(255, 255, 0);  
  9.   
  10. // 第二個功能表項目:CCMenuItemToggle  
  11. // 重新設定字型和大小  
  12. [CCMenuItemFont setFontName:@"Courier New"];  
  13. [CCMenuItemFont setFontSize:40];  
  14. // on為青色  
  15. CCMenuItemFont *on = [CCMenuItemFont itemFromString:@"On"];  
  16. on.color = ccc3(0, 255, 255);  
  17. // off為紅色  
  18. CCMenuItemFont *off = [CCMenuItemFont itemFromString:@"Off"];  
  19. off.color = ccc3(255, 0, 0);  
  20. CCMenuItemToggle *item2 = [CCMenuItemToggle itemWithBlock:^(id sender) {  
  21.     NSLog(@"切換了開關狀態");  
  22. } items:on, off, nil];  
  23.   
  24. // 第三個功能表項目:CCMenuItemSprite  
  25. // 預設的精靈  
  26. CCSprite *normal = [CCSprite spriteWithFile:@"lufy.png"];  
  27. // 被按下時的精靈(紅色)  
  28. CCSprite *selected = [CCSprite spriteWithFile:@"lufy.png"];  
  29. selected.color = ccRED;  
  30. CCMenuItemSprite *item3 = [CCMenuItemSprite itemFromNormalSprite:normal selectedSprite:selected block:^(id sender) {  
  31.     NSLog(@"點擊了圖片按鈕");  
  32. }];  
  33.   
  34. // 按順序添加功能表項目到菜單中  
  35. CCMenu *menu = [CCMenu menuWithItems:item1, item2, item3, nil];  
  36. // 讓菜單置中  
  37. CGSize winSize = [CCDirector sharedDirector].winSize;  
  38. menu.position = ccp(winSize.width * 0.5f, winSize.height * 0.5f);  
  39. // 垂直方向排列功能表項目,每個功能表項目之間有30的垂直間距  
  40. [menu alignItemsVerticallyWithPadding:30];  
  41.   
  42. // 添加菜單到圖層中  
  43. [self addChild:menu];  
預設的



單擊中間的青色的"On"後,變為了紅色的off


長按下面的頭像後,頭像變為紅色



其他CCMenuItem的用法都大同小異


原文地址:http://blog.csdn.net/q199109106q/article/details/8604273

感謝作者~!


Cocos2d CCMenu問題

Character_Info_menu
位置設定成ccp(0,0); 試試 你這明顯在位置跑到螢幕顯示的地區外部了。
 
剛剛開始學COCOS2D,在CCMenu中的菜單按鍵事件中,有按下的效果,但是沒有回調按鍵指定的函數,教

我著實沒看懂你事怎麼寫的。至少應該有:
CCMenuItemFont *font = [CCMenuItemFont itemWithTarget:self selector:@selector(myMethod:)];吧!現在沒有selector添加的地方啊!
這裡有一個CCMenu的例子;
CCMenuItem *starMenuItem = [CCMenuItemImage itemFromNormalImage:@"ButtonStar.png" selectedImage:@"ButtonStarSel.png" target:self
selector:@selector(starButtonTapped:)];
starMenuItem.position = ccp(60, 60);
CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil];
starMenu.position = CGPointZero;
[self addChild:starMenu];
然後相應函數starButtonTapped,就可以有相應了。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.