开发者

scaling a CCMenuItem in Cocos2d (Objective-C)

开发者 https://www.devze.com 2023-01-03 23:01 出处:网络
I\'m trying to make a CCMenuItem that has scaled images. For example, I tried: CCSprite* normalSprite = [CCSprite spriteWithFile:@\"button_play.png\"];

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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消