Cocos2D (8) ---- CCMenu & amp; CCMenuItem, cocos2d ---- ccmenu

Source: Internet
Author: User

Cocos2D (8) ---- CCMenu & CCMenuItem, cocos2d ---- ccmenu

In the game, some menu items are usually provided for the user to start the game, pause \ continue the game, open \ close the music, or return to the previous interface. For example, the menu items marked with red wireframes in the following two figures

We can use CCMenu and CCMenuItem to implement the preceding menu functions. CCMenu inherits from CCLayer and can only be added as subnodes. A CCMenuItem can represent a menu item.

CCMenuItem inherits from CCNode. Let's look at its inheritance structure.

Run the code below.

[Java]View plaincopy
  1. // The first menu item: CCMenuItemFont
  2. // Set the text size
  3. [CCMenuItemFont setFontSize: 50];
  4. CCMenuItemFont * item1 = [CCMenuItemFont itemFromString: @ "text menu" block: ^ (id sender ){
  5. NSLog (@ "click the text menu ");
  6. }];
  7. // The text is yellow
  8. Item1.color = ccc3( 255,255, 0 );
  9. // The second menu item: CCMenuItemToggle
  10. // Reset the font and size.
  11. [CCMenuItemFont setFontName: @ "Courier New"];
  12. [CCMenuItemFont setFontSize: 40];
  13. // On is blue
  14. CCMenuItemFont * on = [CCMenuItemFont itemFromString: @ "On"];
  15. On. color = ccc3( 0,255,255 );
  16. // The value of off is red.
  17. CCMenuItemFont * off = [CCMenuItemFont itemFromString: @ "Off"];
  18. Off. color = ccc3 (255, 0, 0 );
  19. CCMenuItemToggle * item2 = [CCMenuItemToggle itemWithBlock: ^ (id sender ){
  20. NSLog (@ "Switch switch status ");
  21. } Items: on, off, nil];
  22. // The third menu item: CCMenuItemSprite
  23. // Default genie
  24. CCSprite * normal = [CCSprite spriteWithFile: @ "lufy.png"];
  25. // Genie (red) when pressed)
  26. CCSprite * selected = [CCSprite spriteWithFile: @ "lufy.png"];
  27. Selected. color = ccRED;
  28. CCMenuItemSprite * item3 = [CCMenuItemSprite itemFromNormalSprite: normal selectedSprite: selected block: ^ (id sender ){
  29. NSLog (@ "click the image button ");
  30. }];
  31. // Add menu items to the menu in order
  32. CCMenu * menu = [CCMenu menuWithItems: item1, item2, item3, nil];
  33. // Center the menu
  34. CGSize winSize = [CCDirector shareddire]. winSize;
  35. Menu. position = ccp (winSize. width * 0.5f, winSize. height * 0.5f );
  36. // Arrange menu items vertically, with 30 Vertical spacing between each menu item
  37. [Menu alignItemsVerticallyWithPadding: 30];
  38. // Add the menu to the Layer
  39. [Self addChild: menu];
Default



Click "On" in the middle and turn it to red off.


After a long pressed avatar, the Avatar turns red



The usage of other CCMenuItem is similar.


Address: http://blog.csdn.net/q199109106q/article/details/8604273

Thank you ~!


Cocos2d CCMenu Problems

Character_Info_menu
Set the position to ccp (0, 0). Try running the position outside the area displayed on the screen.

COCOS2D was just started. In the menu button event in CCMenu, there is a pressing effect, but there is no function specified by the callback button.

I really didn't understand how you wrote it. At least:
CCMenuItemFont * font = [CCMenuItemFont itemWithTarget: self selector: @ selector (myMethod! There is no selector to add now!
Here is an example of 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];
Then the corresponding function starButtonTapped will be available.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.