peter61
peter61

Reputation: 279

scaling a CCMenuItem in Cocos2d (Objective-C)

I'm trying to make a CCMenuItem that has 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 looks like CCMenuItemSprite ignores the scale of the underlying sprites. Is there a way to do this (aside from just creating scaled versions of the underlying images)? Thanks.

Upvotes: 4

Views: 2817

Answers (3)

Nirav
Nirav

Reputation: 11

CCMenuItemImage Class is also available for displaying image with its scale in CCMenu.Please Check this link http://www.cocos2d-iphone.org/forum/topic/8310

[mainMenu alignItemsVerticallyWithPadding:15.0f];

Upvotes: 1

cc.
cc.

Reputation: 3051

Thyrgle is correct about how CCMenuItem works.

However, there certainly is a way to do what you want. All you need to do is subclass CCMenuItem and override the selected and unselected methods to achieve what you want. In fact, I'm pretty sure you could just cut and paste the code from CCMenuItemLabel, because scaling the item to 1.2 is exactly what it does. (In fact, it does it better, since it animates the scale change.)

-(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];
    }
}

Upvotes: 4

thyrgle
thyrgle

Reputation:

No there is not another way. The thing is that menuItem is only acknowledging the files part of the sprites. It is not looking at other properties such as the scale property.

Upvotes: 0

Related Questions