views:

94

answers:

2

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.

A: 

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.

thyrgle
A: 

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