在CCMenu中缩放CCMenuItemSprite项

时间:2012-08-17 01:18:04

标签: ios xcode cocos2d-iphone scaling ccmenuitem

我在使用带有缩放的CCMenuItemSprite菜单项的CCMenu时遇到问题。我正在尝试根据游戏正在播放的设备来缩放菜单项精灵(iPad需要将其缩放到大约1.5倍,而在iPhone上大约是0.75倍)

根据我的阅读,我们无法直接扩展CCSprite或CCMenuItemSprite,因为当它被添加到CCMenu时,触摸矩形无法正确更新。我相信我必须缩放CCMenu来缩放菜单项。

每当我这样做时,我的精灵似乎缩放到正确的大小,但它似乎也缩放了CCMenu位置坐标,但方向与我期望的方向相反。此外,一旦我超过某个阈值,菜单似乎完全消失。

有没有人对如何在CCMenu中缩放精灵有任何建议?

提前致谢。 Buzzrick

2 个答案:

答案 0 :(得分:4)

试试此代码........

CCMenuItemImage  *Btn1 = [CCMenuItemImage itemWithNormalImage:@"button1.png" selectedImage:@"button1_active.png" target:self selector:@selector(button1_click:)];

CCMenuItemImage  *Btn2 = [CCMenuItemImage itemWithNormalImage:@"button2.png" selectedImage:@"button2_active.png" target:self selector:@selector(button2_click:)];

CCMenu *Action_menu = [CCMenu menuWithItems:Btn1,Btn2, nil];

[Action_menu setPosition:ccp( 79, 288)];

float delayTime = 0.3f;

for (CCMenuItemFont *each in [Action_menu children]) 
    {
        each.scaleX = 0.0f;
        each.scaleY = 0.0f;
        CCAction *action = [CCSequence actions:
                            [CCDelayTime actionWithDuration: delayTime],
                            [CCScaleTo actionWithDuration:0.5F scale:1.0],
                            nil];
        delayTime += 0.2f;
        [each runAction: action];
    }

[self addChild:Action_menu];

答案 1 :(得分:2)

以下是我最终解决的问题: 基本上我首先创建了菜单元素,然后我整理了定位/缩放/旋转。这看起来好多了。 下面的代码示例是我使用单个精灵创建两个相对的左/右箭头按钮的地方

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"ButtonSprites.plist"];
    CCSpriteBatchNode *buttonSprites = [CCSpriteBatchNode batchNodeWithFile:@"ButtonSprites.png"];
    [self addChild:buttonSprites];

    CCSprite *arrowLeftSprite = [CCSprite spriteWithSpriteFrameNameOrFile:@"PageArrow"];
    CCSprite *arrowLeftSpriteSelected = [CCSprite spriteWithSpriteFrameNameOrFile:@"PageArrow"];
    arrowLeftSpriteSelected.opacity = 128;
    CCSprite *arrowRightSprite = [CCSprite spriteWithSpriteFrameNameOrFile:@"PageArrow"];
    CCSprite *arrowRightSpriteSelected = [CCSprite spriteWithSpriteFrameNameOrFile:@"PageArrow"];
    arrowRightSpriteSelected.opacity = 128;

    float buttonWidth = screenSize.width * ButtonWidthPercent;
    int contentPixelWidth = arrowLeftSprite.contentSize.width;
    float scale = buttonWidth / contentPixelWidth;

    CCMenuItemSprite *pageLeftMenu = [CCMenuItemSprite itemFromNormalSprite:arrowLeftSprite selectedSprite:arrowLeftSpriteSelected target:self selector:@selector(buttonPageLeft:)];
    CCMenuItemSprite *pageRightMenu = [CCMenuItemSprite itemFromNormalSprite:arrowRightSprite selectedSprite:arrowRightSpriteSelected target:self selector:@selector(buttonPageRight:)];

    //  First perform the creation
    CCMenu *menu = [CCMenu menuWithItems: mainMenu, pageLeftMenu, pageRightMenu, nil];
    menu.position = ccp(0, 0);

    //  THEN deal with layout,rotation, and scaling
    pageLeftMenu.scale = scale;
    pageLeftMenu.rotation = 180;
    pageRightMenu.scale = scale;

    float arrowHeight = screenSize.height * 0.2;
    pageLeftMenu.position = ccp(screenSize.width * 0.1, arrowHeight);
    pageRightMenu.position = ccp(screenSize.width * 0.9, arrowHeight);

    [self addChild:menu z:2];