我试图通过在场景上显示一些精灵并将menuitemsprite作为继续按钮来模拟cocos2d中的模态视图。在下面的代码中,我以模式显示我的游戏,并使用CCMenuItemSprite设置菜单;不响应触摸,以及CCMenuItemImage;确实有效。

    -(void) gameOver {

    CGSize size = [[CCDirector sharedDirector] winSize];
    self.menu.isTouchEnabled = NO;
    CCLayer *modalLayer = [[CCLayer alloc] init];
    [self addChild:modalLayer z:20];

    CCSprite *spriteGameOver = [CCSprite spriteWithFile:@"game_over.png"];
    spriteGameOver.position = ccp( size.width/2,size.height/2);

    CCLabelTTF *lblGameOver = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Game Over!\nScore %d/%d",numCorrect,questionIdx] dimensions:CGSizeMake(380, 300) alignment:CCTextAlignmentCenter fontName:@"Trebuchet MS" fontSize:50.0f];
    // position the label on the center of the screen

    lblGameOver.position =  ccp(size.width/2-200, size.height/2-100);
    lblGameOver.color = ccc3(20, 20, 20);
    lblGameOver.opacity = 0;
    // add the label as a child to this Layer
    [spriteGameOver addChild: lblGameOver];
    spriteGameOver.opacity = 0;
    [modalLayer addChild:spriteGameOver];

    CCSprite *spriteGameOverBtn = [CCSprite spriteWithFile:@"mainButton.png" rect:CGRectMake(0,0,300,60)];
    spriteGameOverBtn.position = ccp( size.width/2,size.height/2-100);
    CCLabelTTF *lblGameOverBtn = [CCLabelTTF labelWithString:@"Continue" dimensions:CGSizeMake(300, 60) alignment:CCTextAlignmentCenter fontName:@"Trebuchet MS" fontSize:40.0f];
    //lblGameOverBtn.position =  ccp(size.width/2-200, size.height/2-300);
    [lblGameOverBtn setAnchorPoint:ccp(0.0f,0.1f)];
    lblGameOverBtn.color = ccc3(20, 20, 20);
    lblGameOverBtn.opacity = 0;
    // add the label as a child to this Layer
    [spriteGameOverBtn addChild: lblGameOverBtn];
    spriteGameOverBtn.opacity = 0;

    CCMenuItemImage *itemH = [CCMenuItemImage itemFromNormalImage:@"backArrow.png" selectedImage:@"backArrowS.png" target:self selector:@selector(goToMain:)];
    itemH.position = ccp( size.width/2,size.height/2-100);

    CCMenuItemSprite *mGameOverBtn = [CCMenuItemSprite itemFromNormalSprite:spriteGameOverBtn selectedSprite:nil disabledSprite:nil target:self selector:@selector(goToMain:)];

    CCMenu *menuGO = [CCMenu menuWithItems: itemH,mGameOverBtn, nil];
    menuGO.position = ccp( 0, 0);

    [modalLayer addChild:menuGO z:21];




    [lblGameOverBtn runAction:[CCSequence actions:[CCDelayTime actionWithDuration: 1.75f],[CCFadeIn actionWithDuration: 1.75f],nil]];

    [spriteGameOverBtn runAction:[CCSequence actions:[CCDelayTime actionWithDuration: 1.75f],[CCFadeIn actionWithDuration: 1.75f],[CCDelayTime actionWithDuration: 3.75f],nil]];


    [lblGameOver runAction:[CCSequence actions:[CCDelayTime actionWithDuration: 1.75f],[CCFadeIn actionWithDuration: 1.75f],[CCDelayTime actionWithDuration: 3.75f],nil]];

    [spriteGameOver runAction:[CCSequence actions:[CCDelayTime actionWithDuration: 1.75f],[CCFadeIn actionWithDuration: 1.75f],[CCDelayTime actionWithDuration: 3.75f],nil]];

    //[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration: 2.75f],[CCCallFunc actionWithTarget:self selector:@selector(goToMain:)], nil]];

}

最佳答案

我的CCMenuItemSprite也有同样的问题。看来CCMenuItemSprite-class有点马车。就我而言,它不响应触摸。但是我发现它与CCMenuItemSprite中的多层Sprites有关。因此,当我使用单层Sprite时,它可以工作,但是当我使用包含多个嵌入式Sprite的Sprite时,它却不能工作。

我现在的“尚未就绪”解决方案是在初始化后立即将contentSize设置为CCMenuItemSprite的适当大小:

        CCMenuItemSprite * menuItem = [CCMenuItemSprite itemWithNormalSprite:multiLayeredSprite selectedSprite:nil target:self selector:@selector(clickHandler:)];
    [s setContentSize:backgroundSprite.contentSize];
    CCMenu * menu = [CCMenu menuWithItems:
            menuItem,
            nil];


现在正在接收触摸事件。我现在唯一的问题是Rectangle的定位...它仍然在右上角。我将尝试找出如何解决此问题。
希望这可以澄清一些问题。

10-08 03:02