CCMenuItem scaling in Cocos2d (Objective-C)

I am trying to create CCMenuItem with scaled images. For example, I tried:

CCSprite* normalSprite = [CCSprite spriteWithFile:@"button_play.png"]; CCSprite* selectedSprite = [CCSprite spriteWithFile:@"button_play.png"]; selectedSprite.scale = 1.2; CCMenuItem menuItem = [CCMenuItemSprite itemFromNormalSprite:normalSprite selectedSprite:selectedSprite target:self selector:@selector(onPlay:)]; 

But it seems that CCMenuItemSprite is ignoring the scale of the main sprites. Is there a way to do this (besides creating only scaled versions of the base images)? Thanks.

+4
source share
3 answers

Thyrgle correctly describes the operation of CCMenuItem.

However, of course, there is a way to do what you want. All you have to do is subclass CCMenuItem and override the selected and unselected methods to achieve what you want. In fact, I'm sure you could just cut and paste the code from CCMenuItemLabel, because scaling an element to 1.2 is exactly what it does. (In fact, he does it better, as he animates zooming.)

 -(void) selected { // subclass to change the default action if(isEnabled_) { [super selected]; [self stopActionByTag:kZoomActionTag]; CCAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:1.2f]; zoomAction.tag = kZoomActionTag; [self runAction:zoomAction]; } } -(void) unselected { // subclass to change the default action if(isEnabled_) { [super unselected]; [self stopActionByTag:kZoomActionTag]; CCAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:1.0f]; zoomAction.tag = kZoomActionTag; [self runAction:zoomAction]; } } 
+4
source

The CCMenuItemImage class is also available to display an image with its scale in CCMenu. Please check this link http://www.cocos2d-iphone.org/forum/topic/8310

[mainMenu alignItemsVerticallyWithPadding: 15.0f];

+1
source

There is no other way. The fact is that menuItem only confirms the file part of sprites. It does not look at other properties, such as the scale property.

0
source

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


All Articles