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
- // The first menu item: CCMenuItemFont
- // Set the text size
- [CCMenuItemFont setFontSize: 50];
- CCMenuItemFont * item1 = [CCMenuItemFont itemFromString: @ "text menu" block: ^ (id sender ){
- NSLog (@ "click the text menu ");
- }];
- // The text is yellow
- Item1.color = ccc3( 255,255, 0 );
- // The second menu item: CCMenuItemToggle
- // Reset the font and size.
- [CCMenuItemFont setFontName: @ "Courier New"];
- [CCMenuItemFont setFontSize: 40];
- // On is blue
- CCMenuItemFont * on = [CCMenuItemFont itemFromString: @ "On"];
- On. color = ccc3( 0,255,255 );
- // The value of off is red.
- CCMenuItemFont * off = [CCMenuItemFont itemFromString: @ "Off"];
- Off. color = ccc3 (255, 0, 0 );
- CCMenuItemToggle * item2 = [CCMenuItemToggle itemWithBlock: ^ (id sender ){
- NSLog (@ "Switch switch status ");
- } Items: on, off, nil];
- // The third menu item: CCMenuItemSprite
- // Default genie
- CCSprite * normal = [CCSprite spriteWithFile: @ "lufy.png"];
- // Genie (red) when pressed)
- CCSprite * selected = [CCSprite spriteWithFile: @ "lufy.png"];
- Selected. color = ccRED;
- CCMenuItemSprite * item3 = [CCMenuItemSprite itemFromNormalSprite: normal selectedSprite: selected block: ^ (id sender ){
- NSLog (@ "click the image button ");
- }];
- // Add menu items to the menu in order
- CCMenu * menu = [CCMenu menuWithItems: item1, item2, item3, nil];
- // Center the menu
- CGSize winSize = [CCDirector shareddire]. winSize;
- Menu. position = ccp (winSize. width * 0.5f, winSize. height * 0.5f );
- // Arrange menu items vertically, with 30 Vertical spacing between each menu item
- [Menu alignItemsVerticallyWithPadding: 30];
- // Add the menu to the Layer
- [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.