Cocos2d CCMenuItem animation when choosing

I have CCMenu with 5 CCMenuItem s. When a user touches a menu item, I want the menu item to move 10 pixels to the right to distinguish it from others. I tried to make each menu item a global variable so that I could say: if (item.isSelected) { [item runAction:blah]; } if (item.isSelected) { [item runAction:blah]; } But it did nothing. This is my code:

 CCLabelTTF *sin = [CCLabelTTF labelWithString:@"Single Player" dimensions:labelSize alignment:UITextAlignmentLeft fontName:font fontSize:20]; item1 = [CCMenuItemLabel itemWithLabel:sin target:self selector:@selector(goToSinglePlayer:)]; CCLabelTTF *spl = [CCLabelTTF labelWithString:@"Splitscreen" dimensions:labelSize alignment:UITextAlignmentLeft fontName:font fontSize:20]; item2 = [CCMenuItemLabel itemWithLabel:spl target:self selector:@selector(goToSplitscreen:)]; CCLabelTTF *ach = [CCLabelTTF labelWithString:@"Achievements" dimensions:labelSize alignment:UITextAlignmentLeft fontName:font fontSize:20]; item3 = [CCMenuItemLabel itemWithLabel:ach target:self selector:@selector(goToAchievements:)]; CCLabelTTF *str = [CCLabelTTF labelWithString:@"Store" dimensions:labelSize alignment:UITextAlignmentLeft fontName:font fontSize:20]; item4 = [CCMenuItemLabel itemWithLabel:str target:self selector:@selector(goToStore:)]; CCLabelTTF *set = [CCLabelTTF labelWithString:@"Settings" dimensions:labelSize alignment:UITextAlignmentLeft fontName:font fontSize:20]; item5 = [CCMenuItemLabel itemWithLabel:set target:self selector:@selector(goToSettings:)]; CCMenu * mainMenu = [CCMenu menuWithItems:item1, item2, item3, item4, item5, nil]; [mainMenu setColor:ccBLACK]; [mainMenu alignItemsVerticallyWithPadding:10]; mainMenu.position = ccp(90, 90); [self addChild:mainMenu]; if (item1.isSelected) { [item1 runAction:[CCMoveTo actionWithDuration:1.0f position:ccp(120, 90)]]; } 

My question is: how can I achieve the effect that I mentioned earlier? I want the selected CCMenuItem move 10 pixels to the right when the user touches it, but does not release it, and then returns to its normal position when the touch item leaves this menu item. Also, where should I put this animation code? In my init function? thanks for the help

+6
source share
2 answers

If you want to change the out-of-box behavior of the CCMenuItemLabel object, you will need a subclass for a specific cocos2d class. The methods you need to override are

 -(void) selected{ // coco default is to scale up by 10% // place your code to displace the label. self.position=ccp(self.position.x-10,self.position.y); } -(void) unselected{ // coco default is to bring back scale to originalScale. self.position=ccp(self.position.x+10,self.position.y); } 

The selected method is called when the finger touches the mark. The unselected method is called when the finger is raised or pulled out of the mark. I just showed you a basic (very) approach to selected / unselected behavior, experimenting with it. There are problems with synchronization. I would avoid using animations as a first try. Look at the code in the CCMenuItemLabel class if you need an example with animation.

+9
source

Check the following line for towing the code:

  CCMenuItem *item31 = [CCMenuItemImage itemFromNormalImage:@"btn_on.png" selectedImage:@"btn_on_hover.png"]; CCMenuItem *item32 = [CCMenuItemImage itemFromNormalImage:@"btn_off.png" selectedImage:@"btn_off_hover.png"]; 
  • As in the code above, you can configure btn_on_hover.png to look like a 10px offset to the right or anywhere.
  • You can accomplish your task in many ways, since cocos2d is open source. check out the CCMenu.h class. You can change the class according to your requirement.
  • for example, you can make changes in the following code fragment in the CCMenu.h class.

      #pragma mark Menu - Touches #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED 

Let me know in case of any queries. Regards, Neil.

+7
source

Source: https://habr.com/ru/post/915051/


All Articles